如何为数据表中的每一列添加搜索输入?

时间:2019-07-13 11:29:20

标签: javascript html ajax laravel datatable

在我的数据表中,搜索列不起作用。因为,我通过单击页码从数据库10到10用ajax每页获取数据。我的意思是分页在服务器代码中。可修改的列搜索服务器端只给我第一页结果。我该怎么办?

这是我的数据表脚本:

$('#table_id').DataTable({
        "order": [[ 3, "desc" ]],
        "destroy": true,
        "dom": "Bfrtip",
        "serverSide": true,
        "processing" : true,
        "searching": false,
        "aoColumnDefs": [
            { "bSortable": false, "aTargets": [ 0, 1, 2, 3 , 4] },
            { "bSearchable": false, "aTargets": [ 0, 1, 2, 3, 4 ] }
        ] ,
        "pageLength": 10,
        "ajax" : function (data , callback, settings) {

            $.ajax({
                url: url
                type: 'GET',
                contentType: 'application/json; charset=utf-8',
                data: {params: {page: data.start / data.length  ,pageSize: data.length},
                    RecordsStart: data.start,
                    PageSize: data.length
                },
                success: function( data, textStatus, jQxhr ){
                    body = data.Data;

                    $('.box-header').on('click', '#addNewRecord' ,function () {
                        e.preventDefault();
                    });
                    callback({
                        // draw: data.draw,
                        data: body,
                        recordsTotal:  data.TotalRecords,
                        recordsFiltered:  data.RecordsFiltered,
                    });
                },
                error: function( jqXhr, textStatus, errorThrown ){
                }
            });
        },
        "columns": [
            {
                "title":"Id",
                "class":'id',
                "data": "id"
            },
            {
                "title": "name",
                "class":"name",
                "data": "name"
            },
            {
                "title": "status",
                "class":"status",
                "data": "status"
            }
        ]
    });
 $(document).on("keyup","#searchName", function () {
        $name = document.getElementById("searchName").value;
        $.ajax({
            url: url,
            type:"GET",
            data:{name:$name},
            success: function (data) {
                //Here I don't know how to add search record 
            }
        })

和PHP代码:

 public function searchTable(Request $request)
{
    $keyWord = $request->input('searchName', 'default_value');
    $name = myTable::select('id','name', 'status')
        ->where('name', 'like',  '%' . $keyWord .'%')->get();

    return response()->json([
        'info' => $name,
    ]);

}

1 个答案:

答案 0 :(得分:0)

以下代码显示了如何搜索各个列:

$(document).ready(function() {
    // Setup - add a text input to each footer cell
    $('#example tfoot th').each( function () {
        var title = $(this).text();
        $(this).html( '<input type="text" placeholder="Search '+title+'" />' );
    } );

    // DataTable
    var table = $('#example').DataTable();

    // Apply the search
    table.columns().every( function () {
        var that = this;
        $( 'input', this.footer() ).on( 'keyup change clear', function () {
            if ( that.search() !== this.value ) {
                that
                    .search( this.value )
                    .draw();
            }
        } );
    } );
} );

就像下面的图片一样:

enter image description here