【问题标题】:Sorting a Table but Table header is broken对表格进行排序但表格标题已损坏
【发布时间】:2020-06-28 21:40:58
【问题描述】:

我正在为我的 Minecraft 服务器创建一个帮助部分,以便玩家可以查看命令及其用法。 我对它们进行了排序,以便在尝试查找命令时更有意义。

由于某种原因,它通过 Cmds 组织表格,但将表格标题放在底部。 我如何让它跳过这个? 由于某种原因,还有一个空白条目。

<table id="cmdlist">
    <tr>
        <th>Command</th>
        <th><i style="color: rgb(0,255,255);">Tuple</i> Arguments</th>
        <th>Usage</th>
        <th>Server(s)</th>
        <th>Rank</th>
    </tr>
    <td>
    </td>
</table>
<script type="text/javascript">
    var Cmds,Args,Usage,Server,Rank;
    Cmds = ["/server","/msg","/r","/island,/is","/towny,/t","/nation,/n","/echest","/craft,/wb,/workbench","/tpa","/tpahere","/tpaccept"];
    Args = ["(servername)","(playname) (message)","(message)","(optional)","(optional)","(optional)","N/A","N/A","(playername)","(playername)","N/A"]
                Usage = ["Used to switch between the hub and gamemodes.",
                "Used to send a pm to a player.",
                "Sends a pm to the player you last sent/recieved a pm from.",
                "Without args opens gui. With args open a specific section.",
                "Without args displays town info. With args you can config your town.",
                "Without args displays nation info. With args you can config your nation.",
                "Open your enderchest via a command.",
                "Open a crafting table via a command.",
                "Sends a request to teleport to a player.",
                "Sends a request to teleport to a player to you.",
                "Accepts the last pending teleport request."
                ]
    Server = ["*","*","*","Skyblock","Towny","Towny","Towny/Skyblock","Towny/Skyblock","Towny/Skyblock","Towny/Skyblock","Towny/Skyblock"]
    Rank = ["*","*","*","* (Skyblock owner)","* (Town owner)","* (Town owner)","Donator,Donator+,DonatorPro,*Staff","Donator,Donator+,DonatorPro,*Staff","*","*","*"]
    Cmds.sort()
    Cmds.reverse()
    console.log(Cmds)
    var table = document.getElementById("cmdlist");
    var I;
    for (i = 1; i < Cmds.length; i++) {
        // Create an empty <tr> element and add it to the 1st position of the table:
        var row = table.insertRow(0);

        // Insert new cells (<td> elements) at the 1st and 2nd position of the "new" <tr> element:
        var cell1 = row.insertCell(0);
        var cell2 = row.insertCell(1);
        var cell3 = row.insertCell(2);
        var cell4 = row.insertCell(3);
        var cell5 = row.insertCell(4);

        // Add some text to the new cells:
        cell1.innerHTML = Cmds[i];
        cell2.innerHTML = Args[i];
        cell3.innerHTML = Usage[i];
        cell4.innerHTML = Server[i];
        cell5.innerHTML = Rank[i];
    }
</script> 

【问题讨论】:

  • 您的表中有一个空且不必要的td。 Usage 数组缩进太多。两者都没有任何影响(非常确定td,绝对确定缩进) - 我见过更糟的...... .
  • 编辑器里不是这样的,那是我刚贴进去的时候。我把td去掉了
  • insertRow(index)insertCell(index) 插入 before 索引,将所有内容进一步向下/向右推(如果 dir==rtl 则向左)。 0 附加,-1 附加。对于“仅在末尾添加一行”,请使用 -1

标签: javascript html arrays sorting


【解决方案1】:

table.insertRow(0) 会将您的行添加到表格的开头,将之前的所有行向下推(包括您的表格标题)。你的意思是table.insertRow(-1) 会在表格的end 中添加一行吗?

insertRow reference

【讨论】:

    【解决方案2】:

    在您的代码中,您只对第一列进行排序.. 可以吗?

       const Cmds = [
           "/server",
           "/msg",
           "/r",
           "/island,/is",
           "/towny,/t",
           "/nation,/n",
           "/echest",
           "/craft,/wb,/workbench",
           "/tpa",
           "/tpahere",
           "/tpaccept"
       ];
       const Args = [
           "(servername)",
           "(playname) (message)",
           "(message)",
           "(optional)",
           "(optional)",
           "(optional)",
           "N/A",
           "N/A",
           "(playername)",
           "(playername)",
           "N/A"
       ];
       const Usage = [
           "Used to switch between the hub and gamemodes.",
           "Used to send a pm to a player.",
           "Sends a pm to the player you last sent/recieved a pm from.",
           "Without args opens gui. With args open a specific section.",
           "Without args displays town info. With args you can config your town.",
           "Without args displays nation info. With args you can config your nation.",
           "Open your enderchest via a command.",
           "Open a crafting table via a command.",
           "Sends a request to teleport to a player.",
           "Sends a request to teleport to a player to you.",
           "Accepts the last pending teleport request."
       ];
       const Server = [
           "*",
           "*",
           "*",
           "Skyblock",
           "Towny",
           "Towny",
           "Towny/Skyblock",
           "Towny/Skyblock",
           "Towny/Skyblock",
           "Towny/Skyblock",
           "Towny/Skyblock"
       ];
       const Rank = [
           "*",
           "*",
           "*",
           "* (Skyblock owner)",
           "* (Town owner)",
           "* (Town owner)",
           "Donator,Donator+,DonatorPro,*Staff",
           "Donator,Donator+,DonatorPro,*Staff",
           "*",
           "*",
           "*"
       ];
    
       Cmds.sort().reverse();
       var table = document.getElementById("cmdlist");
       for (let i = 0; i < Cmds.length; i++) {
           var row = table.insertRow(i+1);
           [
            Cmds,
            Args,
            Usage,
            Server,
            Rank
          ].forEach((column, index) => {
            row.insertCell(index).innerHTML = column[i]
          })
      }
    table, th, td {
      border: 1px dotted #cdcdcd;
    }
    
    tr:nth-child(even) {
      background: #eee
    }
    tr:nth-child(odd) {
      background: #FFF
    }
    
    th {
      background-color: #000;
      color: white;
    }
    <table id="cmdlist">
        <tr>
            <th>Command</th>
            <th><i style="color: rgb(0,255,255);">Tuple</i> Arguments</th>
            <th>Usage</th>
            <th>Server(s)</th>
            <th>Rank</th>
        </tr>
    </table>

    【讨论】:

    • 我删除了 .reverse 我把它放进去,因为它出于某种原因从 z-a 排序,你这样做的方式意味着我不需要那个。否则效果很好。
    猜你喜欢
    • 1970-01-01
    • 2017-11-08
    • 1970-01-01
    • 2011-10-25
    • 2019-07-26
    • 1970-01-01
    • 2020-06-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多