jQuery 数据表:使用 Ajax 分页进行搜索和过滤

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32438702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:53:28  来源:igfitidea点击:

jQuery Datatables : Searching and filtering with Ajax pagination

jquerysearchdatatablesfilteringpaginate

提问by Pauloscorps

I have a SQL table with 36000 entries to show in a Datatables list. The pagination works well since I develop it like this :

我有一个包含 36000 个条目的 SQL 表要显示在数据表列表中。分页效果很好,因为我是这样开发的:

var table = $('.datatable').DataTable({
    pageLength : 20,
    lengthChange : false,
    processing : true,
    serverSide : true,
    ajax : {
        url :"ajax.php",
        type: "post",
    }
});

In my file ajax.php, I simply echo my lines (JSON encoded), according to the limit set by the page number.

在我的文件 ajax.php 中,我只是根据页码设置的限制回显我的行(JSON 编码)。

The problem is native filtering and searching no longer works. When I want to filter a column, the "Processing" layer appears, then disappears but my data' is still the same. When I want to research through the table, nothing happens.

问题是本机过滤和搜索不再有效。当我想过滤一列时,“处理”层出现,然后消失,但我的数据'仍然相同。当我想通过表格进行研究时,什么也没有发生。

So, here are my questions :

所以,这是我的问题:

  1. How can I restore searching and filtering ?
  2. How can I filter and search through all the lines (not only the ones that are showed) ? With Ajax, yes, but how in Jquery ?
  1. 如何恢复搜索和过滤?
  2. 如何过滤和搜索所有行(不仅是显示的行)?使用 Ajax,是的,但是在 Jquery 中如何?

Thank's in advance

提前致谢

Edit :Thank's to Abdul Rehman Sayed, I manage to do the search part. Here is what I have done :

编辑:感谢 Abdul Rehman Sayed,我设法完成了搜索部分。这是我所做的:

var table = $('.datatable').DataTable({
    pageLength : 20,
    lengthChange : false,
    processing : true,
    serverSide : true,
    ajax : {
        data : function(d) {
            d.searching = get_search($('.datatable'));
        },
        url :"ajax.php",
        type: "post",
    },
    searching : false,
});

$('.datatable thead th').each(function() {
    var title = $(this).data('name');
    $('.datatable').find('tfoot tr').append('<td><input type="text" name="'+title+'"/></td>');
});

table.columns().every(function() {
    var that = this;
    $('input', this.footer()).on('keyup', function(e) {
        that.search(this.value).draw();
    }
});

function get_search(datatable) {
    var result = [];
    datatable.find('tfoot').find('input').each(function() {
        result.push([$(this).attr('name'), $(this).val()]);
    });
    return result;
}

For filtering, I develop an ugly code :

为了过滤,我开发了一个丑陋的代码:

$('.datatable').find('th').click(function() {
    var item = $(this);
    removeClasses($('.datatable'), item.index());
    if(item.hasClass('sorting_asc')) {
        item.removeClass('selected_asc').addClass('selected_desc');
    } else {
        item.removeClass('selected_desc').addClass('selected_asc');
    }
});

function get_sorting(datatable) {
    var result = false;
    datatable.find('th').each(function() {
        var item = $(this);
        var name = item.data('name');
        if(item.hasClass('selected_asc')) {
            result = name+' ASC';
        } else if(item.hasClass('selected_desc')) {
            result = name+' DESC';
        } else {
            // continue
        }
    });
    return result;
}

function removeClasses(datatable, index) {
    datatable.find('th').each(function() {
        if($(this).index() !== index) {
            $(this).removeClass().addClass('sorting');
        }
    });
}

回答by Abdul Rehman Sayed

You will have to do all the searching & filtering on server side.

您必须在服务器端进行所有搜索和过滤。

For every request of search/filter or page, the datatable passes all of this as form data to the server page. Refer https://www.datatables.net/manual/server-side

对于搜索/过滤器或页面的每个请求,数据表将所有这些作为表单数据传递到服务器页面。参考https://www.datatables.net/manual/server-side

You will have to use this form data to filter/search/paginate on the records on sql table & pass it accordingly to the client.

您必须使用此表单数据对 sql 表上的记录进行过滤/搜索/分页,并将其相应地传递给客户端。

The datatable merely shows what it gets from the server.

数据表仅显示它从服务器获取的内容。