We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Search on foreign key value

We have a grid that uses local array data source that is list of orders:

#grid [dataSource]='listOfOrders' [toolbar]='toolbarOptions' searchSettings="" (load)="onLoad()">


It also uses one foreign key column (and respective additional array data source that is list of users):

field='createdBy' foreignKeyField='id' foreignKeyValue='name' [dataSource]='listOfUsers'>


Based upon similar custom search implementations we tried to implement custom search.

Search should look for match (user by name) and get list of filtered users.

Then it should look for match (orders) or where user is one from users' list.

onLoad(): void {
// Keyup event bound to the input element
this.grid.element.addEventListener('keyup', (args: KeyboardEvent) => {
if ((args.target as HTMLElement).getAttribute('id') === 'toolSearch' && args.key === "Enter") {
let gridObj: Grid = this.grid;
let input: HTMLInputElement = (args.target as HTMLInputElement);
let filtUserIds = [];

// First filter users
let userPredicate = new Predicate('name', 'contains', input.value);
new DataManager({ json: this.listOfUsers }).executeQuery(new Query().select('íd').where(userPredicate)).then((e: ReturnOption) => {
filtUserIds = e.result as object[];
});

// Then filter orders by title or by user id
var orderPredicate = new Predicate('title', "contains", input.value).or('createdBy', 'in', filtUserIds);

new DataManager({ json: (gridObj.dataSource as object[]) }).executeQuery(new Query().where(orderPredicate)).then((e: ReturnOption) => {
// The returned result is assigned to the grid datasource
gridObj.dataSource = e.result as object[];
});


The problem is predicate does not support filtering by presence in list. There is no option for

.or('createdBy', 'in', filtUserIds);


It could be that we are looking in wrong direction here, but searching by foreign key value seems to be very common use case.


Are there are any valid alternatives to our approach please?



1 Reply

DM Dineshnarasimman Muthu Syncfusion Team April 20, 2023 04:12 PM UTC

Hi Muamer,


Thank you for reaching out to Syncfusion Support.


We have reviewed your query regarding the search in a foreign key column by using predicate. To better assist you, we kindly request you to provide us with additional information. Could you please provide us with the following details:


  • A sample of the grid rendering code.

  • A sample of the foreign key column data and grid data.

  • The version of the Syncfusion package you are currently using.


With these details, we will be able to provide you with an accurate solution.


Best regards,

Dineshnarasimman


Loader.
Live Chat Icon For mobile
Up arrow icon