设置记录根据json响应以HTML过滤

时间:2019-01-02 12:13:20

标签: php jquery datatable

我正在使用Yajra/DataTable。我想过滤我的数据表并在装载过滤表后找到total number of rows。我已经做了一些事情,但是它从DataTables返回了所有row_counts。我如何才能做到这一点。请建议我。

这是一个ajax调用响应,其中我将recordsFiltered称为filtered_counts,但如何设置它。请建议我。 ![1]:http://tinypic.com/r/287i4y0/9

 var usersPreviewTable;
    function initializeUsersTable() {

        if(usersPreviewTable) {
            usersPreviewTable.ajax.reload();
            return;
        }

        usersPreviewTable = $('#datatables').DataTable({
            searching: false,
            responsive: true,
            ajax:{
                url:'/users/json',
                type:'GET',
                dataType: 'JSON',
                data: function (d) {
                    var criteria = preparePreviewCriteria();
                    for (var field in criteria) {
                        d[field] = criteria[field];
                    }
                }
            },
            processing: true,
            serverSide: true,
            columns: [
                {
                    data: 'id', orderable: false,
                    render: function (data, type, row, meta) {
                        return meta.row + meta.settings._iDisplayStart + 1
                    }
                },
                {
                    data: 'username', name: 'username',
                    render: function (cellData, type, rowData, meta) {
                        return '<a href="/users/' + rowData.id + '/edit" class="plain-anchor">' + cellData + '</a>'
                    }
                },
                {data: 'phone', name: 'phone'},
                {
                    data: 'created_at', name: 'created_at', render: function (cellData, type, rowData, meta) {
                    return moment(cellData).format('DD MMM YY')
                }
                },
                {
                    data: null, orderable: false,
                    render: function (cellData, type, rowData, meta) {
                        var form = '<form id="deleteForm-' + rowData.id + '" method="POST" action="/users/ ' + rowData.id + '" accept-charset="UTF-8" style="display:inline">' +
                            '<input name="_method" type="hidden" value="DELETE">' +
                            '{{ csrf_field() }}' +
                            '<input type="hidden" name="filter" value="[]"> ' +
                            '<button type="button" class="btn btn-danger btn-round btn-fab btn-fab-mini" title="Delete User" onclick="openConfirmDelete(&quot;Confirm delete?&quot;, ' + rowData.id + ', deleteConfirmationCallback)"><i class="material-icons">delete</i></button> ' +
                            '</form>';

                        return '<a href="/users/' + rowData.id + '/edit" class="btn btn-success btn-round btn-fab btn-fab-mini"' +
                            'title="Edit User"><i class="material-icons">edit</i></a> ' + (rowData.session ? '<button href="#" class="btn btn-primary btn-round btn-fab btn-fab-mini kickOut" title="Kick Out User" id="kickOut-' + rowData.id + '" onclick="kickoutUser(event)"> <i class="material-icons">input</i></a> ' +
                                '</button>' : '')
                        /*+ form*/
                    }
                }
            ],
            language: {
                emptyTable: 'No data to display',
                processing: '<img class=\'loader-img\' src=\'/images/golden_loader.gif\'>'
            },
            initComplete: function() {
                var tableinfo = usersPreviewTable.page.info();
                var total = tableinfo.recordsTotal;
                $('#usersCount').text( 'Filtered Users : ' + total )
            }
        });
    }

    $('#filterUsers').on('click', function (event) {
        initializeUsersTable();
        var tableinfo = usersPreviewTable.page.info();
        var total = tableinfo.recordsFiltered;
        $('#usersCount').text('Filtered Users : ' + total)
        console.log(total);
    });

initComplete中,我的表中有总计的行。但是在$('#filterUsers').on('click', function (event)中,我想获取经过过滤的数据,例如5。但是,作为json响应中的预期结果,我得到了recordsFiltered,因此无法在此方法中获取它。请建议我,我该怎么办?

0 个答案:

没有答案