0
A
回答
0
我同意用戶3297291.但是你可以使用html表來加載observable數組。運行下面的代碼。或者您也可以通過填寫表格打擊添加和可觀察到的陣列這裏玩它https://jsfiddle.net/7zop2n9j/12/您可以添加新行到表更新
function tableRow(material, quantity, unitPrice) {
var self = this;
this.material = ko.observable(material);
this.quantity = ko.observable(quantity);
this.unitPrice = ko.observable(unitPrice);
}
function model() {
var self = this;
this.material = ko.observable('');
this.quantity = ko.observable('');
this.unitPrice = ko.observable('');
this.tableRows = ko.observableArray('');
this.addRow = function() {
myViewModel.tableRows.push(new tableRow(
self.material(),
self.quantity(),
self.unitPrice()));
}
}
var myViewModel = new model();
$(document).ready(function() {
ko.applyBindings(myViewModel);
$('#baseTable tbody tr').map(function(i, row) {
myViewModel.tableRows.push(new tableRow(
row.cells[0].textContent,
row.cells[1].textContent,
row.cells[2].textContent));
});
});
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<link href="https://code.getmdl.io/1.3.0/material.indigo-pink.min.css" rel="stylesheet" />
<script src="https://code.getmdl.io/1.3.0/material.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<table id="baseTable" style="display: none;">
<thead>
<tr>
<th>Material</th>
<th>Quantity</th>
<th>Unit price</th>
</tr>
</thead>
<tbody>
<tr>
<td>Acrylic (Transparent)</td>
<td>25</td>
<td>2.90</td>
</tr>
<tr>
<td>Plywood (Birch)</td>
<td>50</td>
<td>1.25</td>
</tr>
<tr>
<td>Laminate (Gold on Blue)</td>
<td>50</td>
<td>1.25</td>
</tr>
</tbody>
</table>
<table class="mdl-data-table mdl-js-data-table mdl-shadow--2dp">
<thead>
<tr>
<th class="mdl-data-table__cell--non-numeric">Material</th>
<th>Quantity</th>
<th>Unit price</th>
</tr>
</thead>
<tbody data-bind="foreach: tableRows">
<tr>
<td class="mdl-data-table__cell--non-numeric" data-bind="text: material"></td>
<td data-bind="text: quantity"></td>
<td data-bind="text: unitPrice"></td>
</tr>
</tbody>
</table>
<p>
<div class="mdl-textfield mdl-js-textfield">
<input class="mdl-textfield__input" type="text" id="material" data-bind="value: material">
<label class="mdl-textfield__label" for="material">Material...</label>
</div>
</p>
<p>
<div class="mdl-textfield mdl-js-textfield">
<input class="mdl-textfield__input" type="text" id="quantity" data-bind="value: quantity">
<label class="mdl-textfield__label" for="quantity">Quantity...</label>
</div>
</p>
<p>
<div class="mdl-textfield mdl-js-textfield">
<input class="mdl-textfield__input" type="text" id="unitPrice" data-bind="value: unitPrice">
<label class="mdl-textfield__label" for="unitPrice">unitPrice...</label>
</div>
</p>
<button class="mdl-button mdl-js-button mdl-button--fab mdl-button--colored" data-bind="click:addRow">
<i class="material-icons">add</i>
</button>
+0
謝謝。此代碼: '$( '#baseTable TBODY TR')映射(功能(I,行){ myViewModel.tableRows.push(新的TableRow( row.cells [0] .textContent, row.cells [ 1] .textContent, row.cells [2] .textContent)); });' 幫我理解如何解決問題。 – sintetico82
相關問題
- 1. 靜態行knockout.js觀察的陣列表
- 2. KnockoutJS如何觀察對象屬性?
- 3. KnockoutJS:如何觀察到的陣列
- 4. KnockoutJS調用可觀察性
- 5. Knockoutjs可觀察綁定
- 6. 如何在knockoutjs的html文件中使用動態可觀察變量名稱
- 7. KnockoutJS觀察對象的可觀察對象
- 8. KnockoutJS可觀察對象的可觀察對象
- 9. knockoutjs可觀察對象的可觀察數組綁定
- 10. knockoutjs - 可觀察到的可觀察值的對象的observableArray
- 11. 實施了「靜態觀察者模式」
- 12. 如何在靜態主要使用非靜態方法(觀察者方法)
- 13. KnockoutJS區分可觀察和計算
- 14. knockoutjs,可觀察更新彼此
- 15. Knockoutjs,取消可觀察更新
- 16. KnockoutJS - 可觀察對象doeasn't工作
- 17. 後期觀察到陣列中KnockoutJS
- 18. knockoutJS JSON沒有轉換爲可觀察
- 19. 更新ObservableArray中的可觀察值knockoutjs
- 20. 週期性觀察到與knockoutjs
- 21. KnockoutJs - 可觀察綁定和範圍
- 22. 嵌套可觀察到的在KnockoutJS
- 23. KnockoutJS:訂閱內觀察到observableArray進入
- 24. 如何觀察靜態對象的變化?
- 25. 如何觀察mac OS中靜態庫的內容?
- 26. 排序KnockoutJS觀察到的數組元素動態
- 27. 使用動態名稱創建KnockoutJS可觀察項目
- 28. 嵌套表與KnockoutJS可觀察數組(父&子表)
- 29. 如何更新knockoutjs中的可觀察數組元素?
- 30. 如何明確驗證knockoutJs可觀察到
這聽起來有點像你試圖用錯誤的方式使用淘汰賽......通常,您會從結構化數據開始,並使用淘汰賽將數據呈現在HTML表格中。你能向我們展示一個你想要映射的數據/ html的例子嗎? – user3297291
我明白這不是該庫的正確使用,但在同樣的情況下,我有這個表是從服務器與所有的HTML生成,並在其他情況下是建立動態與JavaScript添加按鈕。 – sintetico82