如何在Kendo UI中将动态输入字段设置为数字文本框?

时间:2015-09-07 11:13:12

标签: kendo-ui telerik kendo-mvvm kendonumerictextbox

这是我的情况。

我正在使用此article制作动态表单。

在这里你可以看到它(文章演示)使用kendo template

  <script id="fieldsTemplate" type="text/x-kendo-template">
        <li>
             <label data-bind="attr: { for: name}, text: label"></label>
             <input data-bind="attr: { type: type, name: name, class: css}" # if (get("required")) {# required #} # />
       </li>
  </script>

生成表单后,此表单只需使用HTML5制作表单。它没有kendo属性。例如,如果我绑定data-role属性且值为numerictextbox它没有给我一个数字文本框(想想它的类型是数字)。它没有那些属性。(如果我输入一个数字,它不会显示默认小数点。它只显示该数字。)

但在this示例中,如果我们将data-role属性和值添加为numerictextbox,则它将是一个数字文本框。

但在documentationthis中,似乎我必须调用kendoNumericTextBox方法来制作数字文本框。

即使我尝试将此代码添加到模板中,但它也不起作用(请假设我使用this正确添加此代码。)

      $("#mytextboxid").kendoNumericTextBox();​

那么我还有什么选择? 非常感谢你

1 个答案:

答案 0 :(得分:0)

您必须将data-role属性设置为input元素,以将元素转换为kendo控件/元素。请尝试使用以下代码段。

<body>
    <div id="example">
        <!-- container UL to host input fields -->
        <ul data-template="fieldsTemplate" data-bind="source: fields"></ul>
    </div>


    <!-- Kendo Template binds properties from model to input fields -->
    <script id="fieldsTemplate" type="text/x-kendo-template">
        <li>
            <label data-bind="attr: { for: name}, text: label"></label>
            <input name=#= name #  class=#= css # # if (get("required")) {# required #} # 
              # if (type == 'number') {# data-role="numerictextbox" #}else{# type=#=type#  #}#  />
        </li> 
    </script>

    <script type="text/javascript">
        // retrieve the model from an API call
        var model = {
            "fields": [{
                "css": "cssClass1",
                "label": "Field 1",
                "name": "Field1",
                "required": false,
                "type": "text"
            },
            {
                "css": "cssClass2",
                "label": "Field 2",
                "name": "Field2",
                "required": true,
                "type": "number"
            },
            {
                "css": "cssClass2",
                "label": "Checkbox Field",
                "name": "CheckField",
                "required": false,
                "type": "checkbox"
            },
            {
                "css": "cssClass2",
                "label": "Email Address",
                "name": "Email",
                "required": true,
                "type": "email"
            },
            {
                "css": "cssClass2",
                "label": "Password",
                "name": "Password",
                "required": true,
                "type": "password"
            }
            ]
        };
        // convert the JSON to observable object
        var viewModel = kendo.observable(model);
        // bind the model to the container
        kendo.bind($("#example"), viewModel);

    </script>
</body>

JSFiddle

如果有任何疑虑,请告诉我。

相关问题