KoolReport's Forum

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

DataStore sort order ignored when DataGrid / DataTables columns are defined in the view #427

Open Keith Burke opened this topic on on Aug 22, 2018 - 9 comments

Keith Burke commented on Aug 22, 2018

I've noticed a problem with the DataTables object. If I define a subset of columns in the view, the sort order is ignored and set to ASC in column order.

For example, if I pipe in the following to my query

==

The sorting is ignored if I define columns in my view. Is there a nice way to resolve this?

Even sorting in the SQL and removing the sort pipe makes no difference.

Keith Burke commented on Aug 22, 2018

Sorry, I inserted the wrong [view] image but the scenario is the same. If I remove the defined columns in the view, the page shows ALL columns but the desired sorting is preserved.

Keith Burke commented on Aug 22, 2018

I've noticed that the sort order is honoured for about 1/4 of a second then over ridden somewhere as I see the proper sort order displayed them immediately changed.

KoolReport commented on Aug 22, 2018

DataTables has a client-side sorting mechanism so it could override it, could you please post again your full DataTables code here?

David Winterburn commented on Aug 23, 2018

Hi Keith,

By default, i.e with no ordering option set, DataTables widget orders the first column ascending. If you want to keep your data order in the datastore, please use either of these options:

             DataTables::create(array(
                "dataSource" => $this->dataStore('myData'), 
                'options' => [
                    "order" => [],
                ]
            ));

or:

             DataTables::create(array(
                "dataSource" => $this->dataStore('myData'), 
                'options' => [
                    'ordering' => false
                ]
            ));

Please let us know if this is what you want. Thanks!

Keith Burke commented on Aug 24, 2018

Nice one. 'ordering' => false resolved my issue. However, it also disabled clicking on the headers to sort reverse etc.

"order" => [], won't work for me, I think, as I'm sorting on columns that don't exist in the visible data set [sql sort]. Is it possible to hide columns in the DataGrid but sort by them using "order" => [], method, while maintaining the column sort feature?

KoolReport commented on Aug 27, 2018

You may try to hide the columns like this:

DataTables::create(array(
    "options"=>array(
        "columnDefs"=>array(
            array(
                "targets"=>array(3),
                "visible"=>false,
            )
        )
    )
))

Above example is trying to hide the forth column.

Keith Burke commented on Aug 28, 2018

Fantastic. Just to fix a quick typo for other readers. columnsDefs should be columnDefs. Column singular.

DataTables::create(array(
    "options"=>array(
        "columnDefs"=>array(
            array(
                "targets"=>array(3),
                "visible"=>false,
            )
        )
    )
))
KoolReport commented on Aug 28, 2018

Awesome! Thanks for fixing my typo

tony valentino commented on Dec 17, 2022

Neither option worked for me. It always wants to sort on the first column. Code below:

<?php

require_once "../vendor/autoload.php";

$dname = "pass5b"; $x = "tblpass5bevent"; //"tbl" . $dname . "event"; $y = sprintf('"SELECT * FROM %s"', $x); //echo $x; use \koolreport\KoolReport; use \koolreport\processes\Filter; use \koolreport\processes\TimeBucket; use \koolreport\processes\Group; use \koolreport\processes\Limit; //use \koolreport\clients\Bootstrap;

class TestReport extends \koolreport\KoolReport {

//use \koolreport\clients\Bootstrap;

public function settings()
{
    //Get default connection from config.php
    $config = include "../config.php";

    return array(
        "dataSources"=>array(
            "test_report"=>$config["query_source"]
        )
    );

}   
protected function setup()
{
	
    $this->src('test_report')
//			  "SELECT * FROM tblpass5bevent"
    ->query("SELECT * FROM tblpass5bevent ORDER BY 'localtestdate' DESC")
	
//	->query("SELECT * FROM $x")
//	->query($y)
    ->pipe($this->dataStore('test_by_day'));
} 

 

};

?>

<?php // use \koolreport\widgets\koolphp\Table;

use \koolreport\widgets\google\ColumnChart;
use \koolreport\datagrid\DataTables;
include '../functions.php';

?>

<?=report_template_header('Reports')?>

Pass5B Test Result Details

$this->dataStore('test_by_day'), "options"=>array( "paging"=>true, "pageLength" => 5, "order" => [], "orders"=>array( array(19,"desc")) ) )); ?>
<div>

</div>

<script>

function html_table_to_excel(type)
{
    var data = document.getElementById('testr');

    var file = XLSX.utils.table_to_book(data, {sheet: "sheet1"});

    XLSX.write(file, { bookType: type, bookSST: true, type: 'base64' });

    XLSX.writeFile(file, 'TestReport.' + type);
}

const export_button = document.getElementById('export_button');

export_button.addEventListener('click', () =>  {
    html_table_to_excel('xlsx');
});

</script>

<?=template_footer()?>

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