0

我有以下KendoUI模板绑定到一个可观察的。当我把一个新的项目,以可观察到的阵列,如何申请的kendoNumericTextBox只对新项目的模板?KendoUI模板与可观察与NumericTextbox

如果我申请的类,它在现有的数字文本框的纺纱翻番的奇效。

<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" 
      }); 
}); 

感谢您的帮助!

回答

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,以前的元素得到另一个。

+0

感谢。数据角色工作得很好。 – scottrakes 2013-03-06 01:28:47

+0

使用'data-format =“n0”'(我已经更新了包含它的答案)。 – OnaBai 2013-03-06 07:30:54