【问题标题】:Changing td's background color to success or danger将 td 的背景颜色更改为成功或危险
【发布时间】:2015-12-07 16:38:40
【问题描述】:

这是我的 php,如果订单是 delivered,我希望这个 td (order_status) 将背景颜色更改为 green or success,如果订单是 canceled,则将背景颜色更改为 danger or red

<?php
    if(!session_id()){
        session_start();
    }
    include_once '../fileadmin/dbinit.php';
    $todo = $_POST['todo'];
    $con = mysql_connect("localhost","root","","atec_coop");
    if (!$con){
        die("Can't connect".mysql_error());
    }
    mysql_select_db("atec_coop",$con);
    switch ($todo) {
        case "display":
            $sql = "SELECT * from tb_empgroc_master";
            $result = $atecCoop->query($sql);

            $html = ''; $ctr = 0;
            if ($result->num_rows){
                while ($row = $result->fetch_object()){
                $id = $row->empgrocmstID;
                $date_ordered = date("m-d-Y");
                $order_no = date($row->order_no);
                $total_items = number_format($row->total_items);
                $total_amount = number_format($row->total_amount,2);
                $order_status = wordwrap($row->order_status);

                $html .= "<tr id='$id'>";
                $html .= "<td class='date_ordered' style='text-align:center'>$date_ordered</td>";
                $html .= "<td class='order_no' style='text-align:center'>$order_no</td>";
                $html .= "<td class='total_items' style='text-align:right'>$total_items</td>";
                $html .= "<td class='total_amount' style='text-align:right'>$total_amount</td>";
                $html .= "<td class='order_status' style='text-align:center'>$order_status</td>";
                $html .= "</tr>";
                }
            }
            echo $html;
        break;
        case "Cancel":
            $Cancelquery = "UPDATE tb_empgroc_master SET order_status='Cancelled' WHERE empgrocmstID='".$_POST['empgrocmstID']."'";
            mysql_query($Cancelquery, $con);
        break;
        case "Approve":
            $Approvequery = "UPDATE tb_empgroc_master SET order_status='Delivered' WHERE empgrocmstID='".$_POST['empgrocmstID']."'";
            mysql_query($Approvequery, $con);
        break;
    }
?>

这是我的表格

<form class="form-horizontal" id="main-form" action="PHP_groceryReleasingProcess.php" method="POST">
    <table class="tablesorter table table-bordered table-condensed" id="cLoanOut" style="table-layout: fixed;">
        <colgroup>
            <col width="110">
            <col width="130">
            <col width="50">
            <col width="60">
            <col width="90">
        </colgroup>
        <thead>
            <tr>
                <th>Date Ordered</th>
                <th>Order No.</th>
                <th>Total Item(s)</th>
                <th>Total Amount</th>
                <th>Order Status</th>
            </tr>
        </thead>
        <tbody>
        </tbody>
    </table>
    <button id="Approve" role="button" class="btn btn-success" disabled>Approve Order</button>
    <button id="Cancel" role="button" class="btn btn-danger" disabled>Cancel Order</button>
</form>

还有我的 javacript ajax 调用

$("#Approve").click(function(e) {
    e.preventDefault();
    var id = $('#cLoanOut tr.active').attr('id');
    bootbox.confirm("Are you sure you want to approve order?","No","Yes",function(r){
        if(r) {
            $.ajax({  
                url : "<?php echo $server_name; ?>/emcis_coopmain/process/PHP_groceryReleasingProcess.php",
                type : "POST",
                async : false,
                data : {
                    empgrocmstID:id,
                    todo:"Approve"
                },
                success:function(result){
                    bootbox.alert('Order Approved',function(){
                    $("#Approve").attr("disabled", true);
                    });
                    updateTable();
                }
            });   
        } else {

        }
    });
});
$("#Cancel").click(function(e) {
    e.preventDefault();
    var id = $('#cLoanOut tr.active').attr('id');
    bootbox.confirm("Are you sure you want to cancel order?","No","Yes",function(r){
        if(r) {
            $.ajax({
                url : "<?php echo $server_name; ?>/emcis_coopmain/process/PHP_groceryReleasingProcess.php",
                type : "POST",
                async : false,
                data : {
                    empgrocmstID:id,
                    todo:"Cancel"
                },
                success:function(result){
                    bootbox.alert("Order Cancelled",function(){
                    $("#Cancel").attr("disabled", true);
                    });
                    updateTable();
                }
            });   
        } else {

        }
    });
});

如果我点击Approve Order buttonorder_status 的数据,即 td (Pending) 将更改为Delivered,如果我点击Cancel Order button,它将更改为Cancelled

如果成功,如果订单是approved/delivered,我想将tdbackground color 更改为success/green。如果canceled,将background color改为danger/red

感谢您的帮助,谢谢。

看起来像这样。单击时,每个 tr 都有一个活动类。

Date Ordered         Order No.     Total item(s) Total Amount   Order Status
 09-11-2015      15-09-0000000001        3          213.85        Pending
 09-11-2015      15-09-0000000002        1          130.00       Delivered
 09-11-2015      15-09-0000000003        2          134.07        Pending
 09-11-2015      15-09-0000000004        4          846.41       Cancelled

       <button>Approve Order</button> <button>Cancel Order</button>

我的 updateTable(); 脚本

function updateTable(){
//                $tbody = $('#cLoanOut tbody'),
//                url = $('#main-form').attr('action');
//                $.post("PHP_groceryReleasingProcess.php",{todo:"display"},function(response){
//                    $('.progress').hide();
//                    $tbody.html(response);
//                    $table.trigger('update');
//                },'html');
    var dataString = "todo=display";
    $.ajax({
        type: "POST",
        url: "<?php echo $server_name; ?>/emcis_coopmain/process/PHP_groceryReleasingProcess.php",
        data: dataString,
        success: function(sg){
            $("#cLoanOut tbody").empty();
            $("#cLoanOut").find('tbody').append(sg).trigger('update');
        },
        complete: function(){
            $('.progress').hide();
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            bootbox.alert('Search Failed.');
        }
    });
}

我在表单中添加了 css

.table-hover > tbody > tr > td.danger:hover {
     background-color: red !important;
}
.table-hover > tbody > tr > td.warning:hover {
     background-color: yellow !important;
}
.table-hover > tbody > tr > td.success:hover {
     background-color: green !important;
}

如果我的 td (order_status) 是真的,我如何为 Delivereddanger 打电话给 Cancelled

【问题讨论】:

  • 你能提供你的代码吗?
  • 我已经编辑了我的帖子@WisdmLabs
  • @Micaela 你在result 中对两个 ajax 的成功有什么看法?
  • ahm..如果它被批准,它会将表从待处理更新为已交付,如果它被取消,它将被取消。对不起,如果我不明白你的问题@Jai
  • @Micaela 当你进行 ajax 调用时,你从那个 php 端回显了什么。是sucess 还是cancel

标签: javascript php jquery html css


【解决方案1】:

首先,您需要将特定 id 添加到状态订单的&lt;td id="xxx"&gt;,然后您可以在 jquery 中使用相同的 id 添加相应的背景颜色并将文本更改为“已交付或取消”。

您需要在 ajax 调用成功事件上执行此过程。

$("#xxx").css("background-color", "green");
$("#xxx").css("background-color", "red");

$("#xxx").html("Delivered");
$("#xxx").html("Cancel");

【讨论】:

  • td的颜色不变
  • 如果我将 id 放入我的 td 会出错。 $html .= "&lt;td id="Order" class='order_status danger' style='text-align:center'&gt;$order_status&lt;/td&gt;";
  • 是应该使用单引号 $html .= "$order_status";如果它的工作,请采取步骤接受答案
  • 它可以工作,但它只会改变第一行 td 的颜色。如果我批准了另一行,则第一行会更改
  • 并且颜色因为updateTable()而闪烁;但如果我删除它。颜色保持
【解决方案2】:

在你的 PHP 循环中有这样的东西:

$html .= '<tr id="$id" class="'. ($order_status == 'cancel' ? 'cancel' : 'approved') .'">';

您根据 $order_status 为 TR 设置了一个类。然后在你的 CSS 中:

tr.cancel td {
    background: red;
}
tr.approved td {
    background: green;
}

【讨论】:

  • order_status的数据不见了
【解决方案3】:

您可以简化以下操作,而不是单击两次并使用相同的 ajax:

$("#main-form button").click(function(e) {
    e.preventDefault();
    var $this = $(this); // cache the clicked button here
    var id = $('#cLoanOut tr.active').attr('id');
    bootbox.confirm("Are you sure you want to "+ this.id.toLowerCase() +" order?","No","Yes",function(r){
        if(r) {
            $.ajax({  
                url : "<?php echo $server_name; ?>/emcis_coopmain/process/PHP_groceryReleasingProcess.php",
                type : "POST",
                async : false,
                data : {
                    empgrocmstID:id,
                    todo:this.id // pass the button id here Approve/Cancel
                },
                success:function(result){
                    var msg = result === "Approved" ? "Order Approved" : "Order Cancelled";
                    bootbox.alert(msg, function(){
                       $this.prop("disabled", true); // use prop 
                    });
                    updateTable();
                },
                complete:function(){
                   $('#cLoanOut tr').each(function(){
                      $(this).find('td').last().addClass(function(){
                          if(this.textContent.trim() === "Cancelled"){
                              return "danger";
                          }else if(this.textContent.trim() === "Delivered"){ 
                              return "success";
                          }
                      });
                   });
                }
            });   
        } else {

        }
    });
});

在上面的sn-p中我改变了什么:

  1. 表单上下文中的按钮选择器。 $("#main-form button") 可让您点击两个按钮。
  2. var $this = $(this); 您可以使用一个变量,稍后将在回调中使用该变量,例如 error:fn, success:fn, complete:fn
  3. this.id.toLowerCase() 让您有两个按钮的动态弹出消息。
  4. todo:this.id 我们正在传递点击按钮的 id。
  5. var msg = result === "Approved" ? "Order Approved" : "Order Cancelled"; 如果您从 php 返回特定文本,则此行可用于两个按钮消息。

  6. $this.prop("disabled", true); 这里$this 是我们缓存它并使用.prop() 方法更改任何属性,如disabled, checked etc.

  7. 添加complete 回调以将类添加到 tds。你可以在 sn-p 中看到。

在这个答案中 5 是您可能会遇到一些问题的地方。

【讨论】:

  • 你会编辑你的 cmets 并为我做吗.. 我已经尝试过了,但我不知道如何在 todo:this 中传递按钮 ID,而且我不明白点击的缓存按钮在这里。对不起,我是编程新手
  • 我试过这个todo:this.$('#Approve Cancel') // 对吗?
  • @Micaela this.id 你不需要明确地做任何事情。
  • 未捕获 RangeError: 超出最大调用堆栈大小。
  • ^ 这意味着在您的代码中某处,您正在调用一个函数,该函数又调用另一个函数,依此类推,直到您达到调用堆栈限制。
猜你喜欢
  • 2014-12-25
  • 2020-04-03
  • 1970-01-01
  • 2021-03-30
  • 2014-01-19
  • 2021-10-24
  • 2015-08-05
  • 1970-01-01
  • 2021-08-18
相关资源
最近更新 更多