KoolReport's Forum

Official Support Area, Q&As, Discussions, Suggestions and Bug reports.
Forum's Guidelines

Empty Values in Index Column when Exporting #2081

Closed Keith opened this topic on on May 13, 2021 - 6 comments

Keith commented on May 13, 2021

I have a DataTable that creates an empty index column and the populates it with the onReady attribute once the table is loaded.

Relavent code below:

    DataTables::create(
        array(
            "name"      => "reportTable",
            "showFooter" => TRUE,
            "dataSource" => $this->dataStore("result"),
            "columns"   => array(
                "indexColumn" => ["label" => "Row", "formatValue" => function($value, $row) { return ""; }],
                ...
            ),
            "onReady" => "function() {
                    reportTable.on( 'order.dt search.dt', function () {
                        reportTable.column(0, {search:'applied', order:'applied'}).nodes().each( function (cell, i) {
                            cell.innerHTML = i+1;
                        } );
                    } ).draw();
             }",
            "plugins"    => ["Buttons"],
            "options"    => array(
                "columnDefs"=> array(
                    ["type" => "num-fmt", "targets" => [11,12,13,14,15]]
                ),
                "dom" => 'Blfrtip',
                "searching"  => TRUE,
                "paging"     => TRUE,
                "pageLength"  => 50,
                "buttons"    => [
                    array(
                        "extend"=>"csv",
                        "footer"=>"true"
                    ), 
                    array(
                        "extend"=>"excel",
                        "footer"=>"true"
                    ),
                    array(
                        'extend'=>'pdfHtml5',
                        'orientation'=>'landscape',
                        'pageSize'=>'Legal',
                        'text'=>'PDF',
                        'footer' => true
                    ), 
                    "print",
                    "colvis"
                ],
                "scrollX"    => TRUE,
                "fastRender" => TRUE
            ),

My problem is that when I export to pdf/csv/excel etc, my row column is empty I think because I'm only populating it by updating the html.

Is there a way I can populate the Row column with the correct number of current rows when an export is being called?

Thanks

Sebastian Morales commented on May 14, 2021

Since your index column is dynamically built on client side, a normal Excel export on server side could not show it. In this case I suggest you try a client export solution with the Buttons plugin like in this example:

https://www.koolreport.com/examples/reports/datagrid/plugins/ Let us know if it meets your requirement. Tks,

Keith commented on May 14, 2021

I'm already using the Buttons plugin like in that example.

This is how I'm using the Buttons plugin and configuring it, taken from the original code above:

    DataTables::create(
        array(
            "name"      => "reportTable",
            "showFooter" => TRUE,
            "dataSource" => $this->dataStore("result"),
            "columns"   => array(
                               "indexColumn" => ["label" => "Row", "formatValue" => function($value, $row) { return ""; }],
            ),
            "plugins"    => ["Buttons"],
            "dom" => 'Blfrtip',            
            "options"    => array(
                                "buttons"    => [
                    array(
                        "extend"=>"csv",
                        "footer"=>"true"
                    ), 
                    array(
                        "extend"=>"excel",
                        "footer"=>"true"
                    ),
                    array(
                        'extend'=>'pdfHtml5',
                        'orientation'=>'landscape',
                        'pageSize'=>'Legal',
                        'text'=>'PDF',
                        'footer' => true
                    ), 
                    "print",
                    "colvis"
                ],
Sebastian Morales commented on May 17, 2021

When you use the Buttons excel export, does the Index column show in the excel export file?

Sebastian Morales commented on May 17, 2021

We can confirm even the Buttons client export only uses the original value of a column. In this case pls set your Buttons export option like this to get your index column exported:

    DataTables::create(array(
                ...
                "plugins" => ["Buttons"],
                "buttons" => [
                        "copy", 
                        [
                            "extend" => 'excelHtml5',
                            "exportOptions" => [
                                "format" => [
                                    "body" => "function ( data, row, column, node ) {
                                        if (!window.columnIndex) window.columnIndex = 0;
                                        if (column != 0) return data; //assuming that index column is at position 0 (1st column)
                                        else {
                                            window.columnIndex += 1;
                                            return window.columnIndex;
                                        }
                                    }"
                                ]
                            ]
                        ], 
                        "csv", "pdf", "print"
                    ], 

You could make this works for other export type: 'copyHtml5', 'csvHtml5', 'pdfHtml5'.

Keith commented on May 17, 2021

Hi, thank you this works as intended. 1 issue:

When I press the button twice without refreshing the page. The second export will treat the index value as a continuation from the previous exports. So if my first export had 1-13 rows, my second export should be the same, but instead it goes from 14 onward.

Is there a column.length() property I can check so that if the columnIndex is greater than the length/size, I can reset it?

                        'exportOptions' => [
                            "format"=> [
                                "body"=> "function ( data, row, column, node ) {
                                    if (!window.columnIndex) window.columnIndex = 0;
                                    if (column != 0) return data;
                                    else {
                                        //kind of like this 
                                        if(window.columnIndex > window.column.length){        
                                            window.columnIndex = 0;
                                        }                                        
                                        window.columnIndex += 1;
                                        return window.columnIndex;
                                    }
                                }"
                            ]
                        ]
Keith commented on May 17, 2021

Solved. just added a check for row

                                        if(columnIndex > row){
                                            columnIndex = 0
                                        }

Build Your Excellent Data Report

Let KoolReport help you to make great reports. It's free & open-source released under MIT license.

Download KoolReport View demo
help needed

DataGrid