码迷,mamicode.com
首页 > Web开发 > 详细

JQuery Datatables 实现对某一列的数据合计汇总

时间:2021-01-01 12:24:11      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:contex   foo   举例   filter   关闭   als   search   class   关于   

JQuery Datatables 实现对某一列的数据合计汇总

有两种实现方式,举例说明

第一种、JS代码如下:

var table;

table = $(‘#example‘).DataTable({
            dom: ‘Bfrtip‘,
            scrollY: true,
            scrollX: true,
            scrollCollapse: true,
            colReorder: true,
            select: true,
            stateSave: true,
            //info: false,//关闭左下角关于行数和选中的提示
            //filter: false,//关闭搜索
            //paging: false,//关闭分页
            pagingType: "full_numbers",
            columns: [
                { title: "Name", data: "Name", className: "center" },
                { title: "Position", data: "Position" },
                { title: "Office", data: "Office" },
                { title: "Age", data: "Age" },
                { title: "Salary", data: "Salary", className: "canEditor" },
                { title: "操作", data: null, defaultContent: "<button type=‘button‘>编辑</button>" }
            ],
            footerCallback: function (row, data, start, end, display) {
                var api = this.api(), data;
                var intVal = function (i) {
                    return typeof i === ‘string‘ ?
                        i.replace(/[\$,]/g, ‘‘) * 1 :
                        typeof i === ‘number‘ ? i : 0;

                };
                //当前页统计
                total = api.column(4).data().reduce(function (a, b) { return intVal(a) + intVal(b); }, 0);
                //全部统计
                pageTotal = api.column(4, { page: ‘current‘ }).data().reduce(function (a, b) { return intVal(a) + intVal(b); }, 0);

                $(api.column(4).footer()).html(
                    ‘当前页:$‘ + pageTotal + ‘ ( 全部:$‘ + total + ‘)‘
                );
            },
            language: {
                url: dtsLanguage
            }
        });

HTML代码如下:

<table id="example" class="table table-bordered table-condensed" style="white-space: nowrap;width:100%;cursor:pointer" cellspacing="0">
    <tfoot>
        <tr>
            <th style="border-left:0;border-right:0"></th>
            <th style="border-left:0;border-right:0"></th>
            <th style="border-left:0;border-right:0"></th>
            <th style="border-left:0;border-right:0"></th>
            <th style="border-left:0;border-right:0">数据合计:</th>
            <th style="border-left:0;border-right:0"></th>
        </tr>
    </tfoot>
</table>

 

第二种方式:

var table;

table = $(‘#example‘).DataTable({
            dom: ‘Bfrtip‘,
            scrollY: true,
            scrollX: true,
            scrollCollapse: true,
            colReorder: true,
            select: true,
            stateSave: true,
            //info: false,//关闭左下角关于行数和选中的提示
            //filter: false,//关闭搜索
            //paging: false,//关闭分页
            pagingType: "full_numbers",
            columns: [
                { title: "Name", data: "Name", className: "center" },
                { title: "Position", data: "Position" },
                { title: "Office", data: "Office" },
                { title: "Age", data: "Age" },
                { title: "Salary", data: "Salary", className: "canEditor" },
                { title: "操作", data: null, defaultContent: "<button type=‘button‘>编辑</button>" }
            ],
            footerCallback: function (row, data, start, end, display) {
                var data = table.search();
                var ds = table.search(data).context[0].aiDisplay;
                var Sum = 0;

                $.each(ds, function (i, e) {
                    var data = table.row(e).data();
                    Sum = Number(Sum) + Number(data.Salary);
                });
                $(".Sum").html(Sum);
            }
            language: {
                url: dtsLanguage
            }
        });

HTML代码如下:

<table id="example" class="display table table-striped table-over table-bordered" style="white-space: nowrap;width:100%;cursor:pointer" cellspacing="0">
              <thead></thead>
              <tbody></tbody>
              <tfoot>
                    <tr bgcolor="#FFCC99">
                    <th style="border-left:0;border-right:0"></th>
                    <th style="border-left:0;border-right:0"></th>
                    <th style="border-left:0;border-right:0">合计:</th>
                    <th style="border-left:0;border-right:0" class="Sum"></th>
                    <th style="border-left:0;border-right:0"></th>
                    </tr>
               </tfoot>
</table>

 

JQuery Datatables 实现对某一列的数据合计汇总

标签:contex   foo   举例   filter   关闭   als   search   class   关于   

原文地址:https://www.cnblogs.com/dennyw/p/14199952.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!