dsmlf1207915 2016-10-30 06: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 07: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();
    
        }
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?