This is a migrated thread and some comments may be shown as answers.

Use template on FK field

5 Answers 93 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Robert
Top achievements
Rank 1
Robert asked on 26 Sep 2014, 04:32 PM
So I've figured out from the documentation how to format a cell based on a condition.

I've also learned how to display a foreign key lookup in a cell. How do I combine the two?

So in this example:     
​columns: [ {
    field: "name",
    template: "# if (name.substr(0, 4) == 'Jane') { #" +
     "<span style='color: lightgray'> #: name # </span>" +
      "# } else { #" +
         "#: name #" +
      "# } #"
  }],


How would I test on the model's value that I want to test on, but still display the foreign key value?

The foreign key field is defined thus:
field: "MarketngEngnrID",
values: theMarketers, // a JSON array containing key-value pairs to show the text rather than the value


But I don't know how to combine. I need to show people who are no longer active employees in a different color.

Thanks, Bob

5 Answers, 1 is accepted

Sort by
0
Robert
Top achievements
Rank 1
answered on 26 Sep 2014, 04:36 PM
I mean of course: still display the foreign key text

in the line between the two code examples. (Is there no way to edit your posts?)
0
Accepted
Nikolay Rusev
Telerik team
answered on 30 Sep 2014, 10:55 AM
Hello Robert,

There are different ways to achieve this. One could be to handle dataBound event, iterate over the items and format the cell content. Other way is implement custom template, which however as a bit trickier due to the nature of the foreign key columns. Here is one way how this can be achieved: http://dojo.telerik.com/@rusev/UNahi.

Regards,
Nikolay Rusev
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Robert
Top achievements
Rank 1
answered on 30 Sep 2014, 03:48 PM
Thanks Nikolay, I used the foreignKeyTemplate, it gives me the control I need.

Bob
0
Robert
Top achievements
Rank 1
answered on 30 Sep 2014, 04:06 PM
Just a small follow-up question Nikolay:

In your example, how does the foreignKeyTemplate function get the value of the variable "data"? Is it passed silently in the context of the function call?

I realize this is a broader JavaScript question but any light you could shed would help my personal "knowledge base" as well as that of other readers.

Thanks again, Bob
0
Accepted
Nikolay Rusev
Telerik team
answered on 01 Oct 2014, 10:34 AM
Hello Bob,

The inner function accept data arguments. This is the data item for row. It is the same as having inline template
{
 field: "CategoryID",
 template: "#=data.CategoryID#"
}

The above template is compiled to a function that seems something like the following at the end: function (data) { return data.CategoryID; }​. 

Hope this explains it.

Regards,
Nikolay Rusev
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
Tags
Grid
Asked by
Robert
Top achievements
Rank 1
Answers by
Robert
Top achievements
Rank 1
Nikolay Rusev
Telerik team
Share this question
or