简体   繁体   English

如何通过使用Knockout.js在数组中绑定子数组?

[英]How to bind child array in array by using Knockout.js?

I'm using Knockout to bind data to a table: 我正在使用Knockout将数据绑定到表:

I have obj : 我有obj

obj=[{id:"1",productName:"laptop",tag:[promotion,blackfriday]},{id:"2",productName:"Samsung galaxy note III",tag:[samsung,galaxy]}]

HTML: HTML:

<table data-bind="foreach:list" id="listProduct">
  <tr>
      <td data-bind="text:productName"></td>
      <td data-bind="foreach:obj.tag">
          <p data-bind="text:tag"></p>
      </td>
  </tr>
</table>

JavaScript (to bind data): JavaScript (绑定数据):

ko.applyBindings({ list: obj }, document.getElementById('listProduct'));

But it only displays the product name and does not bind "tag" obj; 但是它仅显示产品名称,并且不绑定“标签” obj。 I want Knockout to bind data as: 我希望淘汰赛将数据绑定为:

    Product Name                        Tag
1   Laptop                              promotion
                                        blackfriday

2   Samsung galaxy note III             samsung
                                        galaxy

based on the code you gave, there are a couple simple things that will fix this for you: 根据您提供的代码,有一些简单的方法可以为您解决此问题:

  1. productName is misspelled productName拼写错误
  2. the foreach:obj.tag should be foreach:tag foreach:obj.tag应该是foreach:tag
  3. the text:tag should be text:$data (see the docs on this ) text:tag应该是text:$data (请参阅此文档
  4. your tags in the data given are not strings - so it won't work if they aren't defined elsewhere 您给定数据中的标记不是字符串-因此,如果未在其他位置定义它们,则将无法使用

JS: JS:

var obj = [{
    id: "1",
    productName: "laptop",
    tag: ["promotion", "blackfriday"]
}, {
    id: "2",
    productName: "Samsung galaxy note III",
    tag: ["samsung", "galaxy"]
}];

ko.applyBindings({ list: obj }, document.getElementById('listProduct'));

HTML: HTML:

<table  id="listProduct">
  <tr data-bind="foreach:list">
    <td data-bind="text:productName">test</td>
    <td data-bind="foreach:tag">
      <p data-bind="text:$data">test</p>
    </td>
  </tr>
</table>
  1. The implementation that are you using is too odd, and I think is not correct. 您使用的实现太奇怪了,我认为这是不正确的。 For example I don't know why are you retrieving the listProduct element and sending as argument to ko.applyBidning , this is not how knockout works. 例如,我不知道您为什么要检索listProduct元素并将其作为参数发送给ko.applyBidning ,这不是敲除的工作方式。

  2. You also have mistakes using inside object in the foreach binding, check out documentation to see the correct usage. 您还可能在foreach绑定中使用内部对象时出错,请查阅文档以查看正确用法。 Even you have a misspelled in the HTML with the productName value. 即使您在HTML中使用productName值拼写错误。

  3. You have syntax errors inside both tag arrays, the values are string so you need to add quotes to each element. 两个tag数组中都存在语法错误,值是字符串,因此需要在每个元素上加上引号。

I will show a better and clean way to do it fixing the issues mentioned above: 我将展示一种更好,更干净的方法来解决上述问题:

JS JS

function VM(){ //declare your VM constructor.
  this.list = ko.observableArray([
      {id: '1', productName: 'laptop', tag: ['promotion', 'blackfriday']},
      {id: '2', productName: 'Samsung galaxy note III', tag: ['samsung', 'galaxy']}
    ]); //Add and observable array an set your data. If you don't need that this array be an observable just use a normal JS array.
}

ko.applyBindings(new VM()); //apply binding

HTML HTML

<table data-bind="foreach: list">
<tr>
    <td data-bind="text: productName"></td>
    <td data-bind="foreach: tag">
      <p data-bind="text: $data"></p>
    </td>
</tr>
</table>

Check out this fiddle to see it working. 查看这个小提琴 ,看看它是否有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM