Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twitter Bootstrap Row Filter / Search Box

I'm having trouble finding a tutorial on how to create a simple search query, or row filter, for Twitter Bootstrap. I've tried many, I'm not sure if I'm doing something wrong or the plugins are not compatible with Bootstrap. Please help if you can.

I've tried:

$(document).ready(function() {
 //Declare the custom selector 'containsIgnoreCase'.
      $.expr[':'].containsIgnoreCase = function(n,i,m){
          return jQuery(n).text().toUpperCase().indexOf(m[3].toUpperCase())>=0;
      };

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

          $("#tabela").find("tr").hide();
          var data = this.value.split(" ");
          var jo = $("#tabela").find("tr");
          $.each(data, function(i, v){

               //Use the new containsIgnoreCase function instead
               jo = jo.filter("*:containsIgnoreCase('"+v+"')");
          });

          jo.show();

      }).focus(function(){
          this.value="";
          $(this).css({"color":"black"});
          $(this).unbind('focus');
      }).css({"color":"#C0C0C0"});
});

Nothing with this... Maybe I'm missing any "id" on my table or search box, I'm new with this.

like image 726
user2044129 Avatar asked Feb 05 '13 17:02

user2044129


People also ask

How do you filter in Bootstrap?

Bootstrap Filters. Bootstrap does not have a component that allows filtering. However, we can use jQuery to filter / search for elements.

What is search box in Bootstrap 5?

Bootstrap 5 Search component The search box is an UI element prepared for creating search engines. Its most important element is search input, but it can also contain icons or buttons. It is also adjusted to be used in various other components such as navbar, dropdown, table, select, sidenav and many more.

How do I make a search bar in Bootstrap?

You can use the code script to develop your own custom search bar. This is a bootstrap search box example for a mobile app. Making the search box easier to access is very important in mobile UI designing. The creator has placed the search bar at the top right corner and used a swift animation to show the search bar.

How to filter/search for elements using jQuery?

However, we can use jQuery to filter / search for elements. Type something in the input field to search the table for first names, last names or emails: Example explained: We use jQuery to loop through each table rows to check if there are any text values that matches the value of the input field.


2 Answers

Here's what I use:

$('input.filter').live('keyup', function() {
    var rex = new RegExp($(this).val(), 'i');
    $('.searchable tr').hide();
        $('.searchable tr').filter(function() {
            return rex.test($(this).text());
        }).show();
    });

To use it, you just create a table, with a tbody with the class "searchable" and then an input with class "filter" somewhere on your page (I prefer to put them in a Bootstrap Popup behind a search icon).

like image 147
Filipp Lepalaan Avatar answered Oct 21 '22 09:10

Filipp Lepalaan


This is live example of solution provided by Filipp Lepalaan. Many thanks for this small and perfect code.

Example

$(document).ready(function () {

(function ($) {

    $('#filter').keyup(function () {

        var rex = new RegExp($(this).val(), 'i');
        $('.searchable tr').hide();
        $('.searchable tr').filter(function () {
            return rex.test($(this).text());
        }).show();

    })

}(jQuery));

});

like image 38
GiorgiTBS Avatar answered Oct 21 '22 09:10

GiorgiTBS