在表格行中搜索COLUMN和HIGHLIGHT中的特定文本

时间:2016-07-28 08:44:26

标签: javascript php html

我从数据库中检索信息,然后创建一个表。 我有一个列名Status,几乎所有的值都等于' A'。 虽然有一些值等于' C'。我想要状态列等于' C'背景颜色为红色。

我的代码似乎不起作用,我认为是javascript部分。

欢迎任何帮助。

我的代码是这样的:     

<thead>

   <tr>
        <th>Dongle</th>
        <th>ActDate</th>
        <th>ModDate</th>
        <th>Client</th>
        <th>Company</th>
        <th>Activation</th>
        <th>Comments</th>
        <th>Status</th>
   </tr>

</thead>

<SCRIPT type="text/javascript">

$(document).ready(function(){
    $('table tr').each(function(){
       if($(this).find('td').eq(3).text() == 'C'){
          $(this).css('background','red');
       }
    });
});

</SCRIPT>

<?php

$queryString = $_SESSION['clientid'];

$Server = "something\sqlexpress";
$user = "some";
$password = "thing";

$database = "Something";
$connectionInfo = array( "Database"=>$database,"UID"=>$user, "PWD"=>$password);
$link = sqlsrv_connect($Server, $connectionInfo);

if ($link === false) {
    echo "Connection failed. \n";
    die(print_r(sqlsrv_errors(), true));
}                                   

$Reseller = $_SESSION["Seller"];

$strSQLSel= "SELECT Dongle AS Dongle, ActDate AS ActDate, 
             ModDate AS    ModDate, Client AS Client, Company AS Company, 
             Activation AS Activation,
             Comments AS Comments, Status AS Status
             FROM Licenses
             WHERE Reseller = '$Reseller' 
             GROUP BY Dongle,ActDate,ModDate,Client,Company,Activation,Comments,Status";

$result = sqlsrv_query($link,$strSQLSel);
While ($row = sqlsrv_fetch_array($result,SQLSRV_FETCH_ASSOC)){

?>

<tbody>
    <tr>
      <td><p><?php echo $row['Dongle']; ?></p></td>
      <td><p><?php echo date_format($row['ActDate'],'Y-m-d'); ?></p></td>
      <td><p><?php echo date_format($row['ModDate'],'Y-m-d'); ?></p></td>
      <td><p><?php echo $row['Client']; ?></p></td>
      <td><p><?php echo $row['Company']; ?></p></td>
      <td><p><?php echo $row['Activation']; ?></p></td>
      <td><p><?php echo $row['Comments']; ?></p></td>
      <td><p><?php echo $row['Status']; ?></p></td>
    </tr>

<?php
    }   
?>

</tbody>
</table>

4 个答案:

答案 0 :(得分:0)

将您的代码更改为

    <tbody>
while ($row = sqlsrv_fetch_array($result,SQLSRV_FETCH_ASSOC)){
$cls = ($row['Activation']=='C') ? "style='background-color:red'" : "";
?>


    <tr <?php echo $cls;?>>
      <td><p><?php echo $row['Dongle']; ?></p></td>
      <td><p><?php echo date_format($row['ActDate'],'Y-m-d'); ?></p></td>
      <td><p><?php echo date_format($row['ModDate'],'Y-m-d'); ?></p></td>
      <td><p><?php echo $row['Client']; ?></p></td>
      <td><p><?php echo $row['Company']; ?></p></td>
      <td><p><?php echo $row['Activation']; ?></p></td>
      <td><p><?php echo $row['Comments']; ?></p></td>
      <td><p><?php echo $row['Status']; ?></p></td>
    </tr>

<?php
    }   
?>

答案 1 :(得分:0)

尝试:

<tr style='<?php echo ($row["Status"] == "C") ? "background-color: red" : "" ?>'>
    <td><p><?php echo $row['Dongle']; ?></p></td>
    <td><p><?php echo date('Y-m-d', $row['ActDate']); ?></p></td>
    <td><p><?php echo date('Y-m-d', $row['ModDate']); ?></p></td>
    <td><p><?php echo $row['Client']; ?></p></td>
    <td><p><?php echo $row['Company']; ?></p></td>
    <td><p><?php echo $row['Activation']; ?></p></td>
    <td><p><?php echo $row['Comments']; ?></p></td>
    <td><p><?php echo $row['Status']; ?></p></td>
</tr>

答案 2 :(得分:0)

CSS:

tr.red-status {background-color: red;}

PHP模板,表格tbody:

<tbody>
    <?php
    $result = sqlsrv_query($link, $strSQLSel);
    while ($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) :
    ?>
        <tr class="<?php echo ($row['Status'] == 'C') ? 'red-status' : '' ?>">
            <td><p><?php echo $row['Dongle']; ?></p></td>
            <td><p><?php echo date_format($row['ActDate'],'Y-m-d'); ?></p></td>
            <td><p><?php echo date_format($row['ModDate'],'Y-m-d'); ?></p></td>
            <td><p><?php echo $row['Client']; ?></p></td>
            <td><p><?php echo $row['Company']; ?></p></td>
            <td><p><?php echo $row['Activation']; ?></p></td>
            <td><p><?php echo $row['Comments']; ?></p></td>
            <td><p><?php echo $row['Status']; ?></p></td>
        </tr>
    <?php endwhile; ?>
</tbody>

答案 3 :(得分:0)

EASY TO USE AND VERY SIMPLE:   

 $(document).ready(function () {
            $(".searchCls").val("");
            //Script to search item group in table
            $('.searchCls').keyup(function () {
                var inputval = $(this).val();
                var table = $('#WBSTreeList');
                var search = $('.searchCls');
                if(inputval != '' && inputval != null){
                table.find('tr').each(function (index, row) {
                    var allCells = $(row).find('td');
                    if (allCells.length > 0) {
                        var found = false;
                            allCells.each(function (index, td) {
                                var regExp = new RegExp(inputval, 'i');
                                if (regExp.test($(td).text())) {
                                    found = true;
                                    return false;
                                }
                            });
                            if (found == true) {
                                $(row).show().addClass('match');
                            }

                            else {
                                $(row).hide();
                            }
                    }
                });
                }
                else {
                    table.find('tr').each(function (index, row) {
                        $(row).show().removeClass('match');
                    });
                }
            });
        });

    HTML:
    <input type="text" id="searchtxt" class="searchCls width-100" placeholder="Search ...">

    <div class="col-sm-12 col-xs-12 table-responsive">
        <table id="WBSTreeList" class="table table-bordered width-100">
            <thead>
                <tr>
                    <th></th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
    <tr>
    <td>
    </td>
    </tr>
    </tbody>
    </table>
    </div>