【问题标题】:How to sort a tree table by rows (header on the left) with no <thead> [duplicate]如何在没有 <thead> 的情况下按行(左侧的标题)对树表进行排序 [重复]
【发布时间】:2021-10-24 23:49:00
【问题描述】:

我想不是按列而是按行对树表进行排序,它的标题在树部分的左侧。 我的树表看起来像这样 the tree table plugin I used.

代码是这样的

  <table>
      <tr data-node-id="index">
        <th>Index</th>
        <td>...</td>
        <td>...</td>
      </tr>
      <tr data-node-id="patient">
        <th>Patient Characteristics</th>
      </tr>
      <tr data-node-id="name" data-node-pid="patient">
        <th>Name</th>
        <td>...</td>
        <td>...</td>
      </tr> 
      <tr data-node-id="DOB" data-node-pid="patient">
        <th>DOB</th>
        <td>...</td>
        <td>...</td>
      </tr> 
      <tr data-node-id="age" data-node-pid="patient">
      ...

没有&lt;thead&gt; 部分,所以很多排序方法在这张表上效果不佳。我是javascript之类的新手,不知道这是否容易做到。但是我在网上没有找到针对这种情况的方法,所以我尝试寻求帮助。

【问题讨论】:

  • 链接的副本具有有效的 sn-p 示例。

标签: javascript html css treetable


【解决方案1】:

首先,访问表格元素:

var table = document.getElementById("table").children[0]; // You can just add an id to the table

这是一个按行排序的函数:

function sortRow(rowNum) { // rowNum is index of row: for example, name is 2
var answerArray = [];
for (i = 1; i < table.children[rowNum].children.length; i++) {
    answerArray.push(table.children[rowNum].children[i].innerHTML);
}
return answerArray;

此函数接受行号,并返回该行中所有值的数组。

【讨论】:

    猜你喜欢
    • 2023-01-30
    • 1970-01-01
    • 1970-01-01
    • 2019-02-17
    • 2018-12-24
    • 2020-01-25
    • 1970-01-01
    • 1970-01-01
    • 2021-05-13
    相关资源
    最近更新 更多