【问题标题】:KendoUI Template with Observable with NumericTextbox带有 NumericTextbox 的 Observable 的 KendoUI 模板
【发布时间】:2023-03-29 08:58:01
【问题描述】:

我将以下 KendoUI 模板绑定到一个 observable。当我将新项目推送到可观察数组时,如何将 kendoNumericTextBox 仅应用于模板中的新项目?

如果我按班级申请,它会产生一种奇怪的效果,即现有数字文本框上的微调器加倍。

<div id="slots">
        <table class="table table-striped table-condensed" style="width:auto;">
            <thead>
                <tr>
                    <th>Date</th>
                    <th>Time</th>
                    <th>Volunteers Needed</th>
                    <th>
                        Reservation Passcode <i class="icon-question-sign" title ="Only people with the reservation passcode can signup."></i>
                    </th>
                </tr>
            </thead>
            <tbody data-template="row-template" data-bind="source: slots">
            </tbody>
        </table>

    </div>



 $(document).ready(function () {
          var viewModel = kendo.observable({
                slots: [{DateText:'1/8/1969', ShiftLabel: "3:00 to 5:00",Slots:2,ReservationCode:"ABC" }]
              });
          kendo.bind($("#slots"), viewModel);
          $(".numeric").kendoNumericTextBox({
              format: "n0"
          });

          viewModel.slots.push({DateText:'1/8/1969', ShiftLabel: "3:00 to 5:00",Slots:2,ReservationCode:"ABC" });

          $(".numeric").kendoNumericTextBox({
              format: "n0"
          });  
 });

感谢您的帮助!

【问题讨论】:

    标签: kendo-ui kendo-template kendonumerictextbox kendo-observable


    【解决方案1】:

    尝试将您的模板定义为:

    <script type="text/x-kendo-tmpl" id="row-template">
        <tr>
            <td>#= DateText #</td>
            <td>#= ShiftLabel #</td>
            <td class="numeric"><input data-role="numerictextbox" data-format="n0" data-bind="value: Slots"></td>
            <td>#= ReservationCode #</td>
        </tr>
    </script>
    

    并删除 $(".numeric").kendoNumericTextBox(...) 初始化。执行此操作时,您应该在每次运行模板时都有一个NumericTextBox(每行一个)。

    你的 JavaScript 是这样的:

    $(document).ready(function () {
        var viewModel = kendo.observable({
            slots: [
                {DateText: '1/8/1969', ShiftLabel: "3:00 to 5:00", Slots: 2, ReservationCode: "ABC" }
            ]
        });
        kendo.bind($("#slots"), viewModel);
    
        viewModel.slots.push({DateText: '1/8/1969', ShiftLabel: "3:00 to 5:00", Slots: 3, ReservationCode: "ABC" });
    });
    

    在这里运行http://jsfiddle.net/OnaBai/BV48W/

    原因: 您使用 CSS 类 (.numeric) 的事实导致您最终在另一个内部拥有一个 KendoUI 数字文本框。

    示例:您有以下 HTML:

    <label>Number 1: <input id="number1" class="numeric"/></label>
    <label>Number 2: <input id="number2" class="numeric"/></label>
    

    还有 JavaScript

    $(document).ready(function () {
        $(".numeric").kendoNumericTextBox({
            format: "n0"
        });
        $(".numeric").kendoNumericTextBox({
            format: "n0"
        });
    });
    

    你会看到你所谓的现有数字文本框上的微调器加倍的奇怪效果

    每次使用.numeric 选择器调用kendoNumericTextBox 时,都会向元素添加一个额外的微调器。如果它没有微调器(刚刚添加到viewModel 的数据)它会得到一个但是如果你添加数据并使用.numeric 选择器调用kendoNumericTextBox,则前一个元素会得到另一个。

    【讨论】:

    • 使用data-format="n0"(我已经更新了包含它的答案)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多