Javascript 关于在angular JS中创建矩阵表的建议

Javascript 关于在angular JS中创建矩阵表的建议,javascript,json,angularjs,angularjs-ng-repeat,Javascript,Json,Angularjs,Angularjs Ng Repeat,下面是我的JSON对象(部分),我希望最好使用ng repeat将其显示为矩阵表: [ { "product": "milk", "resource": "a", "checked": true }, { "product": "bread", "resource": "a", "checked": false }, { "product": "

下面是我的JSON对象(部分),我希望最好使用ng repeat将其显示为矩阵表:

[
    {
        "product": "milk",
        "resource": "a",
        "checked": true
    },
    {
        "product": "bread",
        "resource": "a",
        "checked": false
    },
    {
        "product": "butter",
        "resource": "a",
        "checked": true
    }
]


我已经试过了,但我不想使用coffeescript。

没有ng,但使用纯javascript

基本上,您需要一个函数来返回具有给定属性的数组的checked属性。此函数在元素上迭代,直到属性值匹配为止

第二部分是生成表,我认为在ng中应该更容易

功能已检查(产品、资源){
返回数据。部分(函数(a){
if(a.product==产品和a.resource==资源){
返回a.checked;
}
});
}
风险值数据=[
{
“产品”:“牛奶”,
“资源”:“a”,
“已检查”:真
},
{
“产品”:“面包”,
“资源”:“a”,
“选中”:false
},
{
“产品”:“黄油”,
“资源”:“a”,
“已检查”:真
}
],
cols=[“牛奶”、“面包”、“黄油”、“奶酪”、“果酱”],
行=['a','b','c'],
table=document.createElement(“表”),
tr=document.createElement(“tr”),
th=document.createElement(“th”),
运输署;
th.innerHTML='';
tr.appendChild(th);
cols.forEach(函数(c,j){
th=document.createElement(“th”);
th.innerHTML=c;
tr.appendChild(th);
});
表1.儿童(tr);
forEach(函数(r,i){
tr=document.createElement(“tr”);
td=document.createElement(“td”);
td.innerHTML=r;
tr.appendChild(td);
cols.forEach(函数(c,j){
td=document.createElement(“td”);
td.innerHTML=+isChecked(c,r);
tr.appendChild(td);
});
表1.儿童(tr);
});
document.getElementById('table').appendChild(table)

@这里是一个更好的json示例,可以满足您的需要:

[{
  "resource": "a",
  products: [{
    "product": "milk",
    "checked": true
  }, {
    "product": "bread",
    "checked": false
  }, {
    "product": "butter",
    "checked": true
  }]
}, {
  "resource": "b",
  products: [{
    "product": "milk",
    "checked": false
  }, {
    "product": "bread",
    "checked": true
  }, {
    "product": "butter",
    "checked": true
  }]
}, {
  "resource": "c",
  products: [{
    "product": "milk",
    "checked": false
  }, {
    "product": "bread",
    "checked": true
  }, {
    "product": "butter",
    "checked": true
  }]
}]

虽然这不是你问题的确切解决方案,但我希望这能让你知道需要做什么。我正在按顺序打印阵列,但您可以轻松更改条件

将数组转换为矩阵/网格 我有一个数组,我想把它转换成列大小为4的网格/矩阵。下面的实现对我有效。您可以在嵌套的ng repeat中使用两个计数器:行和列

在我的例子中,列数是3。但是你可以用一个变量替换3
h.seats
是我的对象数组,我想根据数组中元素的值打印X或-

<div class="table-responsive">
    <table class="table table-bordered">
        <thead>
            <tr>
                <th ng-repeat="n in [].constructor(3 + 1) track by $index">{{$index}}</th>
            </tr>
        </thead>
        <tbody>
            <tr ng-repeat="(row, y) in getNumber(h.seats.length, 3) track by $index">
                <td>{{row+1}}</td>
                <td class="text-primary"
                    ng-repeat="(col, t) in h.seats track by $index"
                    ng-if="col >= (row)*3 && col < (row+1)*3">
                        <span ng-show="t.status"> X </span> 
                        <span ng-show="!t.status"> - </span>
                </td>
            </tr>
        </tbody>
    </table>
</div>
ng if=“col>=(行)*3&&col<(行+1)*3”
是计算应将哪些元素放入该行的重要逻辑。 输出如下所示

0  1  2  3 
1  e1 e2 e3 
2  e4 e5 e6 
3  e7 e8 
有关如何使用行和列计数器的详细信息,请参阅以下链接:

您应该首先使您的Json有效。我已经在Json中显示了前3项。它们是正确的。根据此属性,名称应封装在有效Json的双引号中。无论如何,您的Json格式无法用于您需要的内容。如果您希望表是动态的(不依赖于数据),它应该采用不同的格式。我如何使用ng repeat进行迭代以显示此JSON?请检查此插件。此处我无法正确显示数据。请检查此处我哪里做错了请在此处查找问题或使用此插件如何从按钮单击上的输入文本框中获取值您需要如何准确存储数据?在单独对象中的初始json r中?请找到这个plunker,我需要在其中单击save从文本框中获取单个值。或者,使用这个plunker,我如何从按钮单击的输入文本框中获取值?请随意提出一个新问题。这是我不知道angular.js的问题。
0  1  2  3 
1  e1 e2 e3 
2  e4 e5 e6 
3  e7 e8