Author Topic: Custom filter  (Read 5631 times)

queensgambit9

  • Pro Ultimate
  • Sr. Member
  • *
  • Posts: 341
    • View Profile
Custom filter
« on: May 25, 2020, 07:37:43 pm »
I would like to apply a global filter and have that setting saved along with state data.
Would that be possible?

Ex. User selects 'a' from dropdown which applies
Code: [Select]
WHERE b = 1 to the select from DB.

Using PHP.

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6310
    • View Profile
Re: Custom filter
« Reply #1 on: May 26, 2020, 10:11:19 am »
Any custom data can be appended to state data as below:

Code: [Select]
var state = grid.saveState({ stringify: false });
state.b = 1; //custom data.
state = JSON.stringify( state );

queensgambit9

  • Pro Ultimate
  • Sr. Member
  • *
  • Posts: 341
    • View Profile
Re: Custom filter
« Reply #2 on: May 26, 2020, 01:23:17 pm »
Ok, thanks.
How can I add the condition to the filterQuery request to DB in a effective way (PHP)?

if ( isset($_POST["custom_param"]))
{
    $pq_custom_params = $_POST["custom_param"];
...
}

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6310
    • View Profile
Re: Custom filter
« Reply #3 on: May 26, 2020, 08:08:28 pm »
it can be appended to the $filterQuery returned by FilterHelper class.

Conceptually ( pseudo code ):

$fiterQuery = $fiterQuery . " AND " . $custom_field_name . " = ". $custom_field_value

queensgambit9

  • Pro Ultimate
  • Sr. Member
  • *
  • Posts: 341
    • View Profile
Re: Custom filter
« Reply #4 on: May 27, 2020, 03:10:38 pm »
Thank you.

Trying to add the custom value to save state but can't get it to work. Made a JSFiddle:

https://jsfiddle.net/queensgambit9/n2035zLo/13/

Would like the custom filter to be saved along with state.   

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6310
    • View Profile
Re: Custom filter
« Reply #5 on: May 27, 2020, 09:20:15 pm »
I don't see any attempt to add custom value to save state in your jsfiddle. Can you please clarify and highlight the relevant code.
« Last Edit: May 27, 2020, 09:32:38 pm by paramvir »

queensgambit9

  • Pro Ultimate
  • Sr. Member
  • *
  • Posts: 341
    • View Profile
Re: Custom filter
« Reply #6 on: May 28, 2020, 02:31:51 pm »
Sorry. I pick up the key value and add it to state, but not sure how to proceed:

Code: [Select]
var $toolbar = this.toolbar()
key = $toolbar.find(".filter100").attr('class')
value = $toolbar.find(".filter100").val()

state.key = value

Modified jsfiddle:
https://jsfiddle.net/queensgambit9/n2035zLo/17/
« Last Edit: May 28, 2020, 02:34:25 pm by queensgambit9 »

queensgambit9

  • Pro Ultimate
  • Sr. Member
  • *
  • Posts: 341
    • View Profile
Re: Custom filter
« Reply #7 on: May 28, 2020, 03:05:08 pm »
Got it working by:

Code: [Select]
var state = this.saveState({ stringify: false });

var $toolbar = this.toolbar()
key = $toolbar.find(".filter100").attr('class')
value = $toolbar.find(".filter100").val()

state.key = value
state = JSON.stringify( state )

Thanks.

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6310
    • View Profile
Re: Custom filter
« Reply #8 on: May 28, 2020, 03:56:36 pm »
Good to know that you got it working.

Just a small correction that you might need while key value assignment.

Code: [Select]
state[ key ] = value

queensgambit9

  • Pro Ultimate
  • Sr. Member
  • *
  • Posts: 341
    • View Profile
Re: Custom filter
« Reply #9 on: May 28, 2020, 05:50:17 pm »
Thanks.

Loading issue:
The custom values does not seem to be sent when state is loaded even though they are set in state...?
How do I send the custom values to php file?

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6310
    • View Profile
Re: Custom filter
« Reply #10 on: May 28, 2020, 06:55:18 pm »
I presume that you are saving grid state remotely in db which is done with an $.ajax call.

So any custom data which is added to the state would also be sent automatically along with main state data.

queensgambit9

  • Pro Ultimate
  • Sr. Member
  • *
  • Posts: 341
    • View Profile
Re: Custom filter
« Reply #11 on: May 28, 2020, 07:43:53 pm »
No, it is saved in localStorage. The state contain the custom data but it ain't sent to php on:

this.loadState({ state: state })

...do I need to modify postData somehow?
Other grid state info works fine.

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6310
    • View Profile
Re: Custom filter
« Reply #12 on: May 28, 2020, 08:17:03 pm »
Quote
No, it is saved in localStorage.

Then your mentioned requirement does not make sense. Why do you want to combine  custom data with grid state?
« Last Edit: May 28, 2020, 08:33:27 pm by paramvir »

queensgambit9

  • Pro Ultimate
  • Sr. Member
  • *
  • Posts: 341
    • View Profile
Re: Custom filter
« Reply #13 on: May 29, 2020, 01:00:34 pm »
Hmm...not sure I understand what you mean. I'll try clarify a bit what I would like to have:

In addition to the standard filters, I would like to have a global filter possibility, ex:

User select some column filters and choose to apply global filter from custom dropdown value 'only team 1 in country b'. In PHP file I check this value and generate SQL for it (added to filterQuery).
That setting should be saved with the state so that the user do not have to select it manually again when loading their state and want to perform same search.

Does it make sense or is there a better way of this?


paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6310
    • View Profile
Re: Custom filter
« Reply #14 on: May 29, 2020, 02:02:32 pm »
Thanks for the clarification.

1. Entire grid state is not saved in local storage when stringify: false is passed. It has to be saved manually with

Code: [Select]
localStorage.setItem( name, state);


2. Please use dataModel.postData callback to send custom data from grid to remote script.