简体   繁体   English

使用Java显示/隐藏表行

[英]Show/Hide Table Rows with Javascript

I have an array of prices that I output to a table with a foreach loop. 我有一个价格数组,并通过foreach循环输出到表中。

I'm trying to figure out how to hide rows in the table if a condition is met. 我试图弄清楚如果满足条件如何隐藏表中的行。 The $status variable evaluates to "YES" if price is => 30 and "NO" if < 30 . $status变量评估为“YES”,如果价格=> 30“否”,如果< 30

<script type="text/javascript">
  function displayRow(){
    var row = document.getElementById("<?php echo $status;?>");
    if (row.id == 'YES')  row.id= 'none';
    else row.display = '';
  }
</script>

<?php
  if($price > 30.00){
    $status = "YES";
  }else if($price < 30.00){
    $status = "NO";
  }

  $prices = array ("20", "56", "33", "12", "66", "25", "55");
  echo "<table border='1'>";
  foreach ($prices as $price) {
    echo "<tr id='$status'><td>$price</td></tr>";
  } 
  echo "</table>";
?>

<button onclick="displayRow()" >Show / Hide</button>

I've tried setting the tr id with the appropriate status. 我尝试将tr id设置为适当的状态。 So then in the Javascript function I try to pass the $status in to getElementById which should then display the prices that are > than 30 and hide those that are < 30 . 所以后来在Javascript函数我尝试通过$statusgetElementById然后应显示为价格>30和隐藏那些< 30
The button is meant to toggle between display all the data or filter those prices > 30 . 该按钮用于在显示所有数据之间切换或过滤价格> 30价格。

I know this is a very basic example and have probably made a lot of mistakes, but I'd appreciate any help. 我知道这是一个非常基本的示例,并且可能犯了很多错误,但是我将不胜感激。

Cheers 干杯

First of all, your code has wrong order (you're trying to read variables before they're even set). 首先,您的代码顺序错误(您试图在设置变量之前先读取变量)。 Therefore it gives undefined values. 因此,它给出了未定义的值。 Make sure you set the $price and $status before you pass them to the script. 在将它们传递到脚本之前,请确保已设置$price$status Also don't use id as show/hide condition (as id have to be unique), use class instead. 也不要使用id作为显示/隐藏条件(因为id必须是唯一的),请改用class。

<?php
  $price = 31;

  $prices = array ("20", "56", "33", "12", "66", "25", "55");
  echo "<table border='1'>";
  foreach ($prices as $price) {
    echo "<tr style='display:block;' class='".($price > 30 ? 'YES' : 'NO')."'><td>$price</td></tr>";
  } 
  echo "</table>";
?>

<script type="text/javascript">
  function displayRow(){
    var elems = document.getElementsByClassName("<?php echo ($price > 30 ? 'YES' : 'NO');?>");
    for (var i=0;i<elems.length;i+=1){
      if(elems[i].style.display == 'block'){
        elems[i].style.display = 'none';
      }else{
        elems[i].style.display = 'block';
      }
    }
  }
</script>
<button onClick="displayRow()" >Show / Hide</button>

First, your code will generate multiple rows with the same id (Yes or No). 首先,您的代码将生成具有相同ID(是或否)的多行。 In HTML, there can be only one id, so maybe change this to class instead of id. 在HTML中, 只能有一个 id,因此可以将其更改为class而不是id。

Second, I wouldn't "hide" the row via code. 其次,我不会通过代码“隐藏”该行。 If you are adding a class use CSS to hide the rows with the appropriate class. 如果要添加类,请使用CSS隐藏相应类的行。

Given: HTML 鉴于: HTML

<table>
    <tr>
        <td>Visible Data</td>
    </tr>
    <tr class='dontshow'>
        <td>Invisible Data</td>
    </tr>
    <tr class='hidethis'>
        <td>Visible Data, can change</td>
    </tr>
    <tr class='hidethis'>
        <td>Visible Data, can change</td>
    </tr>
</table>
<button id="hide">Hide</button>
<button id="show">Show</button>

And, CSS 而且, CSS

.dontshow {
    display:none;
}

Try this to hide a row ... 试试这个隐藏行...

var rows = document.getElementsByClassName("hidethis");
for (var i = 0; i < rows.length; i++) {
    rows[i].setAttribute("class", "hidethis dontshow");
}

... and something like this to display a row ... ...这样的东西显示一行...

var rows = document.getElementsByClassName("hidethis");
for (var i = 0; i < rows.length; i++) {
    rows[i].setAttribute("class", "hidethis");
}

jsFiddle: http://jsfiddle.net/rfornal/o633c360/ jsFiddle: http : //jsfiddle.net/rfornal/o633c360/

I used jQuery in this example, SIMPLY to enable the click events faster. 在本示例中,我使用jQuery SIMPLY来更快地启用单击事件。 You can just as easily use the onclick used in your code. 您可以轻松地使用代码中使用的onclick。

<script type="text/javascript">
function displayRow(){
    var list = document.getElementsByClassName('NO');

    for (var i = 0; i < list.length; i++) {
        if (list[i].style.display !== "none") {
           list[i].style.display = "none";
       }
       else {
           list[i].style.display = "inline-block";
       }
   }
}
</script>

<?php
    $prices = array ("20", "56", "33", "12", "66", "25", "55");
    echo "<table border='1'>";
    foreach ($prices as $price) {
       if($price >= 30.00) {
         $status = "YES";
       }else if ($price < 30.00) {
         $status = "NO";
       }
       echo "<tr class='$status' style="display:inline-block"><td>$price</td></tr>";
    } 
    echo "</table>";
?>

<button onclick="displayRow()" >Show / Hide</button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM