Can I change the <td> background if a field result is great or equal to another field?

柔情痞子 提交于 2019-12-30 11:01:13

问题


I am displaying mySQL in an html table.

I would like to change the TD background color of $qty to red IF $qty >= $max or $qty =< $min.

Is there a simple a way to do this with jQuery or PHP?

I simplified my table and PHP for my example:

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

回答1:


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/




回答2:


<?php
   $class = (($qty >= $max) || ($qty <= min)) ? ' class="red"' : '';
?>

<tr><td>....</td><td<?php echo $class ?>><?php echo $qty ?></td></tr>



回答3:


I know you have your answer, but it would be better to just write a custom tablesorter widget that highlights the bad quantity table cells. Here is a demo and the code:

$.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 
});​


来源:https://stackoverflow.com/questions/10760870/can-i-change-the-td-background-if-a-field-result-is-great-or-equal-to-another

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!