.= 'tag.htm'; break; case 'flag': $pre .= $default_pre .= 'flag.htm'; break; case 'my': $pre .= $default_pre .= 'my.htm'; break; case 'my_password': $pre .= $default_pre .= 'my_password.htm'; break; case 'my_bind': $pre .= $default_pre .= 'my_bind.htm'; break; case 'my_avatar': $pre .= $default_pre .= 'my_avatar.htm'; break; case 'home_article': $pre .= $default_pre .= 'home_article.htm'; break; case 'home_comment': $pre .= $default_pre .= 'home_comment.htm'; break; case 'user': $pre .= $default_pre .= 'user.htm'; break; case 'user_login': $pre .= $default_pre .= 'user_login.htm'; break; case 'user_create': $pre .= $default_pre .= 'user_create.htm'; break; case 'user_resetpw': $pre .= $default_pre .= 'user_resetpw.htm'; break; case 'user_resetpw_complete': $pre .= $default_pre .= 'user_resetpw_complete.htm'; break; case 'user_comment': $pre .= $default_pre .= 'user_comment.htm'; break; case 'single_page': $pre .= $default_pre .= 'single_page.htm'; break; case 'search': $pre .= $default_pre .= 'search.htm'; break; case 'operate_sticky': $pre .= $default_pre .= 'operate_sticky.htm'; break; case 'operate_close': $pre .= $default_pre .= 'operate_close.htm'; break; case 'operate_delete': $pre .= $default_pre .= 'operate_delete.htm'; break; case 'operate_move': $pre .= $default_pre .= 'operate_move.htm'; break; case '404': $pre .= $default_pre .= '404.htm'; break; case 'read_404': $pre .= $default_pre .= 'read_404.htm'; break; case 'list_404': $pre .= $default_pre .= 'list_404.htm'; break; default: $pre .= $default_pre .= theme_mode_pre(); break; } if ($config['theme']) { $conffile = APP_PATH . 'view/template/' . $config['theme'] . '/conf.json'; $json = is_file($conffile) ? xn_json_decode(file_get_contents($conffile)) : array(); } !empty($json['installed']) and $path_file = APP_PATH . 'view/template/' . $config['theme'] . '/htm/' . ($id ? $id . '_' : '') . $pre; (empty($path_file) || !is_file($path_file)) and $path_file = APP_PATH . 'view/template/' . $config['theme'] . '/htm/' . $pre; if (!empty($config['theme_child']) && is_array($config['theme_child'])) { foreach ($config['theme_child'] as $theme) { if (empty($theme) || is_array($theme)) continue; $path_file = APP_PATH . 'view/template/' . $theme . '/htm/' . ($id ? $id . '_' : '') . $pre; !is_file($path_file) and $path_file = APP_PATH . 'view/template/' . $theme . '/htm/' . $pre; } } !is_file($path_file) and $path_file = APP_PATH . ($dir ? 'plugin/' . $dir . '/view/htm/' : 'view/htm/') . $default_pre; return $path_file; } function theme_mode_pre($type = 0) { global $config; $mode = $config['setting']['website_mode']; $pre = ''; if (1 == $mode) { $pre .= 2 == $type ? 'portal_category.htm' : 'portal.htm'; } elseif (2 == $mode) { $pre .= 2 == $type ? 'flat_category.htm' : 'flat.htm'; } else { $pre .= 2 == $type ? 'index_category.htm' : 'index.htm'; } return $pre; } ?>Table in Javascript - hideshow columns - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Table in Javascript - hideshow columns - Stack Overflow

programmeradmin0浏览0评论

I've searched SO for hours but have not been able to find a table created like mine where there are hide/show instances - I've tried using some of the standard hide/show for standard HTML tables however it doesn't translate over to work like I need.

I have a table created in JS that loads data from a json and looks like:

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;

What I am trying to do is hide/show using a button/checkbox the address column. I have tried using style.display as well as .hide/.show in jquery. Everything I try will hide the first entry but still display the addresses for every entry after that.

I need to be able hide the address information on mand for ALL of the entries that are displayed.

I've searched SO for hours but have not been able to find a table created like mine where there are hide/show instances - I've tried using some of the standard hide/show for standard HTML tables however it doesn't translate over to work like I need.

I have a table created in JS that loads data from a json and looks like:

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;

What I am trying to do is hide/show using a button/checkbox the address column. I have tried using style.display as well as .hide/.show in jquery. Everything I try will hide the first entry but still display the addresses for every entry after that.

I need to be able hide the address information on mand for ALL of the entries that are displayed.

Share Improve this question edited Oct 15, 2017 at 20:46 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Mar 13, 2015 at 20:40 AaronAaron 191 gold badge1 silver badge8 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 4

You could use child selector:

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

Or add a class in your address td, and select all of the c class:

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

What you can do is to apply a class to each of your td while creating your table. And while hiding a particular column you can selected elements based on the class name and hide it.

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();
 });

Hope it helps !

Here is a sample that works. I used JQuery just to create the table but the function should work without it.

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';
  }
}

As far as I understood, you want to hide/show columns, but I am not 100% sure. Next code hides and shows columns on a table :

<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>

Create a text file, name it as you want but use the extension HTML, copy and paste previous code, and open it in a browser. If it is not what you are looking for, we can fix it.

If you want to pletely disappear the column, instead of

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

use

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

评论列表(0)

  1. 暂无评论