This is a migrated thread and some comments may be shown as answers.
Filtering foreign key column values in Grid
2 Answers 82 Views
This is a migrated thread and some comments may be shown as answers.
Phil
Top achievements
Rank 1
Phil asked on 21 May 2018, 09:34 PM

Hi all,

 

I hope I can explain this.  I will try to put together a small sample, but I wanted to get this out there as soon as I could.

I'm using ASP.Net Core 2.0.

 

I have a grid that has a foreign key to an enum value on my model.  I have an EditorTemplate set up, which builds a SelectList from the enum values and binds it to a Kendo DropDownList, and it works great:

@model MyEnumType
 
@{
    var enumTypeSelect = Enum.GetValues(typeof(MyEnumType))
                                 .Cast<MyEnumType>()
                                 .Select(value => new SelectListItem()
                                 {
                                     Text = value.ToString(),
                                     Value = ((int)value).ToString()
                                 });
}
@(Html.Kendo().DropDownListFor(model => model)
                .DataTextField("Text")
                .DataValueField("Value")
                .BindTo(enumTypeSelect))

 

And in my grid:

@(Html.Kendo().Grid<MyViewModel>(Model.Items)
                .Name("grid")
                .Columns(columns =>
                {
                    columns.Bound(li => li.ID).Visible(false);
                    columns.Bound(li => li.EnumType).Width(170)
                                                .ClientTemplate("#: Description #")
                                                .EditorTemplateName("_GetValidEnumItemTypesPartial");
                ... rest of grid...

 

Now, though, I have a requirement where once I select certain enum types, I have to exclude OTHER enum types from subsequent entries in my model.  This is similar to, but not exactly, like a set of cascading drop downs, where the contents of the second depend upon the first.

So, I need to filter those enum types from the list.  I've tried a couple things: 

 

- setting the drop down's dataSource filter, like this:

$("#LineItemType").data().kendoDropDownList.dataSource.filter({ field: 'Value', operator: 'neq', value: 1 })

or

$("#LineItemType").data().kendoDropDownList.dataSource.filter({ field: 'Text', operator: 'neq', value: 'MyValue' })

I've tried this at various places: when I've added the item to the grid, and when I'm about to add the item.  This doesn't seem to work at all.

- Loading the list of valid values via Ajax - the problem there is that I don't know whether I'm updating the FIRST value (so I can set it to anything) or a NEW value (where I would restrict), so I can't reliably determine what the values should be.

Nothing seems to work so far.

 

There's an option that changes my data model - but I would rather not do this if I can avoid it.

Any help is appreciated - thanks!

 

Phil

 

 

 

 

 

2 Answers, 1 is accepted

Sort by
0
Phil
Top achievements
Rank 1
answered on 22 May 2018, 01:28 PM

Looks like I have to set the filter every time the row in the grid is edited.

 

I hooked up the grid's Edit event, and then set the drop down's filter, and things are working fine!

0
Stefan
Telerik team
answered on 24 May 2018, 04:11 AM
Hello, Phil,

I'm glad to hear that the issue is resolved.

I can also suggest checking the following examples as they could provide more insights on this scenario:

https://docs.telerik.com/aspnet-mvc/helpers/grid/how-to/editing/grid-editing-cascading-dropdownlist

https://docs.telerik.com/aspnet-mvc/helpers/grid/how-to/editing/grid-incell-editing-cascading-dropdownlist

I hope this is helpful.

Regards,
Stefan
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
Grid
Asked by
Phil
Top achievements
Rank 1
Answers by
Phil
Top achievements
Rank 1
Stefan
Telerik team
Share this question
or