使用Javascript将可扩展表导出到CSV

使用Javascript将可扩展表导出到CSV,javascript,jquery,html,excel,Javascript,Jquery,Html,Excel,HTML中有一个可扩展表: <table width="900" id="mytable"> <tr> <th>Cost Category</th> <th>Costs</th> </tr> <tr data-depth="0" class="collapse level0" bgcolor="#A2D9CE"> <td>&l

HTML中有一个可扩展表:

<table width="900" id="mytable">
      <tr>
        <th>Cost Category</th>
        <th>Costs</th>
      </tr>
<tr data-depth="0" class="collapse level0" bgcolor="#A2D9CE">
    <td><span class="toggle collapse"></span> First row</td>
    <td>500</td>
</tr>
<tr data-depth="1" class="collapse level1" bgcolor="#D5F5E3">
    <td><span class="toggle"></span> Second row</td>
    <td>300</td>
</tr>
<tr data-depth="2" class="collapse level2" bgcolor="#D6EAF8">
    <td><span class="toggle"></span> Third row</td>
    <td>250</td>
</tr>
</table>

成本类别
成本
第一排
500
第二排
300
第三排
250
如何将其导出到CSV,同时保持结构--可扩展表

更新:该表如下所示:

要在CSV中表示此表数据,除了CSV中的“成本类别”和“成本”列之外,还需要一个“深度”列

请参阅下面的代码

const csv=[];
$(“#mytable”)。查找(“tr”)。每个((索引,行)=>{
常量类别=$(行).find(“th,td”).eq(0).text().trim();
const cost=$(行).find(“th,td”).eq(1).text().trim();
常量深度=$(行)。数据(“深度”)| |(索引===0?“深度”:0);
csv.push([类别、成本、深度].join(“,”);
});
console.log(csv.join(“\n”)

成本类别
成本
第一排
500
第二排
300
第三排
250

您所说的可扩展表是什么意思?您是否可以尝试创建一个演示文件来解决以下问题:使用折叠和切换:子行