Lookup filtering

Hi, I’m filtering a lookup field called DocTypeLookup, using the following code, based on a string:

// filter controls for the document type
function filterLookup(v){		       
    // setting filtration
    fd.field('DocTypeLookup').filter = "Category eq '" + v + "'";
    **fd.field('DocTypeLookup').widget.dataSource.read();**
	}
//filter lookup when form opens
filterLookup('Feasibility');

I seem to get an error on the .widget.dataSource.read(), however this I think is new behaviour, as I don’t think I previously got this error - ‘TypeError: Cannot read property ‘dataSource’ of null’ There is definitely a category called Feasibility on the lookup list, and the lookup field works in the form, filtering down the options.

Regardless of whether I leave this line in, or comment the line out, the filter works on the drop down. Any idea why this is happening?

Hi Abolam,

You need to use the on ready for the lookup, the chances are the function is running before the lookup is fully loaded.

Try This

// filter controls for the document type
function filterLookup(){		       
    // setting filtration
    var v = 'Feasibility'
    fd.field('DocTypeLookup').filter = "Category eq '" + v + "'";
    fd.field('DocTypeLookup').widget.dataSource.read();
}
//filter lookup when form opens
fd.field('DocTypeLookup').ready().then(filterLookup);

Good Luck.

Wow, such a quick response. And the resolution works a treat. Thanks.

For my noob knowledge :grinning:, is the widget.dataSource.read() simply telling the lookup control to read it’s (new) filtered data?

Just puzzled as I don’t understand why it still works when you don’t run that line of code. But thank you anyway.

I’m a user just like you so can’t really answer that question, just figured I’d help out when I came across a question I knew the answer to.

Maybe an Admin can come in and answer that one for you…

Thanks

Dear @abolam, @Tony_Duke,

The official Kendo documentation will say better than me :smile: https://docs.telerik.com/kendo-ui/api/javascript/data/datasource/methods/read

Thank you @Tony_Duke, you guys are so cool!

Thank you Tony and Alex, I’ll have a look at that and see if I can get my head around Kendo UI.