javascript - 在表格排序器搜索功能上仅显示一个结果

标签 javascript html search filter tablesorter

我创建了一个搜索功能,用于显示表中的数据。但是我只想显示一行作为结果。我想知道是否有人知道任何代码只会显示一行结果,而不是多行。

我当前的 HTML:

<input class="search selectable" placeholder="Find any film, TV series or video game release date..." data-column="0" type="search">
<table class="tablesorter">
<thead>
    <tr>
        <th>Name</th>
        <th>Release date</th>
        <th>Starring</th>
        <th>Genre</th>
        <th>Age rating</th>
    </tr>
</thead>
<tbody>
    <tr>
        <td>The Human Centipede 3 (Final Sequence)</td>
        <td>
            <ul>
                <li>Wednesday 1st July</li>
            </ul>
        </td>
        <td>
            <ul>
                <li>...</li>
            </ul>
        </td>
        <td>
            <ul>
                <li>Action</li>
            </ul>
        </td>
        <td>
            <ul>
                <li>18</li>
            </ul>
        </td>
    </tr>
       </tbody>
 </table>

CSS:

html {
position: fixed;
height: fixed;
background-color: #bc3030;
min-height: fit;
}
table {
width: 663.5px;
}
form {
display: inline-block;
border-radius: 5px;
background-color: #bc3030;
background-image: -ms-linear-gradient(top, #EDE5DB 0%, #DCD0C2 80%);
background-image: -moz-linear-gradient(top, #EDE5DB 0%, #DCD0C2 80%);
background-image: -webkit-linear-gradient(top, #EDE5DB 0%, #DCD0C2 80%);
background-image: linear-gradient(top, #EDE5DB 0%, #DCD0C2 8a0%);
box-shadow: inset 0 1px #F6F2EC, 0 3px 2px #E5E5E5;

}
input {
border: 2px solid #333333;
width: 529px;
height: 50px;
padding-left: 132px;
font-family:"Raleway";
font-size: 20px;
font-weight: normal;
font-weight: bold;
font-weight: 1000;
background: #fff url(http://static.wixstatic.com/media/97c989_1b9f42901df24660a8a3200c1abb366e.png_srz_p_112_61_75_22_0.50_1.20_0.0    0_png_srz) no-repeat 0.1px;
}
td, th {
width: 550px;
border-bottom: 0px solid #bc3030;
color: #000;
padding: 5px 21px;
font-family:"raleway";
font-weight: normal;
font-weight: bold;
font-weight: 10;
font-size: 18px;
}
thead div {
color: #fff;
padding: 0xpx;
margin-left: 0px;
line-height: normal;
border-left: 0px solid #333333;
}
tbody, td {
background: #fff;
}

Javascript:

document.ontouchmove = function (event) {
event.preventDefault();
}

$('table').on('filterEnd', function () {
if (this.config.lastSearch.join('') === '') {
    $(this).children('tbody').children().addClass('filtered');
}
});
$(function () {

var $table = $('table').tablesorter({
    theme: '',
    widgets: ["zebra", "filter"],
    widgetOptions: {
        filter_columnFilters: false,
        filter_saveFilters: true,
        filter_reset: '.reset'
    }
});


$.tablesorter.filter.bindSearch($table, $('.search'));


$('select').change(function () {
    $('.selectable').attr('data-column', $(this).val());
    $.tablesorter.filter.bindSearch($table, $('.search'), false);
});

});

最佳答案

我不建议这样做,因为这不是用户搜索时所期望的。

如果您必须这样做,请使用此代码 ( demo ):

$(function () {
    $('table')
        .tablesorter({
            theme: 'blue',
            widgets: ['zebra', 'filter']
        })
        .on('filterEnd', function(e, config ){
            if ( config.lastSearch.join('') !== '' ) {
                config.$table
                    .find('tbody tr')
                    .not('.filtered') // rows not hidden
                    .filter(':gt(0)') // target rows > first
                    .addClass('filtered'); // hide
            }
        });
});

我还建议添加注释,就像我在演示中所做的那样,告诉用户会发生什么。

关于javascript - 在表格排序器搜索功能上仅显示一个结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31761125/

相关文章:

c# - 深度优先搜索确实找到了传教士和食人者问题的解决方案状态

php - MySQL选择流派问题(php)

javascript - 隐藏容器不会隐藏里面的内容?

javascript - 在 JavaScript 中实现实例成员/方法

HTML 5,我应该告诉我的客户什么?

html - 如何为不同条目创建具有不同背景图像的有序列表 (HTML/CSS)?

javascript - Bootstrap 放大模型内容

javascript - Webpack - 从非模块文件加载函数?

css - 类在 css 中不起作用

javascript - 通过两个标签小部件作为下拉菜单在博客平台中搜索