tigrang / cakephp-datatable

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

search and Paginate filter #11

Closed alessandro-riz closed 12 years ago

alessandro-riz commented 12 years ago

When you set a pagination condition like this one in the controller

$this->paginate['Post'] = array(
  'conditions' => array('OR' => array(...)),
);

If you use the dataTable search you lost your condition, I think the problem is the file Controller / Component / DataTableComponent.php in the function _search() at line 289 from

if (!empty($conditions)) {
  $this->query['conditions']['OR'] = $conditions;
}
tigrang commented 12 years ago

This was fixed in the component branch. As soon as I get some free time I'm going to make that master, so you should use that branch for now. Just note that $this->paginate should be changed to $this->DataTable->settings['Post']

The component branch also supports multiple datatables per page now, its not well documented, so if you need help open an issue and we'll get it sorted out.

alessandro-riz commented 12 years ago

Thanks for reply, but when I try to use the component branch I've this error on the ajax response:

Notice (8): Undefined index: model [APP/Plugin/DataTable/Controller/Component/DataTableComponent.php, line 131]

If I try to use the helper $this->DataTable->render(); I get this javascript error

Uncaught ReferenceError: dataTableSettings is not defined

Is there something to change, I've add in the controller the line $this->DataTable->paginate = array('Post'); I've to change some setting to make it works?

tigrang commented 12 years ago

Yea I'm sorry about - its the lack of documentation. Add <?php echo $this->fetch('dataTableSettings'); ?> to your layout before your jquery datatable init script, or before fetch('script') if you are going to be using my helper and the default init script. You can disable the default init script by settings scriptBlock => false in the public $helpers.

tigrang commented 12 years ago

Oh if you don't want to use my helper, make sure your sAjaxSource for that table includes a GET param for the model that table uses. Ex. /controller/action.json?model=Article The component needs to know which model to paginate.

alessandro-riz commented 12 years ago

Thanks very much with fetch instruction works perfectly... Can I ask you one more think about the render helper? How can I set aaSorting or bSortable setting? I try some code like this but without result

echo $this->DataTable->render(
            'Post',
            array(),
            array(
                'bJQueryUI' => true,
                'sAjaxSource' => $this->base . '/index?model=Post',
                'aaSorting' => array(
                    array( '1' => 'asc' )
                    )
            );

Thanks for your supprt...

tigrang commented 12 years ago

You would set bSortable in the component. This is so the component knows server-side which columns are sortable and people can't edit the datatable client-side to modify its behavior. That part is covered in the readme. As for aaSorting, that looks correct. Can you take a look at the page source and look for var dataTableSettings and paste that back here - or check and see if its being set?

tigrang commented 12 years ago

I see the error. Change your array to look like:

'aaSorting' => array(
    array( 1,  'asc' ) // instead of array( '1' => 'asc' )
)
alessandro-riz commented 12 years ago

I try with this code

echo $this->DataTable->render(
    'Post',
    array(),
    array(
        'bJQueryUI' => true,
        'oLanguage' => array(
            'sUrl' => $this->webroot . 'files/dataTables.italian.txt'
            ),
        'sAjaxSource' => $this->base . '/index?model=Post',
        'aaSorting' => array(
            array( 1 => 'asc' ),
            ),
        )
    );

I paste the var dataTableSettings

Post: Object
  aaSorting: Array[1]
    0: Object
      1: "asc"
  bJQueryUI: true
  oLanguage: Object
    sUrl: "/files/dataTables.italian.txt"
  sAjaxSource: "/posts/index?model=Post"

And the chrome error is this one Uncaught TypeError: Cannot read property 'asSorting' of undefined jquery.dataTables.min.js:150 (jquery.datTatables.min.js is working fine ;-)) same thing with aoColumns and other... I also try to set like this 'aaSorting' => '[[1, "asc"]]' var dataTableSettings is aaSorting: "[[1, "asc"]]" and the chrome error is always the same

Thaks Alessandro

tigrang commented 12 years ago

You're still doing it wrong - its NOT 'aaSorting' => '[[1, "asc"]]' that creates a string, not an array - you have to do'aaSorting' => array(array( 1, 'asc' ))

Its not an associative array, its two items per array, first being columns index, second being direction.

alessandro-riz commented 12 years ago

By setting aoColumns all work correctly... Thank you very much for your help. When I've some time, and I've work a little bit with your plugin I try to write a small guide on the features of your great plugin

Thaks Alessandro

2012/6/25 Tigran Gabrielyan < reply@reply.github.com

It doesn't look like you setup your columns in the controller? I don't see aoColumns in dataTableSettings. Can you paste your controller. Be sure to setup 'columns' key in the components arrary, look at the readme for an example.


Reply to this email directly or view it on GitHub: https://github.com/tigrang/cakephp-datatable/issues/11#issuecomment-6555060

alessandro@poppix.it http://www.poppix.it/

tigrang commented 12 years ago

Glad you got it working :)