淘汰赛Js模板不显示数据

时间:2018-09-02 07:57:22

标签: javascript html knockout.js

我有以下剔除js代码,由于某种原因,booking.text没有显示,请参阅第1列中没有文本。

谁能建议我做错了什么以及如何解决?

您可以在此处查看JS Fiddle中正在运行的代码。 http://jsfiddle.net/w3fxtdq8/18/

enter image description here

以下是我正在使用的代码

var myViewModel = {
  bookings: ko.observableArray([{
      text: 'booking 1',
      bookingId: 123
    },
    {
      text: 'booking 2',
      bookingId: 123
    },
    {
      text: 'booking 3',
      bookingId: 123
    }
  ])
};
ko.applyBindings(myViewModel);
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<table>
  <thead>
    <th>Column 1</th>
    <th>Column 2</th>
  </thead>
  <tbody data-bind="template: {name: 'booking-table-row-template', foreach: 'bookings', as: 'booking'}">

  </tbody>
</table>

<script type="text/html" id="booking-table-row-template">
  <tr>
    <td data-bind="text: booking.text"></td>
    <td>
      <A href="#">Remove</a>
    </td>
  </tr>
</script>

1 个答案:

答案 0 :(得分:2)

您不应在变量名前后加上引号。线索在于呈现了多少行-8(与单词“ bookings”中的字母数相同)而不是3(数组的大小)。

您将bookings用作字符串。

var myViewModel = {
  bookings: ko.observableArray([{
      text: 'booking 1',
      bookingId: 123
    },
    {
      text: 'booking 2',
      bookingId: 123
    },
    {
      text: 'booking 3',
      bookingId: 123
    }
  ])
};
ko.applyBindings(myViewModel);
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<table>
  <thead>
    <th>Column 1</th>
    <th>Column 2</th>
  </thead>
  <tbody data-bind="template: {name: 'booking-table-row-template', foreach: bookings, as: 'booking'}">

  </tbody>
</table>

<script type="text/html" id="booking-table-row-template">
  <tr>
    <td data-bind="text: booking.text"></td>
    <td>
      <A href="#">Remove</a>
    </td>
  </tr>
</script>