我在具有父类别和子类别数据的数组变量中有一个JSON对象。我想将子类别放在父类别中作为嵌套表。

例如:

const jsonData = [{
  category_id: '010103',
  level1: 'electronics',
  level2: 'TV',
  level3: 'samsung',
}, {
  category_id: '010104',
  level1: 'electronics',
  level2: 'TV',
  level3: 'philips',
}, {
  category_id: '010101',
  level1: 'households',
  level2: 'kitchen',
  level3: 'table',
}];


我想像这样将它们放在桌子上:
javascript - 将json对象打印到嵌套的html表中-LMLPHP

这是我尝试过的:

for (let i = 0; i < list.length; i++) {
  const row = list[i];
  console.log(row);
  let evenClass = 'even';
  if ((i + 1) % 2 == 0) {
    evenClass = 'odd';
  }

  const low = list[i - 1];
  if (row[i] != 0 || 'level1' in low) {
    if (row.level1 == low.level1) {
      const append = `${'<tr> '
        + ' <td>'}${row.level2}</td>`
        + ` <td>${row.level3}</td>`
        + ` <td>${row.meta_count}</td>`
        + ` <td>${moment(row.reg_date).format('YYYY-MM-DD HH:mm:ss')}</td>`
        + ` <td>${row.category_id}</td>`
        + '  </tr>';
      $('#tbody').append(append);
    } else {
      const append = `${'<tr> '
        + ' <td>'}${row.level1}</td>`
        + ` <td>${row.level2}</td>`
        + ` <td>${row.level3}</td>`
        + ` <td>${row.meta_count}</td>`
        + ` <td>${moment(row.reg_date).format('YYYY-MM-DD HH:mm:ss')}</td>`
        + ` <td>${row.category_id}</td>`
        + '  </tr>';
      $('#tbody').append(append);
    }
  } else {
    const appendTr = `${'<tr> '
      + '   <td>'}${row.level1}</td>`
      + `   <td>${row.level2}</td>`
      + `   <td>${row.level3}</td>`
      + `   <td>${row.meta_count}</td>`
      + `   <td>${moment(row.reg_date).format('YYYY-MM-DD HH:mm:ss')}</td>`
      + `   <td>${row.category_id}</td>`
      + '  </tr>';
    $('#tbody').append(appendTr);
  }
}


我该如何实现(跳过相同的名称级别)?

最佳答案

查看JS Fiddle:https://jsfiddle.net/d2fL3rhp/

首先,我们检索了唯一的level1,level2和level3详细信息,然后在使用$ .each循环准备所需的HTML正文之后。

JS代码:

let list = [
                {category_id: "010103", level1: "electronics", level2: "TV", level3: "samsung"},
                {category_id: "010104", level1: "electronics", level2: "TV", level3: "philips"},
                {category_id: "010101", level1: "households", level2: "kitchen", level3: "table"},
                {category_id: "0101021", level1: "households", level2: "kitchen", level3: "table"},
                {category_id: "0101022", level1: "households", level2: "kitchen", level3: "table"},
                {category_id: "0101023", level1: "households", level2: "kitchen", level3: "table"},
                {category_id: "0101031", level1: "grocery", level2: "abc", level3: "xyz"},
                {category_id: "0101032", level1: "grocery", level2: "abc", level3: "pqr"},
                {category_id: "0101033", level1: "grocery", level2: "def", level3: "xyz"},
            ];
let uniqueLevels = {};
if( list != null && list.length > 0 ) {
    $.each(list, function(index, value){
        if(value.level1 != null && (uniqueLevels[value.level1] == null || typeof uniqueLevels[value.level1] == 'undefined')) {
            uniqueLevels[value.level1] = {};
        }
        if( value.level2 != null && (uniqueLevels[value.level1][value.level2] == null || typeof uniqueLevels[value.level1][value.level2] == 'undefined') ) {
            uniqueLevels[value.level1][value.level2] = {};
        }
        if( value.level3 != null ) {
            if(uniqueLevels[value.level1][value.level2][value.level3] == null || typeof uniqueLevels[value.level1][value.level2][value.level3] == 'undefined') {
                uniqueLevels[value.level1][value.level2][value.level3] = 1;
            }
            else {
                uniqueLevels[value.level1][value.level2][value.level3] += 1;
            }
        }
    });
}

var tbodyHTML = '';
$.each(uniqueLevels, function(level1_key, level1_data){
    $.each(level1_data, function(level2_key, level2_data){
        var level2Rows = '', flagAddTR = false, rowspanCount = 0;
        $.each(level2_data, function(level3_key, level3_data){
            rowspanCount += level3_data;
        });

        tbodyHTML += '<tr><td rowspan="'+ rowspanCount +'">'+ level1_key +'</td><td rowspan="'+ rowspanCount +'">'+ level2_key +'</td>';
        $.each(list, function(key, value){
            if( (value['level1'] == level1_key) && (value['level2'] == level2_key) ) {
                if( level2Rows != '' ) {
                    flagAddTR = true;
                }

                if( flagAddTR ) {
                    level2Rows += '<tr>';
                }
                level2Rows += '<td>'+ value['level3'] +'</td><td>'+ value['category_id'] +'</td></tr>';
            }
        });
        tbodyHTML += level2Rows;
    });
});

$('#tbody').html(tbodyHTML);

关于javascript - 将json对象打印到嵌套的html表中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59727507/

10-09 16:48