Login Register

grid query filter via variable?

Hi everybody,

i try to filter my table with a string but i don't find a way to do this.
what can i do to get this working?
var filter='{ id="10" }';
newModel.query = filter;

i hope someone can bring me on the right way.

darkey

You may need to provide a

You may need to provide a fuller example of your source code here, but, one thing to change:

var filter='{ id="10" }';

probably should be:

var filter='{ id:"10" }';

or perhaps this, if your id is a number rather than a string representation of a number:

var filter='{ id:10 }';

Hey there,i have tryed many

Hey there,

i have tryed many opportunities but nothing worked. Everytime i get a "dojo.data.ItemFileReadStore: Invalid item argument." error.
Only if i write the json string directly behind the query it let me pass but i need to dynamicly edit the query.

newModel.query = { id:"10" }; // let me pass

var filter='{ id:"10" }';
newModel.query = filter; // got an error

Before i try this i set the string staticly but the wildcard * doesn't show me empty fields.
I buid the filter variable from inputs an want to filter the grid by every onchange.

Bye,
darkey

I haven't tried what you are

I haven't tried what you are trying to do, so it may take some more experimentation (as you are doing). Yes, it probably does require a true javascript object, so you may want to try doing an eval of your query string (modified) and see if that works, e.g.,

var filter=eval('{ id:"10" }');

Hello frankf, I tried your

Hello frankf,

I tried your code but it fail, too. i have now no idea what i can do to bring it to work.
I hope you still have some ideas.

Hopeful,

darkey

Yep, not a quick way to see

Yep, not a quick way to see how to make that work, that way.

I think you will need to do the documented method. See the Filtering section here.

I believe you will also need to call grid.refresh() after setting a new model.

And you should be able to use the eval in the above method (hmm, needs more study--doesn't work).

I use the code from the link

I use the code from the link you gave me.
The score of eval on the console.log is only the number.
With the grid.refresh() i see no difference.

OK. I think this will

OK. I think this will work.

Use the technique in the previous link, about creating a new model.

When you build your custom query, do it this way:

var filter = {};
filter.id = 'new value';
newModel.query = filter;

Then the rest, e.g., grid.setModel(newModel)

Hey frankf, thank you really

Hey frankf,

thank you really much. This works for me.
I hope this could help someone else.

darkey

Try this

In order to convert a string to an object use this:

var filterString='{id: "10"}';
filterObject=eval('('+filterString+')');

If you want to use this object in your model just do this:

newModel.query = filterObject;

try function buildQuery()

try

function buildQuery() {
    return { field1: dijit.byId("sometextbox").getValue() };
}

and have button to work with your grid

<button id="filter" onclick="grid.filter(buildQuery(),true)">Filter</button>

re: try function buildQuery()

Where is this function found?
grid.filter(buildQuery(),true)