0

我正在使用此代码过滤搜索。我试图只使用 .filter 类来获取我想要过滤的 2 列。问题是脚本现在要求文本在两列中都匹配,以便它过滤结果。如何使它从任一列中找到结果,而不是从两者中找到结果?

$(document).ready(function() {
    var $rows = $('#orders tbody .filter');
    $('#filtersearch').keyup(function() {
        var val = $.trim($(this).val()).replace(/ +/g, ' ').toLowerCase();

        $rows.closest('tr').show().end().filter(function() {
            var text = $(this).text().replace(/\s+/g, ' ').toLowerCase();
            return !~text.indexOf(val);
        }).closest('tr').hide();
    });
    $('#filtersearch').keyup();
});

以及带有数据的表格

<table border="0" align="center" cellpadding="5" cellspacing="0" id="orders">
<thead>
<tr>
<th colspan="6">
            <form name"searchfilter">
            <input type="text" id="filtersearch" name="filtersearch" placeholder="Search by name or phone number" value="<?php echo $search; ?>">
            </form>
            </th>
</tr>
<tr>
<th width="15%">Date</th>
<th width="30%">Name</th>
<th width="30%">Phone</th>
<th width="15%">Location</th>
<th width="10%">Picked Up</th>
</tr>
</thead>
<tbody>
<?php
/* 
        VIEW.PHP
        Displays all data from 'players' table
*/

        // connect to the database
        include('connect-db.php');


        // get results from database
        $result = mysql_query("SELECT * FROM orders") 
                or die(mysql_error());  


        // loop through results of database query, displaying them in the table
        while($row = mysql_fetch_array( $result )) {
     $src = '';
     switch( $row['Location'] ) {
          case '1':
              $src = 'images/rear_icon.png';
              break;
          case '2':
              $src = '2.jpg';
              break;
          default:
              $src = 'default.jpg';

            }      // echo out the contents of each row into a table
                echo "<tr>";
                echo '<td>' . $row['date'] . '</td>';
                echo '<td class="filter">' . $row['Name'] . '</td>';
                echo '<td class="filter">' . $row['Phone'] . '</td>';
                echo '<td><img src="'.$src.'"></td>';
                echo '<td><a href="delete.php?id=' . $row['id'] . '"><img src="images/icons/checkmark.png"></a></td>';
                echo "</tr>"; 
        } 


?>
</tbody>
</table>
4

1 回答 1

0

你可以更换

    $rows.closest('tr').show().end().filter(function() {
        var text = $(this).text().replace(/\s+/g, ' ').toLowerCase();
        return !~text.indexOf(val);
    }).closest('tr').hide();

   $rows.closest('tr').hide().each(function(){
       if ($('td', this).filter(function(){
            var text = $(this).text().replace(/\s+/g, ' ').toLowerCase();
            return text.indexOf(val)!=-1; // too much wine to play with ~ things
       }).length>0) $(this).show();
   })

个人评论:命名$rows单元格不会使代码更易于阅读和修复。

于 2013-03-28T21:12:15.807 回答