MVC Grid Foreign Key column - Dropdownlist with different value per row depended by data

1 Answer 49 Views
Grid
Gerd
Top achievements
Rank 1
Gerd asked on 26 Jul 2023, 09:02 AM | edited on 26 Jul 2023, 09:02 AM

Hi all,

I need a solution for ASP.NET MVC Grid Razor

i have a Grid InCell Edit Mode.

One Column Date

One Column as ForeignKey Column (Possible Values before Date: 01.07.2023 are [A, B, C] and At 01.07.2023 and later possible Values are [D, E]

Is there a solution how to change the selectlist by data of current row?

 

Regards 

 

Gerd

Gerd
Top achievements
Rank 1
commented on 28 Jul 2023, 08:48 AM | edited

I have a solution for that problem:

add to the foreignkey clumn a client template and a editor template

the client template call the js function:

and for the editor template 

Generate a dropdownlist and add to the grid the edit event

while the edit event:

replace the datasource of dropdownlist

the dropdownlist is named by column binding name.

the edit event has the Model with date column

 var ddl = $("#Foreignkeyname").data("kendoDropDownList");
    if (ddl) {
        var kendoDs;
        const dateToCompare = new Date("2023-06-30");
        if ((dateToCompare - new Date(kendo.format("{0:yyyy-MM-dd}", e.model.Date))) >= 0) {
            kendoDs = new kendo.data.DataSource({
                data: [{ Value: "-1", Text: "Please select" },{ Value: "1", Text: "A" },{ Value: "2", Text: "B" }]
            });
        } else {
            kendoDs = new kendo.data.DataSource({
                data: [{ Value: "-1", Text: "No" }, { Value: "1", Text: "Yes" }]
            });
        }
        ddl.setDataSource(kendoDs);
    }

1 Answer, 1 is accepted

Sort by
0
Accepted
Eyup
Telerik team
answered on 28 Jul 2023, 08:46 AM

Hi Gerd,

 

Thank you for writing to us.

Yes, this requirement is possible to achieve using the following approach:

1. Use the .Events(e=>e.Edit()) handler to capture the moment when the user opens the dropdownlist cell:
https://docs.telerik.com/kendo-ui/api/javascript/ui/grid/events/edit

2. Get the Kendo dropdownlist instance. It will be similar to this:

$(".k-dropdownlist>input").data("kendoDropDownList")
3. Change its datasource depending on the record's DateTime condition using the .setDataSource() method:
https://docs.telerik.com/kendo-ui/api/javascript/ui/dropdownlist/methods/setdatasource

Feel free to give this solution a try and let me know if it helps you.

 

Regards,
Eyup
Progress Telerik

Stay tuned by visiting our public roadmap and feedback portal pages. If you're new to the Telerik family, be sure to check out our getting started resources, as well as the only REPL playground for creating, saving, running, and sharing server-side code.

Tags
Grid
Asked by
Gerd
Top achievements
Rank 1
Answers by
Eyup
Telerik team
Share this question
or