tigrang / cakephp-datatable

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

Range filters #66

Open fr0z3nfyr opened 9 years ago

fr0z3nfyr commented 9 years ago

How can I get range filters to work? I have two inputs for a column to do a range search, see image for sample. when I check in console, i get sSearch_0, sSearch_1, sSearch_2, sSearch_3 in config.

One, why is sSearch3 required if there is no search on the last column. I think I have to check DT also to see if that is a mandate. Two, I'm using YADCF for column filtering, so for the range filter, the searched range returns 0 rows. Probably, the query to DB is being passed incorrectly. How can I fix this? I'm supposing that the range filters for dates will also misbehave, I haven't tested. sample

tigrang commented 9 years ago

Hey @fr0z3nfyr, sorry I've been busy. I'll take a look either in the next an hour or so, if not, then tomorrow.

tigrang commented 9 years ago

Can you paste your component config and the js settings you're initializing jquery datatables with?

For range filter, you'll need to use a custom search method on that column. It looks like sSearch0 would be in a format like "fromvalue-tovalue" so the custom find would need to explode on that separator to get the two values.

Custom find works like this:

public $components = [
    'DataTable.DataTable' => [
        'User' => [
            'columns' => [
                'id' => ['bSearchable'  => 'rangeFilter'], 
                'username', 
                'email'
              ],
        ],
];

In your model add a function like:

public function rangeFilter($column, $searchTerm, $columnSearchTerm, $config) {
    // you can then check $columnSearchTerm and add conditions on `$config->conditions`
}

But yadcf states that it supports only server-side processing (1.10.0 +) which my plugin doesn't handle. I don't know how you have any of this working.

fr0z3nfyr commented 9 years ago

Hey sorry, I was sick with a fever, just checked your comment. I will post my component settings as soon as I get back on the project. Meanwhile you can check my interaction with author of yadcf @ http://stackoverflow.com/questions/31045543/ I too was a little confused originally but somehow (probably by fluke) got it working this far. I will also post an implementation for Daniel(yadcf author) on his thread(see comments on answer on SO link).

destinydriven commented 8 years ago

I would like to implement range filters similar to what is used here http://www.alpha.mobotrix.com/cake/users but not necessarily using YADCF.

tigrang commented 8 years ago

@destinydriven the backend is setup for this but you need to manually add the column filters and call the filter function with jquery plugin. See https://legacy.datatables.net/examples/api/multi_filter.html

The important bit is calling

$("tfoot input").keyup( function () {
    /* Filter on the column (the index) of this element */
    oTable.fnFilter( this.value, $("tfoot input").index(this) );
} );

where otable is a reference to the datatables object for your specific table.

destinydriven commented 8 years ago

@tigrang So I need to override render() in the Helper so I can inject the footer into the table?

tigrang commented 8 years ago

@destinydriven check this gist out for an exmaple. https://gist.github.com/tigrang/e6b9d53a407280f791f3abceea285394

destinydriven commented 8 years ago

@tigrang Awesome thanks, that worked great for me. Perhaps we can add this to the wiki. I'm sure it's a much requested feature.

tigrang commented 8 years ago

@destinydriven is containable actually making a join or is it doing a separate query? Can you check sql log?

destinydriven commented 8 years ago

@tigrang It's doing a left join.

tigrang commented 8 years ago

@destinydriven can you hop on irc?