2013-10-31 23 views
0

我想在我的頁面上實現一個按鈕,該按鈕可以讀取它所在的行,並將其發送到變量中作爲該行的內容。knockout.js按鈕讀取對象列表中的單行

  • 我的表
  • 描述---- -----因素-----依賴按鈕(< - 只讀這條線)
  • 內容描述---因子1 ---- dependencies1 ----按鈕
  • 內容描述2 ---因子2 ---- ---- dependencies2按鈕

我使用jQuery庫,基因敲除,尤其是我現在用的是SimpleGrid(淘汰賽)創建一個視圖模型,但只是爲了方便(所以它不是強制性的)。 我最初的想法是爲每一行創建一個表單,並使用已經在敲除示例中使用的指令。 例如:http://knockoutjs.com/examples/cartEditor.html。 還有其他建議嗎?

P.S.在我的情況我修改直接SimpleGrid在這一點上添加表單

templateEngine.addTemplate("ko_simpleGrid_grid", "\ 
       <table class=\"ko-grid\" cellspacing=\"0\">\ 
        <thead>\ 
         <tr data-bind=\"foreach: columns\">\ 
          <th data-bind=\"text: headerText\"></th>\ 
         </tr>\ 
        </thead>\ 
        <tbody data-bind=\"foreach: itemsOnCurrentPage\">\ 
         <tr data-bind=\"foreach: $parent.columns\">\ 
          <td data-bind=\"text: typeof rowText == 'function' ? rowText($parent) : $parent[rowText] \"></td>\ 
         </tr>\ 
        </tbody>\ 
       </table>"); 

感謝您的關注

+0

因爲你似乎在循環,以做它,你可以只通過'$ data'的點擊,這將是當前行項目 – Thewads

+0

你打算使用一個動作按鈕,例如data-bind = click:$ data.readLine * (發明) –

回答

0

這裏是(http://jsfiddle.net/bG4TK/2/的如何從一個按鈕訪問線視圖模型上線爲例)。這是你需要的嗎?

HTML:

<table> 
    <tbody> 
     <!--ko foreach:lines --> 
     <tr> 
      <td><input data-bind="value:description" /></td> 
      <td><input data-bind="value:factor" /></td> 
      <td><input data-bind="value:dependencies"/></td> 
      <td><button data-bind="click:sendStuff">send</button></td> 
     </tr> 
     <!--/ko--> 
    </tbody> 
</table> 
<button data-bind="click:addLine">add line</button> 

JS:

var Line = function(description, factor, dependencies){ 
    var self = this; 
    self.description = ko.observable(description); 
    self.factor = ko.observable(factor); 
    self.dependencies = ko.observable(dependencies); 
    self.sendStuff = function(){ 
     alert("I'm sending the line: " + self.description() + " " + self.factor() + " " + self.dependencies()); 
    } 
} 

var VM = function(){ 
    var self = this; 
    self.lines = ko.observableArray([ 
     new Line("descr1", "factor1", "deps1"), 
     new Line("descr2", "factor2", "deps2"), 
     new Line("descr3", "factor3", "deps3"), 
     ]); 
     self.addLine = function(){ 
      self.lines.push(new Line("newDescr", "newFactor", "newDeps")) 
     } 
} 

ko.applyBindings(new VM()); 
+0

太棒了!!!!!謝謝 –

+0

好的,那你需要什麼? – pax162

+0

哦,對不起,我很困惑 –