KoolReport's Forum

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

Datatable open child row data in a table #2059

Closed Chris Sideris opened this topic on on May 6, 2021 - 9 comments

Chris Sideris commented on May 6, 2021

Hi Koolreport,

I have a complicated situation where I have to generate a report using Datatable and then when I click any of the row 1. It needs to be highlighted as selected. 2. The row data along with some hidden columns needs to be displayed as another table within the same page.

My code is below:

    DataTables::create([
                           "dataSource" => $this->dataStore("mytable"),
                           "name"       => "mainTable",
                           "columns"    => [
                               "first_name" => [
                                   "label" => "First Name"
                               ],
                               "last_name" => [
                                   "label" => "Last Name"
                               ],
                               "date_of_birth" => [
                                   "label" => "DOB"
                               ],
                           ],
                           "excludedColumns" => [
                            "address"
                                ],
                           "cssClass"   => [
                               'td' => 'center-align'
                           ],
                           "options" => [
                               "select" => TRUE,
                               "paging"     => TRUE,
                               "searching"  => TRUE,
                               "pageLength" => 10,
                               "dom"        => 'Bfrtip',
                               "buttons"    => [
                                   "copy", "csv", "excel", "pdf", "print", "colvis"
                               ],
                               "responsive" => [
                                "details" => "function(){
                                     $.fn.dataTable.Responsive.display.modal({
                                        header: function ( row ) {
                                            var data = row.data();
                                            return 'Details for '+data[0]+' '+data[1];
                                        }
                                    });
                                }()",
                                "renderer" => "function(){
                                    $.fn.dataTable.Responsive.renderer.tableAll();
                                }()"
                                ]
                           ],
                           "plugins" => ["Buttons", "Responsive"],
                       ]);
KoolReport commented on May 6, 2021

So you do not want to open modal to show data, but display selected data in another table. Is it 1 row table?

Chris Sideris commented on May 6, 2021

Yes, the 2nd table would be 1 row table. I would like to have the data which is being displayed in the 1st table row as well as the hidden columns in the 1st table row to be displayed into 2nd table.

KoolReport commented on May 6, 2021

I suggest that you use the SubReport feature of KoolReport. SubReport can loaded via ajax. It is something like master and detail table.

You will make a table (datagrid) with shorten information. When user click to a row, it will trigger subReport.update("detailReport",{id:userId}). It will make an ajax call to server and fetch the "detailReport" sub report. This detail subreport contains a table (or whatever form) containing detail data of that user.

By this way, you will reduce the use of javascript to create second table (like what you did above) and utilize the power of KoolReport. I think it will be faster way.

What do you think of this solution?

Chris Sideris commented on May 6, 2021

Hi, I tried to do what you advised but I get the following error

subReport is not defined

My Code is

    DataTables::create([
                           "dataSource"   => $this->dataStore("mainTableDataStore"),
                           "name"         => "mainTableId",
                           "columns"      => [
                               "id"    => [
                                   "label" => "Child Id"
                               ],
                               "fname" => [
                                   "label" => "First Name"
                               ],
                               "lname" => [
                                   "label" => "Last Name"
                               ],
                               "dob_x" => [
                                   "label" => "DOB"
                               ],
                           ],
                           "cssClass"     => [
                               'td' => 'center-align'
                           ],
                           "options"      => [
                               "select"     => TRUE,
                               "paging"     => TRUE,
                               "searching"  => TRUE,
                               "pageLength" => 10,
                               "dom"        => 'Bfrtip',
                               "buttons"    => [
                                   "copy", "csv", "excel", "pdf", "print", "colvis"
                               ],
                           ],
                           "plugins"      => ["Buttons", "Responsive"],
                           "clientEvents" => array(
                               "click" => "function(row){
                               console.log(row);
                                subReport.update('subTable',{id:id});                     
                            }",
                           ),
                           "attributes"   => array(
                               "class" => "form-control",
                           )
                       ]);
Chris Sideris commented on May 6, 2021

Hi Koolreport,

Any suggestions?

Sebastian Morales commented on May 7, 2021

Your title said opening row detail in a new tab but your post said opening within the same page. Which is it pls?

Chris Sideris commented on May 9, 2021

Hi Sebastian, it is for within the same page.

Sebastian Morales commented on May 11, 2021

Chris, pls check our SubReport demo at this link and modify it for your report:

https://www.koolreport.com/examples/reports/others/subreport_demo/

Chris Sideris commented on May 19, 2021

This works, Thanks!

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
solved

DataGrid