【问题标题】:Can I change the <td> background if a field result is great or equal to another field?如果字段结果大于或等于另一个字段,我可以更改 <td> 背景吗?
【发布时间】:2012-06-01 10:00:45
【问题描述】:

我在 html 表中显示 mySQL。

我想将 $qty 的 TD 背景颜色更改为红色IF $qty &gt;= $max or $qty =&lt; $min

有没有一种简单的方法可以用 jQuery 或 PHP 做到这一点?

我为示例简化了表格和 PHP:

<table id="tablesorter-demo">
<tr><td>'.$min.'</td><td>'.$max.'</td><td>'.$qty.'</td></tr>
</table>

【问题讨论】:

  • 当然,在 php 中使用 if else 语句

标签: php jquery html-table background-color tablesorter


【解决方案1】:

我知道你有你的答案,但最好只编写一个自定义表格排序器小部件来突出显示错误数量的表格单元格。这是demo 和代码:

$.tablesorter.addWidget({
    id : "qty",
    format: function(table){
        var i, $td, cur,
            c = table.config,
            cols = c.widgetQty,
            $tr = $(table).children('tbody').children('tr'),
            rows = $tr.length;
        for (i = 0; i < rows; i++){
            $td = $tr.eq(i).find('td');
            cur = parseInt( $td.eq(cols[2]).text(), 10); // current
            if (cur <= parseInt( $td.eq(cols[0]).text(), 10) || // min
                cur >= parseInt( $td.eq(cols[1]).text(), 10) ){ // max
                $td.eq(cols[2]).addClass('badqty');
            }
        }
    }
});

$('table').tablesorter({
    widgets : [ 'zebra', 'qty' ],
    widgetQty : [ 0, 1, 2 ] // min, max, current qty column indexes 
});​

【讨论】:

    【解决方案2】:
    var min = $('table tr td:eq(0)').text();
    var max = $('table tr td:eq(1)').text();
    var qty = $('table tr td:eq(2)').text();
    
    if (qty >= max || qty <= min ) {
       $('table tr td:eq(2)').css('background-color', 'red');
    }
    

    http://jsfiddle.net/7vUFS/3/

    【讨论】:

    • 如果 (qty >= max || qty =
    • 感谢尝试,但我可以在红色背景中启动
    • $o .= ' '.$type.' '.$part_no.' '.$description. ' '.$artwork.'     链接   '.number_format($min).' '.number_format($max).' '.number_format($qty).' ';}
    • 这是我的 tr 行 php
    • 我遇到了与 table-sorter 脚本的冲突。这一定是问题所在。
    【解决方案3】:
    <?php
       $class = (($qty >= $max) || ($qty <= min)) ? ' class="red"' : '';
    ?>
    
    <tr><td>....</td><td<?php echo $class ?>><?php echo $qty ?></td></tr>
    

    【讨论】:

      猜你喜欢
      • 2022-10-15
      • 2020-08-19
      • 1970-01-01
      • 1970-01-01
      • 2021-11-20
      • 1970-01-01
      • 1970-01-01
      • 2017-05-26
      • 2017-02-02
      相关资源
      最近更新 更多