【问题标题】:jQuery Datatables How to print a table with row grouping?jQuery Datatables 如何打印带有行分组的表格?
【发布时间】:2015-12-17 07:14:03
【问题描述】:

几天我试图实现一个带有行分组的 dataTable 打印视图,但它仍然无法工作.. :(

我有一个数据表,其中隐藏了一列,并且该列是行分组的。与本例相同:https://datatables.net/examples/advanced_init/row_grouping.html

我正在为打印按钮使用按钮扩展 (https://datatables.net/extensions/buttons/)。当我点击打印按钮时,我会看到我的 dataTable 的所有列,甚至应该隐藏一列。然后我使用了一个选项 "columns": ":visible",只打印可见列,但这又不好,因为缺少分组的行。

那么有谁知道如何在打印视图中获得与原始数据表相同的行分组的表?

table = $('#vartTable').DataTable({
            "dom": "Bt",
            "paging": false,
            "fixedHeader": {   
                "headerOffset": -10
            },
            "autoWidth": false,
            "ordering": false,
            "data": data,
            "columns": [
                {"data": "path",},
                {
                    "data": "username",
                    "render": function(data, type, row) {
                        return '<a href="User-view.do?varId=' + row.id + '">' + row.username+ '</a>';
                    }
                },
                {
                    "data": "name",
                    "render": function (data, type, row) {
                        return row.name+ ' ' + row.surname;
                    }
                },
                {"data": "details"}
            ],
            "columnDefs": [{"visible": false, "targets": 0}],

            "buttons": [
                {
                    "extend": 'print',
                    "title": "",
                    "text": 'Print current page',
                    "autoPrint": false,
                    "exportOptions": {
                        "columns": ':visible',
                        "modifier": {
                            "page": 'current'
                        }
                    }
                }
            ],
            "drawCallback": function () {
                var api = this.api();
                var rows = api.rows({page: 'current'}).nodes();
                var last = null;

                api.column(0, {page: 'current'}).data().each(function (group, i) {
                    if (last !== group) {
                        $(rows).eq(i).before('<tr><td colspan="3" class="group">' + group + '</td></tr>');
                        last = group;
                    }
                });
            }
        });

【问题讨论】:

  • 抛出一些你的代码!
  • 我更新了我的问题(添加了代码)
  • 目前这很可能是不可能的,请参阅Print button 参考,打印视图是通过复制不保留某些内容的原始表来创建的。
  • 这个功能好像还没有实现:datatables.net/forums/discussion/32120/…

标签: jquery datatables


【解决方案1】:

Datatable 中不支持导出行探索功能。您必须在 datatables.buttons.js 文件中的 __exportData 函数中添加分组代码,以便在导出之前将分组行添加到数据中

var grouped_array_index=config.grouped_array_index;                     //customised
    var no_of_columns = header.length;
    var no_of_rows = no_of_columns > 0 ? cells.length / no_of_columns : 0;
    var body = new Array( no_of_rows );
        var body_data = new Array( no_of_rows );                                //customised
        var body_with_nodes = new Array( no_of_rows );                          //customised
        var body_with_nodes_static = new Array( no_of_rows );                          //customised
    var cellCounter = 0;

    for ( var i=0, ien=no_of_rows ; i<ien ; i++ ) {
        var rows = new Array( no_of_columns );
                var rows_with_nodes = new Array( no_of_columns );
        for ( var j=0 ; j<no_of_columns ; j++ ) {
            rows[j] = config.format.body( cells[ cellCounter ], i, j, cellNodes[ cellCounter ] );
                        rows_with_nodes[j] = config.format.body( cellNodes[ cellCounter ], i, j, cells[ cellCounter ] ).outerHTML;
            cellCounter++;
        }
        body[i] = rows;
                body_data[i] = rows;
                body_with_nodes[i] = $.parseHTML('<tr class="even">'+rows_with_nodes.join("")+'</tr>');
                body_with_nodes_static[i] = $.parseHTML('<tr class="even">'+rows_with_nodes.join("")+'</tr>');
    }
        //console.log(dt.rows().nodes());
        /******************************************** GROUP DATA *****************************************************/
        var row_array=dt.rows().nodes();
        var row_data_array=dt.rows().data();
        var iColspan =no_of_columns;
        var sLastGroup = "";
        var inner_html='',grouped_index;
        var individual_group_array= new Array(),sub_group_array= new Array(),total_group_array= new Array();
        var no_of_splices=0;  //to keep track of no of element insertion into the array as index changes after splicing one element
        for ( var i=0, row_length=body_with_nodes.length ; i< row_length; i++ ){
            sub_group_array[i]= new Array();
            individual_group_array[i]= new Array();  
            var sGroup='';
            for(var k=0;k<grouped_array_index.length;k++){
                sGroup = sGroup+''+row_data_array[i][grouped_array_index[k]]+' ';
                inner_html=row_data_array[i][grouped_array_index[k]];
                grouped_index=k;
                individual_group_array[i][k]=row_data_array[i][grouped_array_index[k]];
                sub_group_array[i][k]=sGroup;
            }
            total_group_array[i]=sGroup;
            if ( sGroup !== sLastGroup ){
                var table_data=[];
                var table_data_with_node='';

                for(var $column_index=0;$column_index<iColspan;$column_index++){
                    if($column_index===0){
                        table_data_with_node+='<td style="border-left:none;border-right:none">'+inner_html+'</td>';
                        table_data[$column_index]=inner_html+" ";
                    }    
                    else{
                        table_data_with_node+='<td style="border-left:none;border-right:none"></td>';
                        table_data[$column_index]='';
                    }
                }

                body_with_nodes.splice(i+no_of_splices,0,$.parseHTML('<tr class="group group_'+grouped_index+' grouped-array-index_'+grouped_array_index[grouped_index]+'">'+table_data_with_node+'</tr>'));
                body_data.splice(i+no_of_splices,0,table_data);
                no_of_splices++;
                sLastGroup = sGroup;
            }
        }

【讨论】:

  • 你能展示一下演示吗?我尝试了您的代码,但无法打印该组。
【解决方案2】:

这是我的代码,它是 Vinothkumar S

答案的修改版本
var _exportData = function (dt, inOpts) {

    var config = $.extend(true, {}, {
        rows: null,
        columns: '',
        grouped_array_index: [],
        modifier: {
            search: 'applied',
            order: 'applied'
        },
        orthogonal: 'display',
        stripHtml: true,
        stripNewlines: true,
        decodeEntities: true,
        trim: true,
        format: {
            header: function (d) {
                return strip(d);
            },
            footer: function (d) {
                return strip(d);
            },
            body: function (d) {
                return strip(d);
            }
        }

    }, inOpts);

    var strip = function (str) {
        if (typeof str !== 'string') {
            return str;
        }

        // Always remove script tags
        str = str.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, '');

        if (config.stripHtml) {
            str = str.replace(/<[^>]*>/g, '');
        }

        if (config.trim) {
            str = str.replace(/^\s+|\s+$/g, '');
        }

        if (config.stripNewlines) {
            str = str.replace(/\n/g, ' ');
        }

        if (config.decodeEntities) {
            _exportTextarea.innerHTML = str;
            str = _exportTextarea.value;
        }

        return str;
    };


    var header = dt.columns(config.columns).indexes().map(function (idx) {
        var el = dt.column(idx).header();
        return config.format.header(el.innerHTML, idx, el);
    }).toArray();

    var footer = dt.table().footer() ?
        dt.columns(config.columns).indexes().map(function (idx) {
            var el = dt.column(idx).footer();
            return config.format.footer(el ? el.innerHTML : '', idx, el);
        }).toArray() :
        null;

    var rowIndexes = dt.rows(config.rows, config.modifier).indexes().toArray();
    var selectedCells = dt.cells(rowIndexes, config.columns);
    var cells = selectedCells
        .render(config.orthogonal)
        .toArray();
    var cellNodes = selectedCells
        .nodes()
        .toArray();

    var grouped_array_index = config.grouped_array_index;                     //customised
    var no_of_columns = header.length;
    var no_of_rows = no_of_columns > 0 ? cells.length / no_of_columns : 0;
    var body = new Array(no_of_rows);
    var body_data = new Array(no_of_rows);                                //customised
    var body_with_nodes = new Array(no_of_rows);                          //customised
    var body_with_nodes_static = new Array(no_of_rows);                          //customised
    var cellCounter = 0;

    for (var i = 0, ien = no_of_rows; i < ien; i++) {
        var rows = new Array(no_of_columns);
        var rows_with_nodes = new Array(no_of_columns);

        for (var j = 0; j < no_of_columns; j++) {
            rows[j] = config.format.body(cells[cellCounter], i, j, cellNodes[cellCounter]);
            rows_with_nodes[j] = config.format.body(cellNodes[cellCounter], i, j, cells[cellCounter]).outerHTML;
            cellCounter++;
        }

        body[i] = rows;
        body_data[i] = rows;
        body_with_nodes[i] = $.parseHTML('<tr class="even">' + rows_with_nodes.join("") + '</tr>');
        body_with_nodes_static[i] = $.parseHTML('<tr class="even">' + rows_with_nodes.join("") + '</tr>');
    }

    // only attempt group if even defined grouped_array_index
    if (grouped_array_index.length > 0) {

        // also check to see if the column(s) defined in grouped_array_index are present in the data.  we can grab the first row from row_data_array and check
        var testRow = dt.rows().data()[0];
        var testCheck = true;
        for (var k = 0; k < grouped_array_index.length; k++) {
            if (testRow[grouped_array_index[k]] == null) {
                console.log('Column ' + grouped_array_index[k] + ' does not exist in data, aborting grouping...');
                testCheck = false;
                break;
            }
        }
        if (testCheck == true) {

            /******************************************** GROUP DATA *****************************************************/
            var row_array = dt.rows().nodes();
            var row_data_array = dt.rows().data();
            var iColspan = no_of_columns;
            var sLastGroup = "";
            var inner_html = '',
                grouped_index;
            var individual_group_array = [],
                sub_group_array = [],
                total_group_array = [];
            var no_of_splices = 0;  //to keep track of no of element insertion into the array as index changes after splicing one element

            for (var i = 0, row_length = body_with_nodes.length; i < row_length; i++) {
                sub_group_array[i] = [];
                individual_group_array[i] = [];

                var sGroup = '';

                for (var k = 0; k < grouped_array_index.length; k++) {
                    sGroup = row_data_array[i][grouped_array_index[k]];
                    inner_html = row_data_array[i][grouped_array_index[k]];
                    grouped_index = k;
                    individual_group_array[i][k] = row_data_array[i][grouped_array_index[k]];
                    sub_group_array[i][k] = sGroup;
                }

                total_group_array[i] = sGroup;

                if (sGroup !== sLastGroup) {
                    var table_data = [];
                    var table_data_with_node = '';

                    for (var $column_index = 0; $column_index < iColspan; $column_index++) {
                        if ($column_index === 0) {
                            table_data_with_node += '<td style="border-left:none;border-right:none">' + inner_html + '</td>';
                            table_data[$column_index] = inner_html + " ";
                        }
                        else {
                            table_data_with_node += '<td style="border-left:none;border-right:none"></td>';
                            table_data[$column_index] = '';
                        }
                    }

                    body_with_nodes.splice(i + no_of_splices, 0, $.parseHTML('<tr class="group group_' + grouped_index + ' grouped-array-index_' + grouped_array_index[grouped_index] + '">' + table_data_with_node + '</tr>'));
                    body_data.splice(i + no_of_splices, 0, table_data);
                    no_of_splices++;
                    sLastGroup = sGroup;
                }
            }
        }
    }

    return {
        header: header,
        footer: footer,
        body: body_data
    };
};

此代码替换 dataTables.buttons.js 中的 _exportData 函数。要使用,在您为按钮配置的表格中:

    'buttons': [
        {
            extend: 'print',
            exportOptions: {
                grouped_array_index: ['NameOfColumn'] //note the brackets, i think this is so you can group by multiple columns.
            }
        }
    ],

我修改了代码,以便它检查 grouped_array_index 是否存在,并检查您提供的列是否存在于数据中。如果其中任何一个失败,它仍然会不分组打印,否则会分组。

【讨论】:

    猜你喜欢
    • 2017-07-15
    • 1970-01-01
    • 2019-07-05
    • 2013-03-11
    • 2012-07-06
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 2014-12-15
    相关资源
    最近更新 更多