NutzCN Logo
问答 关于datatables添加总计行功能
发布于 278天前 作者 Memory 810 次浏览 复制 上一个帖子 下一个帖子
标签: nutzwk
<%
layout("/layouts/platform.html"){
%>
<section class="content-wrap bg-white">
    <header class="header navbar bg-white shadow">
        <div class="btn-group tool-button">
            <a id="add" class="btn btn-primary navbar-btn" href="${base}/platform/rep/busi/actual/add" data-pjax><i class="ti-plus"></i> ${msg['globals.button.add']}</a>
            <a id="set" class="btn btn-info navbar-btn" href="${base}/platform/rep/busi/actual/set" data-pjax><i class="ti-settings"></i> ${msg['globals.button.set']}</a>
            <button class="btn btn-danger navbar-btn" onclick="delCheck()"><i class="ti-close"></i> ${msg['globals.button.delete.more']}</button>
        </div>
        <div class="pull-right offscreen-right">
            <button class="btn btn-primary navbar-btn" onclick="sublime.toggleFilter('.cd-panel')"><i
                    class="fa fa-sliders"></i> 筛选
            </button>
        </div>
    </header>
    <div class=panel-body style="padding-top: 50px;">
        <div class="table-responsive no-border">
            <table class="table table-bordered table-striped mg-t datatable">
                <thead>
                    <tr>
                            <th>${msg['rep.busi.actual.column.bid']}</th>
                            <th>${msg['rep.busi.actual.column.year']}</th>
                            <th>${msg['rep.busi.actual.column.month']}</th>
                            <th>${msg['rep.busi.actual.column.user_count']}</th>
                            <th>${msg['rep.busi.actual.column.bill_count']}</th>
                            <th>${msg['rep.busi.actual.column.out_count']}</th>
                            <th>${msg['rep.busi.actual.column.new_count']}</th>
                            <th>${msg['rep.busi.actual.column.unimp_count']}</th>
                            <th>${msg['rep.busi.actual.column.unsub_count']}</th>
                            <th>${msg['rep.busi.actual.column.net_count']}</th>
                            <th>${msg['rep.busi.actual.column.marketing_cost']}</th>
                            <th>${msg['rep.busi.actual.column.refund_money']}</th>
                            <th>${msg['rep.busi.actual.column.refund_rate']}</th>
                            <th>${msg['rep.busi.actual.column.receivables']}</th>
                            <th>${msg['rep.busi.actual.column.actual_payment']}</th>
                            <th>${msg['rep.busi.actual.column.editStatus']}</th>
                            <th>${msg['rep.busi.actual.column.status']}</th>
                            <th>${msg['globals.table.column.operation']}</th>
                    </tr>
                </thead>
            </table>
        </div>
    </div>
</section>
<div class="cd-panel from-right">
    <header class="cd-panel-header">
        <h4>高级筛选</h4>
        <a href="javascript:;" class="cd-panel-close text-center"><i class="ti-close"></i> </a>
    </header>
    <div class="cd-panel-container">
        <div class="cd-panel-content shadow">
            <div class="form-group">
                <label for="sid" class="control-label">业务</label>
                    <select id="sid" name="sid" class="selectpicker show-tick form-control"
                            data-parsley-required="true" data-live-search="true" data-live-search-style="begins">
                        <% for(o in list){
                        %>
                        <option value="${o.id}">${o.name}</option>
                        <%
                        } %>
                    </select>
            </div>
            <div class="form-group">
                <label for="year" class="control-label">初始化年份</label>
                <div class="input-group date form_datetime "  data-date="" data-date-format="yyyy" data-link-field="year" data-link-format="yyyy">
                    <input type="text" size="16" readonly class="form-control" value="${@date.getYear()}" data-parsley-required="true">
                    <span class="input-group-addon"><span class="glyphicon glyphicon-remove"></span></span>
                    <span class="input-group-addon"><span class="glyphicon glyphicon-th"></span></span>
                </div>
                <input type="hidden" id="year" name="year" value="${@date.getYear()}" />
            </div>
            <button id="searchBtn" type="button" class="btn btn-default">查询</button>
        </div>
    </div>
</div>
<div id="dialogDelete" class="modal fade bs-modal-sm" tabindex="-2" role="dialog" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title">${msg['globals.button.delete']}</h4>
            </div>
            <div class="modal-body">
                <div class="row">
                    <div class="col-xs-12">
                        ${msg['globals.button.delete.notice']}
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">${msg['globals.button.cancel']}</button>
                <button id="okDel" type="button" class="btn btn-primary" data-loading-text="${msg['globals.button.delete.tip']}">${msg['globals.button.confirm']}</button>
            </div>
        </div>
    </div>
</div>
<div id="dialogDetail" class="modal fade bs-modal-sm" tabindex="-3" role="dialog" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
        </div>
    </div>
</div>
<script language="JavaScript">
    var datatable;
    function initDatatable() {
        datatable = $('.datatable').DataTable({
            "dom": '<"toolbar">frtip',
            "searching":false,
            "processing": false,
            "serverSide": true,
            "pageLength": 12,
            "select": true,
            "ordering": true,
            "language": {
                "url": "${base}/assets/plugins/datatables/${lang}.json"
            },
            "preDrawCallback": function () {
                sublime.showLoadingbar($(".main-content"));
            },
            "drawCallback": function () {
                sublime.closeLoadingbar($(".main-content"));
            },
            "ajax": {
                "url": "${base}/platform/rep/busi/actual/data",
                "type": "post",
                "data": function (d) {
                    d.year=$("#year").val();
                    d.bid=$("#sid").val();
                }
            },
            "order": [[0, "desc"],[2,"asc"]],
            "columns": [
                    {"data": "bid", "bSortable": true},
                    {"data": "year", "bSortable": true},
                    {"data": "month", "bSortable": true},
                    {"data": "user_count", "bSortable": true},
                    {"data": "bill_count", "bSortable": true},
                    {"data": "out_count", "bSortable": true},
                    {"data": "new_count", "bSortable": true},
                    {"data": "unimp_count", "bSortable": true},
                    {"data": "unsub_count", "bSortable": true},
                    {"data": "net_count", "bSortable": true},
                    {"data": "marketing_cost", "bSortable": true},
                    {"data": "refund_money", "bSortable": true},
                    {"data": "refund_rate", "bSortable": true},
                    {"data": "receivables", "bSortable": true},
                    {"data": "actual_payment", "bSortable": true},
                    {"data": "editStatus", "bSortable": true},
                    {"data": "status", "bSortable": true},
            ],
            "columnDefs": [
                {
                    "render": function (data, type, row) {
                        return '<div class="btn-group"><button type="button" class="btn btn-default btn-xs dropdown-toggle" data-toggle="dropdown">' +
                                ' <i class="ti-settings"></i> <span class="ti-angle-down"></span></button><ul class="dropdown-menu" role="menu">' +
                                '<li><a href="${base}/platform/rep/busi/actual/detail/' + row.id + '" data-toggle="modal" data-target="#dialogDetail">${msg["globals.button.detail"]}</a></li>' +
                                '<li><a href="${base}/platform/rep/busi/actual/edit/' + row.id + '" data-pjax>${msg["globals.button.update"]}</a></li>' +
                                '<li class="divider"></li>' +
                                '<li><a href="javascript:;" onclick="del(\'' + row.id + '\')">${msg["globals.button.delete"]}</a></li>' +
                                '</ul></div>';
                    },
                    "targets": 17
                },
                {
                    "render": function (data, type, row) {
                        if(null != row.repBusi &&null != row.repBusi.name && "" !=row.repBusi.name)
                            return row.repBusi.name;
                    },
                    "targets": 0
                }
            ]
        });
        var data={
            "bid":"1",
            "year":"1",
            "month":"1",
            "user_count":"1",
            "bill_count":"1",
            "out_count":"1",
            "new_count":"1",
            "unimp_count":"1",
            "unsub_count":"1",
            "net_count":"1",
            "marketing_cost":"1",
            "refund_money":"1",
            "refund_rate":"1",
            "receivables":"1",
            "actual_payment":"1",
            "editStatus":"1",
            "status":"1"
        }

        datatable.row.add(data).draw();
        datatable.on('click', 'tr', function () {
            $(this).toggleClass('selected');
        });
        $("#searchBtn").on('click', function () {
            datatable.ajax.reload();
        });
    }

    $("#searchBtn").on('click', function () {
        datatable.ajax.reload();
    });
    function del(id) {
        var dialog = $("#dialogDelete");
        dialog.modal("show");
        dialog.find("#okDel").unbind("click");
        dialog.find("#okDel").bind("click", function (event) {
            var btn = $(this);
            btn.button("loading");
            $.post("${base}/platform/rep/busi/actual/delete/" + id, {}, function (data) {
                if (data.code == 0) {
                    datatable.ajax.reload(null,false);
                } else {
                    Toast.error(data.msg);
                }
                btn.button("reset");
                dialog.modal("hide");
            }, "json");
        });
    }
    function delCheck() {
        var chks = datatable.rows('.selected').data();
        if (chks.length > 0) {
            var ids = [];
            $.each(datatable.rows('.selected').data(), function (i, n) {
                ids.push(n.id);
            });
            var dialog = $("#dialogDelete");
            dialog.modal("show");
            dialog.find("#okDel").unbind("click");
            dialog.find("#okDel").bind("click", function (event) {
                var btn = $(this);
                btn.button("loading");
                $.post("${base}/platform/rep/busi/actual/delete", {ids: ids.toString()}, function (data) {
                    if (data.code == 0) {
                        datatable.ajax.reload(null,false);
                    } else {
                        Toast.error(data.msg);
                    }
                    btn.button("reset");
                    dialog.modal("hide");
                }, "json");
            });
        } else {
            Toast.warning("${msg['globals.button.delete.select']}");
        }
    }
    $(function () {

        $('.form_datetime').datetimepicker({
            language:  'zh-CN',
            format:'yyyy',
            weekStart: 1,
            todayBtn:  1,
            autoclose: 1,
            todayHighlight: 1,
            startView: 4,
            minView:4,
            maxView:4,
            forceParse: 0,
            showMeridian: 1
        });
        initDatatable();
        $("#dialogDetail").on("hidden.bs.modal", function () {
            $(this).removeData("bs.modal");
        });
    });
</script>
<%}%>
7 回复
 var data={
            "bid":"1",
            "year":"1",
            "month":"1",
            "user_count":"1",
            "bill_count":"1",
            "out_count":"1",
            "new_count":"1",
            "unimp_count":"1",
            "unsub_count":"1",
            "net_count":"1",
            "marketing_cost":"1",
            "refund_money":"1",
            "refund_rate":"1",
            "receivables":"1",
            "actual_payment":"1",
            "editStatus":"1",
            "status":"1"
        }

        datatable.row.add(data).draw();

第一页位完整的代码,这个是我新增行的代码,报错信息为

DataTables warning: table id=DataTables_Table_0 - Requested unknown parameter 'bid' for row 0. For more information about this error, please see http://datatables.net/tn/4

实在搞不懂,求大神解决

是不是忘记加@Ok("json:full")

            'fnDrawCallback': function(table) {
                $(".table tbody").append("<tr role=\"row\" class=\"odd\"><td>1</td><td>2</td><td>3</td><td></td><td></td><td>4</td><td>5</td></tr>")
            },

欢迎打赏给大鲨鱼~~我~~~啊哈哈哈

问题1 fnDrawCallback和drawCallback 两个回调貌似作用一样,对吗? 反正百度没啥结果
问题2 datatable.row.add(data).draw();用法的正确姿势是啥?

不一样,一个是“画”之后,一个是“画”之前。
正确姿势当然去看它的源码,或者google咯,别用百度。

添加回复
请先登陆
回到顶部