tigrang / cakephp-datatable

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

Multiple Datatables in the same view and from the same model! #53

Closed salouri closed 9 years ago

salouri commented 10 years ago

I have two data sets (datatables) in the same view, they both fetch data from the same model. Normally I could just have the same settings in the related action and do the filtering on aoColumns on the front side. However, one data set is filtered based on data from another model, while the other isn't.

Any ideas??

salouri commented 10 years ago

Also, I even can't find any documentation on how multiple datatables are supported through the plugin. (regardless if from the same model or not)

fdalex commented 10 years ago

Hey, I have also problems to find how multiple datatables on the same pages and same model should work. My first table is like $this->DataTable->render(null, array("id" => "myTable1"), $jsOptions1); My 2nd table like this : $this->DataTable->render(null, array("id" => "myTable2"), $jsOptions2); However the jsOption2 is applied on both tables ...

salouri commented 10 years ago

fdalex, I dropped the idea of two datatables from the same model because in my case it wasn't important. But I solved the problem of datatables in the same view from two different associated models . Here is how: (below is how you can use the same way to solve your problem) 1) in the controller's action, I sent all fields of both models through the settings array ($this->DataTable->settings) 2) in the datatable/view.ctp: I built all necessary values and built the $this->dtResponse['aaData'][ ] array with them all. Here, you have to pay attention to two things: a) when you build the values from a model, check first if (isset($result['myCurrentModel'])) b) when you build the "aaData' always check if the value was not set and replace it with null e.g. $this->dtResponse['aaData'][] = array( isset($student_ID) ? $student_ID : null, // 0 c) put each model's related fields above each other, and most importantly remember their orders (starting from 0) this is the main trick! because the datatables settings in the view are based on this order 3) I manually did the settings of the two datatables in my view.ctp and in each datatable I hid the fields related to the other datatables (using "aoColumnDefs"). don't forget to add extra empty tags for the number of hidden fields in each table. In one table they will come at the end, and in another they will come before the actual tags.

For two datatables from same model, I think if you just create a model with a different name but uses the same table (public $useTable ="same_table") and use both in your settings array (in the action) and the rest will be just like the above.

If tigrang can find a way to "alias" the models in the settings array then that will solve the whole problem and no need for two models of the same table way.

tigrang commented 9 years ago

@salouri Hey, this is really late I know. But you can try out the dev branch which has what you were looking for. Documentation at: https://gist.github.com/tigrang/755d77500c97ae3e5953 It's still a WIP.

tigrang commented 9 years ago

Latest docs in the wiki https://github.com/tigrang/cakephp-datatable/wiki/ Its for the dev branch.

tigrang commented 9 years ago

This has been merged into master. Check the wiki for docs. Thanks.