0

I using DataTables (https://datatables.net/) for data arrangement, and I already achieved show data in child row, the more I want to do is showing more detail data in it.

I have a JSON data like this

{
    "MACHINE": "A01",
    "STATUS": "ENGTEST",
    "TXN_TIME": "2020/09/17 00:04:15",
    "AVAINFO": [
      { 
        "RECIPE": "8EX-001",
        "ava": "Z03341#25#A9161#P#Z03934#5#A9021#P"
      },
      {
        "RECIPE": "8EX-005",
        "ava": "Z80597#3#B3542#L"
      }
    ],
    "ID": "1"
  }

and I want to split [ava] with "#" and show the data format in child row like this enter image description here

the detail code I have tried as below link http://live.datatables.net/bohoriga/1/edit

2 Answers 2

1

You can build a sub-table inside your format(d) function - something like this:

function format (d) {
    var sub_table = '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">';
    
    var sub_rows = d.AVAINFO;
    var array_length = sub_rows.length;
    var sub_row = '';

    if (array_length) { // AVAINFO is an array of values:
      for (var i = 0; i < array_length; i++) {
        sub_row = '<tr><td>' + sub_rows[i].RECIPE + '</td><td>' + sub_rows[i].ava + '</td></tr>';
        sub_table = sub_table + sub_row;
      }
    } else { // AVAINFO is a single object - not an array:
      sub_row = '<tr><td>' + d.AVAINFO.RECIPE + '</td><td>' + d.AVAINFO.ava + '</td></tr>';
      sub_table = sub_table + sub_row;
    }

    sub_table = sub_table + '</table>';
    return sub_table;
}

However, bear in mind that the code sample you link to in the question has some other bugs in it, which need to be fixed before my proposed approach will work.

For example, the DataTable refers to non-existent columns (e.g. "targets": [4,5,6,7,8] and elsewhere) and to non-existent data, e.g. Type.

Also, the JSON data in $ajax_data is not valid. It needs to be an array:

$ajax_data = [
  {
    "MACHINE": "A01",
    "STATUS": "ENGTEST",
    "TXN_TIME": "2020/09/17 00:04:15",
    "AVAINFO": [
      { 
        "RECIPE": "8EX-001",
        "ava": "Z03341#25#A9161#P#Z03934#5#A9021#P"
      },
      {
        "RECIPE": "8EX-005",
        "ava": "Z80597#3#B3542#L"
      }
    ],
    "ID": "1"
  },
  {
    "MACHINE": "A03",
    "STATUS": "IDLE",
    "TXN_TIME": "2020/11/30 21:25:43",
    "AVAINFO": {
      "RECIPE": "8PI",
      "ava": "Z90018#25#B1825#P#Z80238#20#B1115#P#Z20018#13#B0095#L#"
    },
    "ID": "2"
  }
];
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you @andrewjames, I renew the link of the example and tried to use slice() and .split() to solve it, could u give me more advice to approach the result?
I am not sure what you mean. What specific problem are you trying to solve?
1

I reference other related disscusions then solved this by add .splice(), .slice(), and use loop to create sub-row.

function separate(arr, size) {
  let res = [];
  while (arr.length) {
    res.push(arr.splice(0, size));
  }
  return res;
}

function format ( d ) {
  var tr = '';
  d.AVAINFO.forEach(function (item) {
    var  a = item.ava.split("#");
    result = separate(a, 4)   
    //console.log(result);
  tr += '<tr>'+          
            '<th>Recipe</th>'+
            '<th>ORDER</th>'+
            '<th>QTY</th>'+
            '<th>CODE</th>'+
            '<th>TYPE</th>'+
        '</tr>';
  
    for (i=0; i<result.length; i++) {
        tr += '</tr>'+
        '<tr>'+
            '<td>'+item.RECIPE+'</td>'+
            '<td>'+result[i][0]+'</td>'+
            '<td>'+result[i][1]+'</td>'+
            '<td>'+result[i][2]+'</td>'+
            '<td>'+result[i][3]+'</td>'+
        '</tr>';
    }
  });
  
    return '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">'+
      tr +
      '</table>';
}

1 Comment

Nice! - glad you figured out what you needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.