tigrang / cakephp-datatable

JQuery DataTable plugin server-side processing component for CakePHP
47 stars 29 forks source link

sort by in server side before send to json #47

Closed oktoleong closed 10 years ago

oktoleong commented 10 years ago

I have looked through other similar issues but couldn't solve my problem.

I need to sort or order by mymodel.id DESC before processing to json format. This mymodel.id is not displayed in view

I open js debug tool and have a looked at my json data. it is not in order

tigrang commented 10 years ago

Have you tried passing it in the settings?

public $components = array(
    'DataTable.DataTable' => array(
        'YourModel' => array('order' => ['id' => 'desc']),
        ....
    )
);
oktoleong commented 10 years ago

Thanks tigrang I found a way to work around by removing / commenting out ID


public $components = array('Session','RequestHandler',
        'DataTable.DataTable' => array(

            'columns' => array(
                //'id' => false,                  
                ...
                ),
            ),
    );

By removing ID column it will make the records retrieved from DB sorted by ID = ASC (default mysql).

then add this line to datatable


$(document).ready(function() {
    $('#example').dataTable( {
        "order": [[ 3, "desc" ]]
    } );
} );

Thanks for your help tigrang. Your plugin rocks :)

tigrang commented 10 years ago

Thanks. Glad you got it working :)