NullRefExcep / yii2-datatables

Yii2 Widget for DataTables jQuery plug-in
http://www.datatables.net/
MIT License
72 stars 46 forks source link

send parameter #59

Closed hasbi001 closed 4 years ago

hasbi001 commented 4 years ago

Hallo Friend, thank you for your extension. i have a custom filter for datatable but i don't know to send parameter in your action. so how i can send my parameter in ajax server side datatable? please help me..

dmytrokarpovych commented 4 years ago

Hello, I'm not sure that we have solution out of box, but I'll try to looking for it during next few days

dmytrokarpovych commented 4 years ago

did you try to use applyFilter inside nullref\datatable\DataTableAction?

hasbi001 commented 4 years ago

@dmytrokarpovych thanks for the response but I'm not sure if I try to applyFilter function because I will apply it on another page and another filter. so how i do that?

thank you

hasbi001 commented 4 years ago

hallo @dmytrokarpovych, can you give me example for use applayFilter and applyOrder ? thank you

ZAYEC77 commented 4 years ago

could you describe your page? what do you have on it except datatable?

hasbi001 commented 4 years ago

Hi @ZAYEC77, thank you for your response. so on the page, there is a search form and table then in the search form there are several fields based on the columns in the table then when pressing the search button the table will update according to the fields that are filled

ZAYEC77 commented 4 years ago

Hey, sorry for the delay Please, check this commit:

https://github.com/ZAYEC77/cat-with-eav/commit/8874b1b270f1db2369eed64b8a684c1292acc4b1

I have added a small example for you

hasbi001 commented 4 years ago

Hi @ZAYEC77 , thank you for helping me I will try and follow from the example that you have made

hasbi001 commented 4 years ago

Hallo @ZAYEC77, I am sorry before. previously I followed the steps below, as explained in the documentation

public function actions() { return [ 'datatables' => [ 'class' => 'nullref\datatable\DataTableAction', 'query' => Model::find(), 'applyOrder' => function($query, $columns, $order) { //custom ordering logic $orderBy = []; foreach ($order as $orderItem) { $orderBy[$columns[$orderItem['column']]['data']] = $orderItem['dir'] == 'asc' ? SORT_ASC : SORT_DESC; } return $query->orderBy($orderBy); }, 'applyFilter' => function($query, $columns, $search) { //custom search logic $modelClass = $query->modelClass; $schema = $modelClass::getTableSchema()->columns; foreach ($columns as $column) { if ($column['searchable'] == 'true' && array_key_exists($column['data'], $schema) !== false) { $value = empty($search['value']) ? $column['search']['value'] : $search['value']; $query->orFilterWhere(['like', $column['data'], $value]); } } return $query; }, ], ]; }

so, What should I do?

ZAYEC77 commented 4 years ago

My example works based on SearchModel. If you have a form on your page and use a model for it -- you could use this implementation. If you want to add some special filtering logic, you could use applyFilter function at DataTableAction. Default implementation of applyFilter works with filters from DataTables plugin.

ZAYEC77 commented 4 years ago

🤷‍♂️

hasbi001 commented 3 years ago

Hallo @ZAYEC77 thank you for response.

[Solve] I custom my filter function to apply in applyFilter function and add parameter in ajax data.

thank you