tigrang / cakephp-datatable

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

Conditions not working #16

Closed jaganreddy closed 11 years ago

jaganreddy commented 11 years ago

conditions in action method not executing. public function index() { $this->paginate['Avatar'] = array( 'conditions' => array('Avatar.id' => '3'), ); }

Results are also fetching when I comment code in index action.

Is it mandatory to pass a model paramer in ajax request? ex: /avatars?model=Avatar

when i remove model parameter the data is not loading

tigrang commented 11 years ago

Put it inside $this->DataTable->settings['Avatar']

jaganreddy commented 11 years ago

Thanks tigrang, but still i am not getting data with conditions.

in my controller

public $components = array('DataTable.DataTable'=> array( 'columns' => array( 'id' => 'Id', 'name' => 'Name', 'inactive' => 'Status', 'created' => 'Created', 'Actions' => null, ) ));

in index action public function index() { $this->DataTable->settings['Avatar']; $this->paginate['Avatar'] = array('conditions' => array('Avatar.id' => 1),); }

in view

bProcessing:true, bServerSide:true, sAjaxSource:"/avatars/?model=Avatar"

If i not added model=Avatar, i am getting exception 'DateTableComponent: Model not specified for request.'

When i look into the DataTableComponent file at line number 130 to 134, it is looks like model parameter in mandatory and the parameter model is passing into process() method. In process() method, gathering information is missing.

I also faced problem to getting data with multiple model with join. Could you provide me simple example

Thanks again

tigrang commented 11 years ago

Yes a model is required in the url. I meant $this->DataTable->settings['Avatar'] = array('conditions' => array('Avatar.id' => 1),);

jaganreddy commented 11 years ago

Thanks a lot tigrang, its working.

Before this change i tried by replacing files component and helper file with older version, it is also working.

Now i changed with new version.

Thanks again.

I have a requirement to handle multiple models with multiple actions with in single controller.

public function index() { $this->DataTable->settings['Avatar']; $this->paginate['Avatar'] = array('conditions' => array('Avatar.id' => 1),); }

public function admin_index() { $this->DataTable->settings['SuperAvatar']; $this->paginate['SuperAvatar'] = array('conditions' => array('SuperAvatar.id' => 1),); }

For the above actions how can I define columns in controller component initialization ie at

public $components = array('DataTable.DataTable'=> array(....

tigrang commented 11 years ago

$this->components = array('DataTable.DataTable' => array('Model1' => array('columns' => ...), 'Model2' => array('columns' => ...)));

jaganreddy commented 11 years ago

Its working great. Thanks a lot for your helping.

Can you please explain what is the $scope parameter for?

I want to get total count with conditions. every time the table is loading the $total is getting all the records from the table. eg(Showing 1 to 1 of 1 entries (filtered from 4 total entries)). Total entries should also fetch depends on the condition.

jaganreddy commented 11 years ago

On declaring as below search is not working $this->components = array('DataTable.DataTable' => array('Model1' => array('columns' => ...), 'Model2' => array('columns' => ...)));

jaganreddy commented 11 years ago

$this->components=array('DataTable.DataTable' => array('Avatar'=>array('columns'=>array('name'=>'Name', created=>'created')),'SuperAvatar'=>array('columns'=>array('superadmin'=>'Name','adminname'=>'admin')......

With the above initialization sorting and searching not working.

tigrang commented 11 years ago

Not working is never helpful. What errors are you getting? Check with firebug or chrome developer tools for the result of the ajax request.

jaganreddy commented 11 years ago

There are no errors with ajax request. Following request and output is generating when i click on column heading for sort

URL http://localhost/privileges/?model=Avatar&sEcho=5&iColumns=5&sColumns=&iDisplayStart=0&iDisplayLength=10&mDataProp_0=0&mDataProp_1=1&mDataProp_2=2&mDataProp_3=3&mDataProp_4=4&sSearch=&bRegex=false&sSearch_0=&bRegex_0=false&bSearchable_0=true&sSearch_1=&bRegex_1=false&bSearchable_1=true&sSearch_2=&bRegex_2=false&bSearchable_2=true&sSearch_3=&bRegex_3=false&bSearchable_3=true&sSearch_4=&bRegex_4=false&bSearchable_4=true&iSortingCols=1&iSortCol_0=2&sSortDir_0=asc&bSortable_0=true&bSortable_1=true&bSortable_2=true&bSortable_3=true&bSortable_4=false&_=1354509818282

Respnse {"iTotalRecords":0,"iTotalDisplayRecords":2,"sEcho":8,"aaData":[["1","<a href=\"\/avatars\/view\/1\">Admin<\/a>","Active","11-28-2012 06:50PM","<\/a>"],["2","<a href=\"\/avatars\/view\/2\">User<\/a>","Active","11-28-2012 06:50PM","<\/a>"]]}

Thanks for your reply

tigrang commented 11 years ago

Do you get an error from the jquery lib? You have only 2 columns configured array('Avatar'=>array('columns'=>array('name'=>'Name', created=>'created')), but in the response I see 5. I think that mismatch can cause an issue? If you aren't getting any errors, then I dont know because you are getting results back. The only other thing I see is that ITotalRecord = 0 but iTotalDisplayRecords is 2. I'll get back to you on that.

jaganreddy commented 11 years ago

I am statically assigning ITotalRecord=0 for other reason. But my main problem is sorting and searching. My configuration in Controller. I am hiding 'id' column in front end. 'DataTable.DataTable'=> array('Avatar'=>array( 'columns' => array('id', 'name', 'inactive', 'created', 'Actions' => null), 'triggerAction' => array('index')), 'Zavatar'=>array( 'columns' => array('id', 'name', 'inactive', 'created', 'Actions' => null), 'triggerAction' => array('admin_index'))

tigrang commented 11 years ago

Um..ok. What exactly is the issue with sorting and searching? You click a header and it doesn't change?

jaganreddy commented 11 years ago

while clicking on header doesn't changing and while searching by entering text in search box also not changing results

tigrang commented 11 years ago

It's working for me. I dont know sorry. You can try putting in debug statements see what's going on. Grab the latest master and try that, too.

jaganreddy commented 11 years ago

ok. I will try with debug and also with latest master. Let you know if I found any information.

Thanks for the help and your time.

jaganreddy commented 11 years ago

It is working when I remove the condition statement in index()

Not working when code as follow public function index() { $this->DataTable->settings['Avatar'] = array('conditions' => array('Avatar.org_id' => $this->Session->read('UserAuth.User.org_id'))); }

It is working when I comment above code.

When I debug in DataTableComponent file _parseSettings() method $settings['columns'] are fetching empty. So, code not executing in foreach($settings['columns'] as $field => $options) {

The main requirement for me is, the default table data of avatars should always fetch with in a organization. So I want to add a mandatory condition for datatable.

Is there any otherway to set default condition.

Thanks

tigrang commented 11 years ago

Whats the output of debug($this->Session->read('UserAuth.User.org_id')) are you sure that's set? Do me a favor and paste your controller on gist or any other code sharing site.

Edit: I am blind - sorry it's late. You are overriding the 'Avatar' key by doing that. Change it to this:

$this->DataTable->settings['Avatar']['conditions'] = array('Avatar.org_id' => $this->Session->read('UserAuth.User.org_id'));

Otherwise you're overwriting the columns and other config values set in $components

Althought I think what you are after is $this->DataTable->settings['Avatar']['scope'] = array('Avatar.org_id' => $this->Session->read('UserAuth.User.org_id'));

Note: scope option may be bugged in the latest master. I'll have a fix for it soon.

jaganreddy commented 11 years ago

Oh, its working great. You made my day, Today is my delivery of my first phase.

Thanks a lot tigrang

tigrang commented 11 years ago

No problem. Thanks for using the plugin :)