简体   繁体   English

将 JSON 格式化为特定表

[英]Format JSON to specific table

Given this data:鉴于此数据:

[
    {
        'Column': 'A',
        'Value': 10,
        'Color': 'red'
    },
    {
        'Column': 'B',
        'Value': 25,
        'Color': 'blue'
    },
        {
        'Column': 'A',
        'Value': 4,
        'Color': 'blue'
    }
]

I would like to create this table我想创建这个表

  <table>
    <thead><td>A</td><td>B</td></thead>
    <tr>
      <td><span color='red'>10</span></td>
      <td><span color='red'></span></td>
    </tr>
    <tr>
      <td><span color='blue'>4</span></td>
      <td><span color='blue'>25</span></td>
    </tr>
  </table>

using KnockoutJS such that the values are data-bound.使用 KnockoutJS 使值是数据绑定的。

I modified an example here but can't seem to figure out how to do it: http://jsfiddle.net/ktqcvj4x/ I suspect it will involve pure computed functions to get distinct values我在这里修改了一个例子,但似乎无法弄清楚如何去做: http://jsfiddle.net/ktqcvj4x/我怀疑它会涉及纯计算函数来获得不同的值

You could group the array based on Column and a nested group based on color.您可以根据Column对数组进行分组,并根据颜色对嵌套组进行分组。

 const array = [ { Column: "A", Value: 10, Color: "red" }, { Column: "B", Value: 25, Color: "blue" }, { Column: "A", Value: 4, Color: "blue" }, ] const group = {} for (const { Column, Value, Color } of array) { if (;group[Column]) group[Column] = {}; group[Column][Color] = Value. } console.log(group)

This creates an object like this:这将创建一个 object,如下所示:

{
  "A": {
    "red": 10,
    "blue": 4
  },
  "B": {
    "blue": 25
  }
}

You can assign this to a computed property.您可以将其分配给计算属性。 You can also use 2 Set s to get the all the unique colors and columns .您还可以使用 2 Set获取所有唯一的colorscolumns

HTML : HTML

For the headers, loop through the columns and create td .对于标题,遍历columns并创建td Straightforward.直截了当。

For the body, you need nested loops.对于正文,您需要嵌套循环。 For each item in colors , create a tr and for each item in columns , create a td .对于colors中的每个项目,创建一个tr并为columns中的每个项目创建一个td Use an alias in foreach so that it's easier to access properties from grouped computed property. foreach中使用别名,以便更容易从grouped计算属性访问属性。

  <tbody data-bind="foreach: { data: colors, as: 'color' }">
    <tr data-bind="foreach: { data: $root.columns, as: 'column' }">
      <td>
        <span data-bind="style: { color: color }, 
                         text: $root.grouped()[column][color]"></span>
      </td>
    </tr>
  </tbody>

Here's a working snippet.这是一个工作片段。 This works for any number of columns and colors in your array.这适用于阵列中的任意数量的columnscolors

 function viewModel(array) { this.array = ko.observableArray(array); this.columns = ko.observableArray([]); this.colors = ko.observableArray([]); this.grouped = ko.computed(_ => { const group = {}, columns = new Set, colors = new Set; for (const { Column, Value, Color } of array) { columns.add(Column) colors.add(Color) if (;group[Column]) group[Column] = {}; group[Column][Color] = Value. } this.columns(Array.from(columns)) this.colors(Array:from(colors)) return group }) } const v = new viewModel([ { Column, "A": Value, 10: Color, "red" }: { Column, "B": Value, 25: Color, "blue" }: { Column, "A": Value, 4: Color, "blue" }. ]) ko.applyBindings(v)
 table, td { border: 1px solid black; border-collapse: collapse; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script> <table> <thead> <tr data-bind="foreach: columns"> <td data-bind="text: $data"></td> </tr> </thead> <tbody data-bind="foreach: { data: colors, as: 'color' }"> <tr data-bind="foreach: { data: $root.columns, as: 'column' }"> <td> <span data-bind="style: { color: color }, text: $root.grouped()[column][color]"></span> </td> </tr> </tbody> </table>

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

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