-2
\$\begingroup\$

I have this search script on my page which search through the displayed table. The table is displayed with PHP from a database. It is actually a list, and this list is sorted out alphabetically with the help of hyperlinks of A to Z. Now I need to search the whole database instead of displayed ones. Any helps or resources would be helpful. :)

$(document).ready(function() {

    $("#search").keyup(function() {

        if($(this).val() != "") {

            $("#some_table tbody>tr").hide();
            $("#some_table td:contains-ci('" + $(this).val() + "')").parent("tr").show();
        } else {
            $("#some_table tbody>tr").show();
        }
    });
});

$.extend($.expr[":"], {
    "contains-ci" : function(elem, i, match, array) {
        return (elem.textContent || elem.innerText || $(elem).text() || "").toLowerCase().indexOf((match[3] || "").toLowerCase()) >= 0;
    }
});
\$\endgroup\$
1
  • \$\begingroup\$ It seems like you are looking for someone to write some code for you rather than receive a review of what you have written. Code Review is for working code (it is not the right place for this sort of question). Also, your question is vague and unanswerable in its current form regardless of where it is posted. \$\endgroup\$ – Paul Apr 13 '12 at 4:12
2
\$\begingroup\$

I am doing a review of the code, not answering your question which should be on stackoverflow and not codereview.

//$( function(){} ) is a short cut for ready!
$(function() {
   //No reason to keep looking this up on every keypress, store it once and reuse
    var tableRows = $("#some_table tbody>tr");
    $("#search").keyup(
        function() {        
            //Grab the value and remember it so we do not have to look up again
            var value = $(this).value;            
            if ( value.length > 0) {
                //Chain the selectors, the tds live inside the rows so just do a find based on that starting point
                tableRows.hide().find("td:contains-ci('" + value + "')").parent("tr").show();
            } else {
                tableRows.show();
            }
        }
    );
});
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.