Dynamically created table queries

随声附和 提交于 2019-12-13 08:07:31

问题


I have a page on my site which lets users see a database of their items. It has a pretty simple format:

Barcode - ProductName - BrandName - Weight

As it stands at the moment some users are asking for the ability to search each column individually. for example, if 3 sample entries were:

0000001 - macbookPro - Apple - 1.5kg

0000002 - macbookAir - Apple - 1.5kg

0000003 - Apple - Granny Smith - 200g

I would like the ability to be able to query each row individually (and maybe even sort them alphabetically). So if i went to the productName text input field and typed 'apple' then rows 1 & 2 would temporarily disappear leaving me only with my remaining row.

I have attached my sample js & html code and also attached a picture:

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 CODE:

$(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

回答1:


So, just incase anyone is interested, when using the JS technique of event binding, jquery plugins like JQuery-datatables is a nuisance. So, instead here is a pure JS solution where rows disappear depending on what input you enter into the search box. The delete functionality still works perfectly.

JS CODE:

$("#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;
    });
});

Pictures:



来源:https://stackoverflow.com/questions/40704573/dynamically-created-table-queries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!