简体   繁体   English

如何从Kendo UI模板获取价值

[英]How do i get a value from Kendo UI template

I have this template: 我有这个模板:

var template = kendo.template("<div class='relatedItemRow'>"
                            + "<span id='relatedItemName'>Related Item #: Number #</span>"
                            + "<div  class='relatedItemRowInfo'><span >#: Name #</span>"
                            + "<a data-relatedItemID='#: Value #' class='removeRelatedItem'>"
                            + "<img src= '#: Img #'/</a></div><br class='clear'/></div>");
var data = {
    Name: "" + checkbox.getAttribute('flatName'),
    Number: $('#relatedItemsList').children().length + 1,
    Img: '/Content/images/x_remove.png',
    Value: checkbox.value
};

var result = template(data);
$("#relatedItemsList").append(result);

I am able to access the data-relatedItemID by using: 我可以使用以下方式访问与data-relatedItemID:

$('#relatedItemsList').children().eq(i).children().last().attr('data-relatedItemID')

But how do I get to the Number field in data? 但是如何到达数据中的Number字段? I want to change that one dynamically. 我想动态地更改那个。 I have tried: 我努力了:

$('#relatedItemsList').children().eq(i).children().attr('Number') == 5

but it does not work. 但它不起作用。 Any idea how to do that? 任何想法如何做到这一点?

I know that there is an answer for this question even accepted but I'd like to suggest a different approach that I think it is much simpler and more Kendo UI oriented and is using Kendo UI ObservableObject . 我知道这个问题甚至有一个答案,但我想提出一种不同的方法,我认为它更简单,更面向Kendo UI,并且正在使用Kendo UI ObservableObject This allows you to update an HTML that is bound to the ObservableObject without having to crawl the HTML. 这使您可以更新绑定到ObservableObject的HTML,而不必对HTML进行爬网。

This approach is as follow: 该方法如下:

  1. Wrap your data definition with a Kendo Observable Array initialization. 用Kendo Observable Array初始化包装 data定义。
  2. Redefine your template and start using using data-binding . 重新定义您的模板并开始使用data-binding
  3. Append this new template to your HTML. 将此新模板附加到HTML。
  4. Bind data to the HTML. data绑定到HTML。

Now you can get current value using data.get("Number") or set a new value using data.set("Number", 5) and the HTML get magically updated. 现在,您可以使用data.get("Number")获取当前值,或使用data.set("Number", 5)设置新值data.set("Number", 5)然后HTML会得到神奇的更新。

The code is: 代码是:

Template definition 模板定义

<script type="text/kendo-template" id="template">
    <div class='relatedItemRow'>
        <span id='relatedItemName'>Related Item <span data-bind="text : Number"></span></span>

        <div class='relatedItemRowInfo'>
            <span data-bind="html : Name"></span>
            <a class='removeRelatedItem' data-bind="attr: { data-relatedItemID : Value }">
                <img data-bind="attr : { src : Img }"/>
            </a>
        </div>
        <br class='clear'/>
    </div>
</script>

data definition as: data定义为:

var data = new kendo.data.ObservableObject({
    Name: "" + checkbox.getAttribute('flatName'),
    Number: $('#relatedItemsList').children().length + 1,
    Img: '/Content/images/x_remove.png',
    Value: checkbox.value
});

and the initialization of the HTML is: HTML的初始化为:

$("#relatedItemsList").append($("#template").html());

Getting current value of Number is: 获取Number当前值为:

var old = data.get("Number");

Setting is: 设置为:

data.set("Number", 5);

Running example in JSFiddle here : http://jsfiddle.net/OnaBai/76GWW/ 在此处的JSFiddle中运行示例: http : //jsfiddle.net/OnaBai/76GWW/

The variable "result" and thus the data you're trying to change aren't a Kendo templates, they're just html created by the template, and the number is just text in the html. 变量“结果”以及因此要更改的数据不是Kendo模板,它们只是由模板创建的html,而数字只是html中的文本。 To modify the number without rebuilding the whole string, you need to change the template so you can select the number by itself with jQuery by putting it in it's own element, and adding something to identify it. 要在不重新构建整个字符串的情况下修改数字,您需要更改模板,以便您可以使用jQuery自行选择数字,方法是将其放入自己的元素中,并添加一些内容进行标识。

var template = kendo.template("<div class='relatedItemRow'><span id='relatedItemName'>Related Item <span class='relatedNumberValue'>#: Number #</span></span><div  class='relatedItemRowInfo'><span >#: Name #</span><a data-relatedItemID='#: Value #' class='removeRelatedItem'><img src= '#: Img #'/</a></div><br class='clear'/></div>");
//other code the same

And once you can select it, then you can change it like this. 一旦选择它,就可以像这样更改它。

$('#relatedItemsList').children().eq(i).find('.relatedNumberValue').text(5);

And retrieve it like this. 并像这样检索它。

var foo = $('#relatedItemsList').children().eq(i).find('.relatedNumberValue').text();

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

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