简体   繁体   English

使用 jQuery 处理服务器端的数据表

[英]Datatables on server side processing with jQuery

I am new to datatables.我是数据表的新手。 I am trying to find solution for server side processing since last two days but didnt find the solution.自前两天以来,我一直在尝试寻找服务器端处理的解决方案,但没有找到解决方案。

My JS code is我的JS代码是

this.$("#example").DataTable({
            "processing": true,
            "serverSide": true,
            "ajax": "../employees.json",
            "columns": [{
                "data": "Name"
            }, {
                "data": "Age"
            }, {
                "data": "Country"
            }, {
                "data": "Address"
            }, {
                "data": "Married"
            }]
        });

Datatable renders JSON in table format. Datatable 以表格格式呈现 JSON。 But sorting, pagination and search operation is not working.但是排序、分页和搜索操作不起作用。 It shows all results on the first page no matter how much value I have selected from dropdown无论我从下拉列表中选择了多少值,它都会在第一页上显示所有结果

Also at bottom it shows message like "Showing 0 to 0 of 0 entries (filtered from NaN total entries)"同样在底部它会显示类似“显示 0 到 0 个条目中的 0 个条目(从 NaN 总条目过滤)”的消息

If I pass serverSide: false.如果我通过 serverSide: false。 everything works fine.一切正常。 But I want server side processing of the same但我想要相同的服务器端处理

Any help would be appreciated任何帮助,将不胜感激

When you set serverSide to true, you are telling DataTables that the server will handle all the sorting and paging instead of DataTables.当您将 serverSide 设置为 true 时,您是在告诉 DataTables 服务器将处理所有排序和分页而不是 DataTables。 DataTables will just display the data as-is from the server. DataTables 只会按原样显示来自服务器的数据。

So if your server is ignoring all the sorting and paging parameters sent from DataTables, then the data will look funny.因此,如果您的服务器忽略从 DataTables 发送的所有排序和分页参数,那么数据看起来会很有趣。 (In your case it seems that the server is listing all records, regardless of the requested page size). (在您的情况下,无论请求的页面大小如何,服务器似乎都列出了所有记录)。

You have two options:您有两个选择:

  1. Keep serverSide false.保持服务器端为假。 Let the server send DataTables all the data and let it handle the sorting, paging, ordering.让服务器向 DataTables 发送所有数据并让它处理排序、分页、排序。 Usually this is sufficient for a moderate number of records (50,000 records or less)通常这对于中等数量的记录(50,000 条记录或更少)就足够了
  2. Modify server to properly handle sorting, paging, ordering as requested by DataTables.修改服务器以按照 DataTables 的要求正确处理排序、分页、排序。 You will need to provide more information (such as total number of records for paging purposes) because DataTables can't deduce that information from 1 page worth of data.您将需要提供更多信息(例如用于分页的记录总数),因为 DataTables 无法从 1 页的数据中推断出该信息。 See https://datatables.net/manual/server-side#Sent-parameters to see what parameters DataTables sends to the server and https://datatables.net/manual/server-side#Returned-data for details on what the server should return.https://datatables.net/manual/server-side#Sent-parameters看什么参数数据表发送到服务器和https://datatables.net/manual/server-side#Returned-data详情,以了解该服务器应该返回。

In the return of your json form must have these:在你的 json 表单的返回中必须有这些:

iTotalRecords : (Total rows),
iTotalDisplayRecords : (Total rows to display in your grud),
aaData : {(Your data)}.

Works for me.为我工作。

Ther are some options that you must set true有一些选项必须设为 true

eg例如

this.$("#example").DataTable({
            "processing": true,
            "serverSide": true,
            "ajax": "../employees.json",
            "columns": [{
                "data": "Name"
            }, {
                "data": "Age"
            }, {
                "data": "Country"
            }, {
                "data": "Address"
            }, {
                "data": "Married"
            }],            
            'scrollCollapse': true,
            'ordering': true,
            'order': [[0, 'asc']],
            'searching': true,
            'paging': true,
        });

This may be late but you can use fnInfoCallback so for example:这可能会晚,但您可以使用 fnInfoCallback 例如:

"fnInfoCallback": function( oSettings, iStart, iEnd, iMax, iTotal, sPre ) {
                           if (isNaN(iTotal)) { 
                           return '';
                           }

                    return "Showing " + iStart +" to "+ iEnd + " of " + iTotal + " entries";
                      },

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM