简体   繁体   English

如何将逗号分隔的值分解为<tr>

[英]How to break comma separated values into <tr>

JSON JSON格式

{
  "catalog_name": ["Sistem Autodownline ", "Karipap Pusing Ayu"],
  "price": ["100", "8"],
  "qty": "",
  "qty2": ["", ""],
  "total_qty": "",
  "total": "",
  "mem": "10",
  "email_2": "",
  "ic_add": "890527-08-6136",
  "comm": "20",
  "grand": "",
  "cash": "120",
  "change": "120.00",
  "cust_id": "TGF 566",
  "cust_name": "coco crancy",
  "mem_id": "123",
  "mem_name": "QIZLAF MARKETING",
  "action": "test"
}

The above json is passed as jQuery Object via ajax to a page. 上面的json通过ajax作为jQuery Object传递给页面。 And I use foreach loop to display values that has no array. 而且我使用foreach loop来显示没有数组的值。 For those with array values,I use $.each loop like below: 对于那些具有数组值的对象,我使用$.each loop如下所示:

var _tableHTMl = "<table><thead><tr>"+
    "<th>Item Name</th>"+
    "<th>Unit Price RM</th>"+
    "<th>Qty</th>"+
    "<th>Amount</th></thead><tbody>";

$("#print_receipt").append(_tableHTML);


var new_array = {};

for (var i = 0; i < data.length; i++) {

  new_array[i] = {
    'catalog_name': data[i].catalog_name,
    'price': data[i].price,
    'qty': data[i].qty,
    'amt': data[i].grand
  };

}
//this simply displays all values within one `<tr>` with comma separated

$.each(new_array, function(index, value) {
  $("#print_receipt").append(
    "<table><tr><td>" +
    value.catalog_name +
    "</td><td>" +
    value.price +
    "</td><td>" +
    value.qty +
    "</td><td>" +
    value.amt +
    "</td></tr></tbody></table>"
  );
});

The output: 输出:

在此处输入图片说明

RESULT a per suggested by Rajesh: 结果由Rajesh建议:
在此处输入图片说明

RESULT as per suggested by BG101: 根据BG101的建议结果:

在此处输入图片说明

I ve applied a split function to your data source , then i just given the json object to a datatable ( you can still use the append method ) . 我已经对您的数据源应用了split函数,然后我只是将json对象提供给了数据表(您仍然可以使用append方法)。

 $(document).ready(function() { var json = '[{"catalog_name": ["Sistem Autodownline ", "Karipap Pusing Ayu"],"price": ["100", "8"],"qty": "","qty2": ["", ""],"total_qty": "","total": "","mem": "10","email_2": "","ic_add": "890527-08-6136","comm": "20","grand": "","cash": "120","change": "120.00","cust_id": "TGF 566","cust_name": "coco crancy","mem_id": "123","mem_name": "QIZLAF MARKETING","action": "test"}]'; var data = JSON.parse(json); $.each(data,function(i,v){ if($.isArray(v.catalog_name)){ var newone = $.map(v.catalog_name,function(elem,j){ return {catalog_name: elem, price: v.price[j], qty: v.qty, grand: v.grand } }); Array.prototype.splice.apply(data,[i, newone.length].concat(newone)); } }); $('#table').DataTable({ data: data, columns: [{ data: "catalog_name" }, { data: "price" }, { data: "qty" }, { data: "grand" }] }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link href="https://cdn.datatables.net/1.10.10/css/jquery.dataTables.min.css" rel="stylesheet" /> <script src="https://cdn.datatables.net/1.10.10/js/jquery.dataTables.min.js"></script> <table id="table"> <thead> <tr> <th>Item Name</th> <th>Unit Price RM</th> <th>Qty</th> <th>Amount</th> </tr> </thead> <tbody> </tbody> </table> 

This is causing issues, 这引起了问题,

$.each(new_array, function(index, value) {
  $("#print_receipt").append(
    "<table><tr><td>" +
    value.catalog_name +
    "</td><td>" +
    value.price +
    "</td><td>" +
    value.qty +
    "</td><td>" +
    value.amt +
    "</td></tr></tbody></table>"
  );
});

If you notice, you are adding table inside table . 如果您发现,要添加tabletable Update it to this: 将其更新为:

$.each(new_array, function(index, value) {
  $("#print_receipt").append(
    "<tr><td>" +
    value.catalog_name +
    "</td><td>" +
    value.price +
    "</td><td>" +
    value.qty +
    "</td><td>" +
    value.amt +
    "</td></tr>"
  );
});

Edit 1 编辑1

As pointed out by @BG101, print_receipt is a container and not a table . 正如@ BG101指出的那样, print_receipt是一个容器,而不是一个table So either you can add ID while creating table, or you can add table in querySelector like $("#print_receipt table") . 因此,您既可以在创建表时添加ID ,也可以在querySelector添加表,例如$("#print_receipt table")

Also following is a working sample code: 另外,下面是一个有效的示例代码:

 var data = [{ "catalog_name": ["Sistem Autodownline ", "Karipap Pusing Ayu"], "price": ["100", "8"], "qty": "", "qty2": ["", ""], "total_qty": "", "total": "", "mem": "10", "email_2": "", "ic_add": "890527-08-6136", "comm": "20", "grand": "", "cash": "120", "change": "120.00", "cust_id": "TGF 566", "cust_name": "coco crancy", "mem_id": "123", "mem_name": "QIZLAF MARKETING", "action": "test" }] var _tableHTMl = "<table><thead><tr>" + "<th>Item Name</th>" + "<th>Unit Price RM</th>" + "<th>Qty</th>" + "<th>Amount</th></thead><tbody>"; $("#print_receipt").append(_tableHTMl); var new_array = []; for (var i = 0; i < data.length; i++) { new_array.push({ 'catalog_name': data[i].catalog_name, 'price': data[i].price, 'qty': data[i].qty, 'amt': data[i].grand }); } $.each(new_array, function(index, value) { $("#print_receipt table").append( "<tr><td>" + value.catalog_name + "</td><td>" + value.price + "</td><td>" + value.qty + "</td><td>" + value.amt + "</td></tr>" ); }); 
 td{ border:1px solid gray; padding:2px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <div id="print_receipt" ></div> 

You shouldn't create another <table> tag but extend the <tbody> 您不应该创建另一个<table>标签,而应扩展<tbody>

$("#print_receipt").append(
"<table><tr><td>" +
value.catalog_name +

this will break the connection within the layout 这将断开布局内的连接
it should be more like this: 应该更像这样:

$("#print_receipt").append(
"<tr><td>" +

... an don't forget the closing tags ...不要忘了结束标签
for the multiple values (arrays) you could use array.join('<concatenation character[s]') which will transform them to a string. 对于多个值(数组),可以使用array.join('<concatenation character[s]')将其转换为字符串。

or if you need to do special transformation where you want for example a list within a <td> you can use 或者,如果您需要进行特殊的转换,例如在<td>的列表,则可以使用

"<ul>" + array.map(function(item){ return "<li>" + item + "<li>"; }) + "</ul>" or something like that "<ul>" + array.map(function(item){ return "<li>" + item + "<li>"; }) + "</ul>"或类似的东西

but the layout breaks because you're not extending the table with the <th> you're breaking it :). 但是布局中断了,因为您没有用<th>扩展表:)。

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

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