【问题标题】:knockoutjs: How to observe static html table?knockoutjs:如何观察静态 html 表?
【发布时间】:2017-03-03 11:36:32
【问题描述】:

我有一个包含现有行的 html 表。我想将所有行映射为可观察对象的可观察数组,所以如果我更改行的输入值,我的可观察数组上的对象就知道了。

这可能吗?

【问题讨论】:

  • 这听起来有点像您试图以错误的方式使用敲除...通常,您从结构化数据开始并使用敲除将数据渲染一个 HTML 表格。你能告诉我们(一个例子)你想要映射的数据/html吗?
  • 我知道这不是对库的正确使用,但在同样的情况下,我有这个表是从服务器生成的,包含所有的 html,在其他一些情况下,是使用 javascript 添加按钮动态构建的.

标签: knockout.js knockout-2.0 knockout-3.0


【解决方案1】:

我同意用户 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>

【讨论】:

  • 谢谢。此代码:` $('#baseTable tbody tr').map(function(i, row) { myViewModel.tableRows.push(new tableRow( row.cells[0].textContent, row.cells[1].textContent, row.cells[2].textContent)); });` 帮助我了解如何解决问题。
猜你喜欢
  • 2015-09-18
  • 1970-01-01
  • 2016-05-04
  • 2012-11-02
  • 2012-04-20
  • 1970-01-01
  • 2017-03-01
  • 2014-07-06
  • 2021-01-26
相关资源
最近更新 更多