KoolReport's Forum

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

DataTable colvis #2699

Open 50Grosh opened this topic on on May 27, 2022 - 10 comments

50Grosh commented on May 27, 2022

Hello KoolTeam :)

I found cool functionality in DataTables which saves colvis state ( https://datatables.net/extensions/buttons/examples/column_visibility/stateSave.html ) but its not working for me and i try to understand where is the issue but i stuck.

DataTables::create(array(
        "fastRender" => true,
        "dataSource"=>$this->dataStore("test"),
        "plugins" => ["Buttons"],
        "showFooter"=>true,
        "options" => array(
            "paging"=>true,
            "searching" => true,
            "dom" => 'Bfrtip',
            "stateSave"=>"true",
            "buttons" => [
                [
                    "extend" => 'pdfHtml5',
                    "messageTop" => $messageTop,
                    "title" => $companyName,
                    "exportOptions" => array(
                            "columns"=> ":visible",
                        ),
                    "customize"=>"function(doc) {
                        doc.pageMargins = [10, 10, 10,10 ];
                    }",

                ],
                [
                    "extend" => 'excelHtml5',
                    "exportOptions" => ["columns"=> ":visible"],
                    "filename" => $fileName,
                    "title" => $companyName,
                    "sheetName" => $sheetName,
                    "messageTop" => $messageTop

                ],
                [
                    "extend" => 'print',
                    "exportOptions" => ["columns"=> ":visible"],
                    "text" => "print"
                ],
                [
                    "extend" => 'csv',
                    "exportOptions" =>["columns"=> ":visible"],
                    "text" => "CSV"
                ],
                [
                    "extend" => 'colvis',
                    "text" => "colvis"
                ],
            ],

        ),
        "columns" => array(
            "a" => array(
                "type"=>"string",
                "label"=>"a",
            ),
            "b" => array(
                "type"=>"string",
                "label"=>"b",
            ),
            "c" => array(
                "type"=>"string",
                "label"=>"c",
            ),
            "d" => array(
                "type"=>"string",
                "label"=>"d",
                "formatValue"=>function($value){
                    return ($value)=="1"? "1":"0";
                }
            ),
            "e" => array(
                "type"=>"string",
                "label"=>"e",
            ),
            "f" => array(
                "type"=>"string",
                "label"=>"f",
                "formatValue"=>function($value){
                    return '<a target="_blank" href=" ' . WP_CONTENT_URL . '/test/'. $value .'">'. $value .'</a>';
                }
            ),
            "g"=> array(
                "type"=>"number",
                "decimals"=>2,
                "decimalPoint"=>".",
                "thousandSeparator"=>",",
                "label"=>"g",
                "footer"=>"sum",
                "footerText" => "sum : <br> @value",
            ),
            "h" => array(
                "type"=>"string",
                "label"=>"h",
            ),
        ),
        "cssClass"=>array(
            "table"=>"table table-striped table-bordered"
        ),
    ));

Greetings

Sebastian Morales commented on May 30, 2022

Pls try to use true (boolean type) instead of "true" (string type):

        "options" => array(
            ...
            "stateSave"=>true, // instead of "true"

Let us know how it works for you. Tks,

50Grosh commented on May 30, 2022

Sorry, it's my typo. Still not working

Sebastian Morales commented on May 30, 2022

By default, "stateSave" uses localStorage property of browser window object. Pls try the following option so that sessionStorage is used instead:

        "options" => array(
            ...
            "stateSave"=>true,
            "stateDuration" => -1,
50Grosh commented on May 30, 2022

Checked on all my reports and still nothing. After submit or refresh shows all columns.

Sebastian Morales commented on May 30, 2022

Does the online example

https://datatables.net/extensions/buttons/examples/column_visibility/stateSave.html

work on your browser? If it does pls try your DataTables with the simplest setting (like having only "dataSource" and options' stateSave) to see whether it works.

50Grosh commented on May 30, 2022

example works

I changed setting for somthing like this

DataTables::create(array(
       "dataSource"=>$this->dataStore("test"),
        "options" => array(
            "dom" => 'B',
            "stateSave"=> true,
            "stateDuration" => -1,
            "buttons" => ['colvis'],
        )
    ));

and its not working

Sebastian Morales commented on May 30, 2022

May I know your version of Datagrid (which can be found in file koolreport/datagrid/composer.json)?

50Grosh commented on May 30, 2022

7.0.0

Sebastian Morales commented on May 30, 2022

Oh, I've just found the reason for "stateSave" not working. Pls add an unique "name", i.e id, to your DataTables widget like this:

DataTables::create(array(
       "name" => "myUniqueTableName", // add an unique name here
       "dataSource"=>$this->dataStore("test"),
        "options" => array(
            "dom" => 'B',
            "stateSave"=> true,
            "stateDuration" => -1,
            "buttons" => ['colvis'],
        )
    )); 

If you don't, each time you refresh your page a different automatic id is given to the DataTables. Therefore, it can not restore its previous state saved under a different automatic id.

50Grosh commented on May 30, 2022

Yes ! Thanks guys its working :) You are the best :) !!

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
solved
suggestion

Inputs