Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter cards in Bootstrap 4 with jQuery

I am attempting to create a page that is populated by many cards, using bootstrap 4's new card component.

I want to create a search bar, that when searched, filters out cards whose titles don't match the search query.

Here is a plunker of what I have in mind. Plunker

I would like the cards to get something like a display: none, or opacity:0 if they don't match.

I currently am attempting to write a function that onChange of the search bar does this. I'll post if I can get it figured out.

I've tried to use the built in snippet feature as well.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script src="https://cdn.rawgit.com/twbs/bootstrap/v4-dev/dist/js/bootstrap.js"></script>
<link href="https://cdn.rawgit.com/twbs/bootstrap/v4-dev/dist/css/bootstrap.css" rel="stylesheet" />

<div class="container">
  <div class="row">
    <div class="col-sm-4">
      <input type="search" placeholder="Search......" name="search" class="searchbox-input" onkeyup="buttonUp();" required>
    </div>
    <div class="col-sm-4">
    </div>
    <div class="col-sm-4">
    </div>
  </div>
  <div class="card-columns">
    <div class="card">
      <div class="card-block">
        <h4 class="card-title">Card title that wraps to a new line</h4>
        <p class="card-text">This is a longer card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
      </div>
    </div>
    <div class="card card-block">
      <blockquote class="card-blockquote">
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer posuere erat a ante.</p>
        <footer>
          <small class="text-muted">
          Someone famous in <cite title="Source Title">Source Title</cite>
        </small>
        </footer>
      </blockquote>
    </div>
    <div class="card">
      <div class="card-block">
        <h4 class="card-title">Card title</h4>
        <p class="card-text">This card has supporting text below as a natural lead-in to additional content.</p>
        <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small>
        </p>
      </div>
    </div>
    <div class="card card-block card-inverse card-primary text-xs-center">
      <blockquote class="card-blockquote">
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer posuere erat.</p>
        <footer>
          <small>
          Someone famous in <cite title="Source Title">Source Title</cite>
        </small>
        </footer>
      </blockquote>
    </div>
    <div class="card card-block text-xs-center">
      <h4 class="card-title">Card title</h4>
      <p class="card-text">This card has supporting text below as a natural lead-in to additional content.</p>
      <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small>
      </p>
    </div>
    <div class="card">
    </div>
    <div class="card card-block text-xs-right">
      <blockquote class="card-blockquote">
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer posuere erat a ante.</p>
        <footer>
          <small class="text-muted">
          Someone famous in <cite title="Source Title">Source Title</cite>
        </small>
        </footer>
      </blockquote>
    </div>
    <div class="card card-block">
      <h4 class="card-title">Card title</h4>
      <p class="card-text">This is a wider card with supporting text below as a natural lead-in to additional content. This card has even longer content than the first to show that equal height action.</p>
      <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small>
      </p>
    </div>
  </div>
</div>
like image 534
Xgongiveittoya Avatar asked Apr 04 '16 19:04

Xgongiveittoya


3 Answers

Here's a quick example of how you could do it using jQuery's contains selector:

$('.searchbox-input').change( function () {
    $('.card').show();
    var filter = $(this).val(); // get the value of the input, which we filter on
    $('.container').find(".card-title:not(:contains(" + filter + "))").parent().css('display','none');
});

Currently this is set up to happen on change of the search input, you would probably want set up a submit button and have it fire on submit instead.

Bootply Example

like image 82
APAD1 Avatar answered Sep 21 '22 13:09

APAD1


Here are a few more modern options that are Bootstrap 4 or Bootstrap 5 friendly...

Bootstrap 5 (using JavaScript)

var buttonUp = () => {
    const input = document.querySelector(".searchbox-input");
    const cards = document.getElementsByClassName("card");
    let filter = input.value
    for (let i = 0; i < cards.length; i++) {
        let title = cards[i].querySelector(".card-body");
        if (title.innerText.indexOf(filter) > -1) {
            cards[i].classList.remove("d-none")
        } else {
            cards[i].classList.add("d-none")
        }
    }
}

Demo

Bootstrap 4 (using jQuery)

// this overrides `contains` to make it case insenstive
jQuery.expr[':'].contains = function(a, i, m) {
  return jQuery(a).text().toUpperCase()
      .indexOf(m[3].toUpperCase()) >= 0;
};

var buttonUp = () => {
    $('.card').removeClass('d-none');
    var filter = $(this).val(); // get the value of the input, which we filter on
    $('.card-deck').find('.card .card-body h4:not(:contains("'+filter+'"))').parent().parent().addClass('d-none');
}

Demo

like image 34
Zim Avatar answered Sep 23 '22 13:09

Zim


Here is the simple solution.

$(document).ready(function(){
  $('.searchbox-input').on("keyup", function() {
    var value = $(this).val().toLowerCase();
    $(".card").filter(function() {
      $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
    });
  });
});

like image 30
Daniel Wear Avatar answered Sep 22 '22 13:09

Daniel Wear