dsmlf1207915 2016-10-30 14:52
浏览 89
已采纳

如何使用默认搜索值运行datatable api?

I was wondering how to load datatable api with default search value. I tried something like this.

$(document).ready(function{
    $('#datatable').DataTable();
    $("input[type=search]").val('john')
});

But since, in order to show the search results the search box needs to be submitted. How do I this ?

  • 写回答

1条回答 默认 最新

  • donglun1020 2016-10-30 15:46
    关注

    Try triggering a keyup event...

    $(document).ready(function{
        $("#datatable").DataTable();
        $("input[type='search']").val("john").trigger("keyup");
    });
    



    EDIT

    I found a way to disable the pagination when a search is active.
    Meaning when the search input field isn't empty.

    I refined my solution to also hide the unsefull controls, considering a no-pagination dataTable.

    See it on CodePen.

    searchField.on("input",function(){
    
        // Grab the seach term (text inputed in the search field)
        searchTerm = $(this).val();
        console.log("searchTerm: "+searchTerm);
    
        // The paginate links and buttons...
        var paginate = $("#myTable").siblings('.dataTables_paginate');
    
        // Remove the whole table when search term is empty.
        if(searchTerm==""){
            console.log("searchField: empty");
    
            // Set pagination to desired length
            // and show controls.
            myTable.page.len( paginationLength ).columns.adjust().draw();
            paginate.show();
            pageLenghtSelect.show();
        }else{
            console.log("searchField: NOT empty");
    
            // Set pagination to no pagination at all (only one page).
            // and hide controls.
            myTable.page.len( -1 ).columns.adjust().draw();
            paginate.hide();
            pageLenghtSelect.hide();
    
        }
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan