简体   繁体   English

kendo-ui:在网格中具有三元运算符的自定义模板

[英]kendo-ui: custom template with ternary operator in a grid

I am trying to create a template for grid in kendo-ui. 我正在尝试在kendo-ui中为网格创建模板。 Its formatting is conditional...as follows: 其格式是有条件的...如下:

dataset sample: 数据集样本:

json: [{ "name" :"abc", "link":123 },{ "name" :"def", "link":null}...]

The template should be of the logic:

link === null ? <span>name</span> : <a target="_blank" href="http://mywebsite/name">#=name#</a>

ie conditionally make the text hyperlinked v/s display it as is ( "abc" should be displayed with a hyperlink while "def" should not have a hyperlink). 也就是说,有条件地使文本超链接v / s照原样显示( "abc"应显示为超链接,而"def"不应具有超链接)。

I am able to get the unconditional way of template working with always make the text as hyperlink as follows: 我能够以无条件的方式使用模板,始终使文本成为超链接,如下所示:

var nameTemplate = '<a target="_blank" href="http://mywebsite/#=name#">#=name#</a>';

But cannot get the template with the above ternary operator logic to work 但是无法获得具有上述三元运算符逻辑的模板

Thoughts? 有什么想法吗?

Thanks 谢谢

You can use the function option of the template : 您可以使用模板的功能选项:

columns: [
    "name",
    { 
      field: "link", 
      title: "link", 
      template: function(dataItem) {
        return dataItem.link === null ? "<span>" + dataItem.name + "</span>" : "<a href='http://mywebsite/" + dataItem.name + "'>" + dataItem.name + "</a>"
      }
    },
]

DEMO 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM