0

我的网站上有一个页面,可让用户查看其项目的数据库。它有一个非常简单的格式:

条码 - 产品名称 - 品牌名称 - 重量

就目前而言,一些用户要求能够单独搜索每一列。例如,如果 3 个样本条目是:

0000001 - macbookPro - 苹果 - 1.5kg

0000002 - macbookAir - 苹果 - 1.5kg

0000003 - 苹果 - Granny Smith - 200g

我希望能够单独查询每一行(甚至可能按字母顺序排序)。因此,如果我转到 productName 文本输入字段并输入“apple”,那么第 1 行和第 2 行将暂时消失,只剩下我剩下的行。

我附上了我的示例 js 和 html 代码,还附上了一张图片:

桌子的图片

HTML:

<body>
    <div id="header"></div>
    <div id="content"></div><!-- This is where the content will be loaded -->


    <table class="contenttable" style = "width: 40%;">
        <tr id="searchBoxes">
            <td><input type="text" placeholder="Search"></td><!-- Searches barcodeID -->
            <td><input type="text" placeholder="Search"></td><!-- Searches ProductName -->
            <td><input type="text" placeholder="Search"></td><!-- Searches BrandName -->
            <td><input type="text" placeholder="Search"></td><!-- Searches Weight -->

        </tr>
        <tr id="columnNames">
            <td><b>BarcodeID</b></td>
            <td><b>ProductName</b></td> 
            <td><b>BrandName</b></td>
            <td><b>Weight</b></td>
        </tr>
        <tr id="final_row"></tr>
        <div class="error" id="display_error"></div>
    </table>

</body>

JS代码:

$(document).ready(function(){
$.get("../php/security.php", function(response){
        if(response.result == "failure"){
            location.href='../user_login.html';
        } else {
            $("#header").load("../header_logout.html");
            //from here down is the relevant code
            $.post("../php/item_database.php", {email1:response.data.authUser}, function(indata){
                indata.items.forEach(function(element){
                    $BarcodeID = element.BarcodeID;
                    $ProductName = element.ProductName;
                    $BrandName = element.BrandName;
                    $Weight = element.Weight;
                    $row = "<tr class='row'><td class='rowbarcode'>" + $BarcodeID + "</td>" + "<td>" + $ProductName + "</td>" + "<td>" + $BrandName + "</td>" + "<td>" + $Weight + "</td>" + "<td>" + "<button class='delete'>Delete</button>" + "</td></tr>";
                    $("#final_row").before($row);
                });
            }, "json");//eo post
        } //eo else
 }, "json"); //eo get

$(".contenttable").on('click', '.delete', function(){
    var BarcodeID = $(this).closest('tr').find('.rowbarcode').text(); //get barcode of row to delete
    console.log(BarcodeID);
    //send barcode ID and UserID to removescan.php to delete from DB
});

});//eof
4

1 回答 1

0

所以,万一有人感兴趣,当使用事件绑定的 JS 技术时,像 JQuery-datatables 这样的 jquery 插件是一件麻烦事。因此,这里是一个纯 JS 解决方案,其中行会根据您在搜索框中输入的内容而消失。删除功能仍然可以完美运行。

JS代码:

$("#search").keyup(function () {
var value = this.value.toLowerCase().trim();

$("table tr").each(function (index) {
    if (!index) return;
    $(this).find("td").each(function () {
        var id = $(this).text().toLowerCase().trim();
        var not_found = (id.indexOf(value) == -1);
        $(this).closest('tr').toggle(!not_found);
        return not_found;
    });
});

图片: 没有搜索

搜索

于 2016-11-21T14:40:58.450 回答