Kendo grid get selected row foreign key column values

1 Answer 691 Views
Grid
Faz
Top achievements
Rank 1
Iron
Faz asked on 23 Dec 2021, 06:02 AM

I have a grid with foreign key columns

columns.ForeignKey(c => c.LanguageID, (System.Collections.IEnumerable)ViewData["languages"], "Id", "Description").Title("Language").Width(200);

I want to get the text value of the foreign key drop down value ie: English in this case for selected rows.

I can Iterate selected rows and get the dataItem
       grid.select().each(function () {

            var dataItem = grid.dataItem($(this));
            console.log(dataItem);
        
        });
which gets me the Id 

 

is there an easy way to read the description?

Thanks

 

1 Answer, 1 is accepted

Sort by
0
Tsvetomir
Telerik team
answered on 27 Dec 2021, 07:47 AM

Hi Faz,

The foreign key columns contain a values property that holds all of the key-value pairs. Therefore, you should loop through the values collection and get the text corresponding to the id that you have gathered from the data item:

var values = $("#grid").getKendoGrid().columns[1].values; // or the col number on your side.
// loop through the values and compare the "value" property to the value from the data item.
You could dynamically get the column by looping through the columns and searching for the field that matches your property. Currently, a hard-coded value is used.

I hope you find this helpful.

 

Kind regards,
Tsvetomir
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Faz
Top achievements
Rank 1
Iron
commented on 31 Dec 2021, 08:59 AM

Hi Tsvetomir,

Both options require to get the Id and then iterate and find the description. I thought there would be a simpler way to get what displayed on the screen as it is.

Tsvetomir
Telerik team
commented on 04 Jan 2022, 12:40 PM

Hi Faz, your assumption is correct. However, after the template is evaluated and the text representation of the foreign key is shown, the actual text is not stored in a variable or a session. Therefore, any consequent accessing of the text should happen by looping through the values collection.
Tags
Grid
Asked by
Faz
Top achievements
Rank 1
Iron
Answers by
Tsvetomir
Telerik team
Share this question
or