【问题标题】:Table in Javascript - hide/show columnsJavascript 中的表格 - 隐藏/显示列
【发布时间】:2015-05-16 11:41:21
【问题描述】:

我已经搜索了几个小时,但找不到像我一样创建的表,其中有隐藏/显示实例 - 我尝试使用标准 HTML 表的一些标准隐藏/显示,但它没有t 像我需要的那样翻译工作。

我在 JS 中创建了一个从 json 加载数据的表,如下所示:

var output = "<table class = sample>",

tableHeadings = "<thead>" +

//set column names
"<tr>" +
"<th></th>" +
"<th><u>Name:</u></th>" +
"<th><u>Address:</u></th>" +
"<th><u>City:</u></th>" +
"<th><u>State:</u></th>" +
"<th><u>Phone Number:</u></th>" +
"<th><u>PO:</u></th>" +
"<th><u>Stuff:</u></th>" +
"<th><u>Stuff:</u></th>" +
"<th><u>Stuff:</u></th>" +
"<th><u>Stuff:</u></th>" +
"<th><u>Stuff:</u></th>" +
"</tr>" +
"</thead>";
output += tableHeadings;

output += "<td>"+'<a href="#" onclick="javascript:displayInfobox(' + (i) + ');">' + results[i]["Business Name"] +'<\/a>' + "</td>" +
"<td>" + results[i]["Address"] + "</td>" + 
"<td><center>" + results[i]["City"] + "</center></td>" + 
"<td><center>" + results[i]["StateListing"] + "</center></td>"; 

document.getElementById("placeholder").innerHTML = output;

我想要做的是使用按钮/复选框隐藏/显示地址列。我尝试在 jquery 中使用 style.display 和 .hide/.show 。我尝试的所有操作都会隐藏第一个条目,但之后仍会显示每个条目的地址。

我需要能够在命令中隐藏所有显示的条目的地址信息。

【问题讨论】:

    标签: javascript html-table show-hide


    【解决方案1】:

    您可以使用子选择器:

    $("td:nth-child(2)").hide()
    

    或者在你的地址td中添加一个类,并选择所有的c类:

    <td class='c'>
    $(".c").hide()
    

    【讨论】:

      【解决方案2】:

      您可以做的是在创建表格时为每个 td 应用一个类。在隐藏特定列的同时,您可以根据类名选择元素并将其隐藏。

      Here's is the fiddle, with an example

       // Using Javascript
       function hideAddress()
       {
         var elems = document.getElementsByClassName("addr");
         for(var i = 0; i<elems.length; i++) {
           elems[i].style.display = "none";
         }
       }
      
       // Using Jquery
       $("#hideAddr").click(function() {
        $(".addr").hide();
       });
      

      希望对你有帮助!

      【讨论】:

      • jquery 太棒了。但是不能以我需要的方式工作->而且我的 jquery 无法启动。感谢您的帮助!
      【解决方案3】:

      这是一个有效的示例。我使用 JQuery 只是为了创建表,但没有它该函数应该可以工作。

      http://plnkr.co/edit/MWlXNRhAAzDjPPf42a19?p=info

      $(function() {
      var results = [];
        results.push({
          'Business Name': 'Bus1',
          'Address': 1234,
          'City': 'test',
          'StateListing': 'CA'
        });
        results.push({
          'Business Name': 'Bus2',
          'Address': 5678,
          'City': 'test',
          'StateListing': 'CA'
        });
        results.push({
          'Business Name': 'Bus3',
          'Address': 9120,
          'City': 'test',
          'StateListing': 'CA'
        });
      
        function setupTable() {
          var output = "<table class = sample>",
      
            tableHeadings = "<thead>" +
      
            //set column names
            "<tr>" +
              "<th><u>Name:</u></th>" +
              "<th name='addressCol'><u>Address:</u></th>" +
              "<th><u>City:</u></th>" +
              "<th><u>State:</u></th>" +
              "<th><u>Phone Number:</u></th>" +
              "<th><u>PO:</u></th>" +
              "<th><u>Stuff:</u></th>" +
              "<th><u>Stuff:</u></th>" +
              "<th><u>Stuff:</u></th>" +
              "<th><u>Stuff:</u></th>" +
              "<th><u>Stuff:</u></th>" +
              "</tr>" +
              "</thead>";
          output += tableHeadings;
      
          for (var i = 0; i < results.length; i++) {
            output += "<tr><td>" + '<a href="#" onclick="javascript:displayInfobox(' + (i) + ');">' + results[i]["Business Name"] + '<\/a>' + "</td>" +
              "<td name='addressCol'>" + results[i]["Address"] + "</td>" +
              "<td><center>" + results[i]["City"] + "</center></td>" +
              "<td><center>" + results[i]["StateListing"] + "</center></td></tr>";
          }
      
          document.getElementById("placeholder").innerHTML = output;
        }
      
        setupTable();
      });
      
      function hideFunction() {
        var items = document.getElementsByName('addressCol');
        for (var i = 0; i < items.length; i++) {
          items[i].style.display = 'none';
        }
      }
      

      【讨论】:

      • 这很好用,减去它没有“显示”功能。我可以弄清楚,这部分很容易使用“隐藏”。感谢您的帮助!
      • 我最终复制了名为 showFunction 的函数并添加了类似 document.getElementByID('button').style.display="none/block" 的内容来显示/隐藏链接到每个按钮的每个按钮文件。可以满足我的需要。再次感谢您的帮助
      【解决方案4】:

      据我了解,您想隐藏/显示列,但我不确定 100%。下一个代码隐藏和显示表格上的列:

      <html>
        <head>
          <title>Show-Hide</title>
          <script type="text/javascript">
      
      function hide ( column ) {
      var tbl = document.getElementById( "tbl" );
      var i;
      for ( i = 0; i < tbl.rows.length; i++ )
        tbl.rows[ i ].cells[ column ].style.visibility = "hidden";
      }
      
      function restore () {
      var tbl = document.getElementById( "tbl" );
      var i;
      var j;
      for ( i = 0; i < tbl.rows.length; i++ )
        for ( j = 0; j < tbl.rows[ i ].cells.length; j++ )
          tbl.rows[ i ].cells[ j ].style.visibility = "visible";
      }
      
          </script>
        </head>
        <body>
          <table id="tbl">
            <tr>
              <td><button onclick="hide(0)">Hide</button></td>
              <td><button onclick="hide(1)">Hide</button></td>
              <td><button onclick="hide(2)">Hide</button></td>
            </tr>
            <tr>
              <td>111</td>
              <td>222</td>
              <td>333</td>
            </tr>
            <tr>
              <td>444</td>
              <td>555</td>
              <td>666</td>
            </tr>
            <tr>
              <td>777</td>
              <td>888</td>
              <td>999</td>
            </tr>
          </table>
          <br/>
          <button onclick="restore();">Restore columns</button>
        </body>
      </html>
      

      创建一个文本文件,根据需要命名,但使用扩展名 HTML,复制并粘贴之前的代码,然后在浏览器中打开它。如果不是您想要的,我们可以修复它。

      如果你想完全消失列,而不是

      tbl.rows[ i ].cells[ column ].style.visibility = "hidden"; // or "visible".
      

      使用

      tbl.rows[ i ].cells[ column ].style.display = "none"; // or "table-cell".
      

      【讨论】:

      • 我喜欢你这样做的方式,但是对于你所做的桌子 - 它不适用于我的桌子。不过还是谢谢。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-02
      • 2011-11-06
      • 2021-03-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多