从serverSide设置为true获取分页jQuery DataTable中的所有列值

时间:2021-09-18 14:29:18

I am trying to implement the footerCallback to find the sum of all elements in one of the columns of my paginated data table. When I try to calculate the total amount in all the pages, only the sum of rows in the current page is being calculated. My code for footerCallback is as follows:

我正在尝试实现footerCallback来查找我的分页数据表的一列中的所有元素的总和。当我尝试计算所有页面中的总金额时,仅计算当前页面中的行总和。我的footerCallback代码如下:

"footerCallback": function ( row, data, start, end, display ) {
                            var api = this.api(), data;

                            // Remove the formatting to get integer data for summation
                            var intVal = function ( i ) {
                                return typeof i === 'string' ?
                                    i.replace(/[\$,]/g, '')*1 :
                                    typeof i === 'number' ?
                                        i : 0;
                            };

                            // Total over all pages
                            total = api
                            .column( 5 )
                            .data()
                            .reduce( function (a, b) {
                                return intVal(a) + intVal(b);
                            }, 0 );

                            // Total over this page
                            pageTotal = api
                                .column( 5, { page: 'current'} )
                                .data()
                                .reduce( function (a, b) {
                                    return intVal(a) + intVal(b);
                                }, 0 );

                            // Update footer
                            jQuery( api.column( 5 ).footer() ).html(
                                '$'+pageTotal +' ( $'+ total +' total)'
                            );
                        }

This function is given in the ajax call along with columns and columndefs. Both, pageTotal and total are returning the same value. When I try the table.column( "#header5" ).data(); command in the console, even it is returning just the data in the current page. In one of the websites, I read that, when server side is given true, data() and fnGetData() returns just the data in the current page. Is that true? If yes, how can I get the data in other pages?

此函数在ajax调用中与columns和columndef一起提供。 pageTotal和total都返回相同的值。当我尝试table.column(“#header5”)。data();控制台中的命令,即使它只返回当前页面中的数据。在其中一个网站中,我读到,当服务器端为真时,data()和fnGetData()只返回当前页面中的数据。真的吗?如果是,我如何在其他页面中获取数据?

1 个解决方案

#1


0  

Yes, that's true. If you set the server side option to true, the datatables expect you to do the pagination processing on your server (i.e. the datatables request will ask for a given page of the data set, and your server must be ready to return the results of the query for the given page). If you are not dealing with great amounts of data and wish to avoid making the pagination yourself, you better set the serverside option to false and datatables will take care of it. This way all you need to do is provide all of the data at once. More on it: https://datatables.net/examples/data_sources/server_side.html

是的,这是真的。如果将服务器端选项设置为true,则数据表期望您在服务器上执行分页处理(即,数据表请求将询问数据集的给定页面,并且您的服务器必须准备好返回结果查询给定页面)。如果您不处理大量数据并希望避免自己进行分页,则最好将serverside选项设置为false,数据表将处理它。这样,您只需一次提供所有数据即可。更多信息:https://datatables.net/examples/data_sources/server_side.html

#1


0  

Yes, that's true. If you set the server side option to true, the datatables expect you to do the pagination processing on your server (i.e. the datatables request will ask for a given page of the data set, and your server must be ready to return the results of the query for the given page). If you are not dealing with great amounts of data and wish to avoid making the pagination yourself, you better set the serverside option to false and datatables will take care of it. This way all you need to do is provide all of the data at once. More on it: https://datatables.net/examples/data_sources/server_side.html

是的,这是真的。如果将服务器端选项设置为true,则数据表期望您在服务器上执行分页处理(即,数据表请求将询问数据集的给定页面,并且您的服务器必须准备好返回结果查询给定页面)。如果您不处理大量数据并希望避免自己进行分页,则最好将serverside选项设置为false,数据表将处理它。这样,您只需一次提供所有数据即可。更多信息:https://datatables.net/examples/data_sources/server_side.html