jQuery Mobile: Filter Reveal

This entry is part 34 of 73 in the series jQuery Mobile

The filter reveal feature makes it easy to build a simple autocomplete with local data.

When a filter has the data-filter-reveal=’true’ attribute, it will auto-hide all the list items when the search field is blank.

Filter Reveal
<!DOCTYPE html>
<html>
<head>
    <link href='http://code.jquery.com/mobile/1.4.1/jquery.mobile-1.4.1.min.css' rel='stylesheet'/>
    <script src='http://code.jquery.com/jquery-1.10.2.min.js'></script>
    <script src='http://code.jquery.com/mobile/1.4.1/jquery.mobile-1.4.1.min.js'></script>
    <title>Upwhere</title>
</head>
<body>
<div data-role='page'>
    <div data-role='header'><h1>Filter Reveal</h1></div>
    <ul data-role='listview' data-filter='true' data-filter-reveal='true' data-filter-placeholder='Search fruits...' data-inset='true'>
        <li><a href='#'>Apple</a></li>
        <li><a href='#'>Banana</a></li>
        <li><a href='#'>Cherry</a></li>
        <li><a href='#'>Cranberry</a></li>
        <li><a href='#'>Grape</a></li>
        <li><a href='#'>Orange</a></li>
    </ul>

    <div data-role='footer' data-position='fixed'>
        <h1>Footer</h1>
    </div>
</div>
</body>
</html>
Try The Code

The data-filter-placeholder attribute can be added to specify the placeholder text for the filter.

In the example above, I have put the text search fruit as the attribute for data-filter-placeholder since the list contains mainly different types of fruits.

jQuery Mobile: Filter Collapsible Set

This entry is part 33 of 73 in the series jQuery Mobile

I have never seen an app that use this UI but if you have thought of a way to use it, here is the code.

Filter Collapsible Set
<!DOCTYPE html>
<html>
<head>
    <link href='http://code.jquery.com/mobile/1.4.1/jquery.mobile-1.4.1.min.css' rel='stylesheet'/>
    <script src='http://code.jquery.com/jquery-1.10.2.min.js'></script>
    <script src='http://code.jquery.com/mobile/1.4.1/jquery.mobile-1.4.1.min.js'></script>
    <title>Codecrawl</title>
</head>
<body>
<div data-role='page'>
    <div data-role='header'><h1>Filterable Collapsible Set</h1></div>
    <form>
            <input data-type='search' id='searchForCollapsibleSet'>
    </form>
        <div data-role='collapsibleset' data-filter='true' data-inset='true' data-input='#searchForCollapsibleSet'>
            <div data-role='collapsible' data-filtertext='Animals'>
                <h3>Animals</h3>
                <ul data-role='listview' data-inset='false'>
                    <li>Cats</li>
                    <li>Dogs</li>
                    <li>Lizards</li>
                    <li>Snakes</li>
                </ul>
            </div>
            <div data-role='collapsible' data-filtertext='Cars'>
                <h3>Cars</h3>
                <ul data-role='listview' data-inset='false'>
                    <li>Acura</li>
                    <li>Audi</li>
                    <li>BMW</li>
                    <li>Cadillac</li>
                </ul>
            </div>
            <div data-role='collapsible' data-filtertext='Planets'>
                <h3>Planets</h3>
                <ul data-role='listview' data-inset='false'>
                    <li>Earth</li>
                    <li>Jupiter</li>
                    <li>Mars</li>
                    <li>Mercury</li>
                </ul>
            </div>
        </div>

    <div data-role='footer' data-position='fixed'>
        <h1>Footer</h1>
    </div>
</div>
</body>
</html>
Try The Code

To use filterable collapsible set, we use the attribute data-role=’collapsibleset’.

The input box needs to have data-type=’search’ and its id is pointing towards the collapsible set.

jQuery Mobile: Table Filter

This entry is part 32 of 73 in the series jQuery Mobile

You are not limited to using filters on listviews.

To create a filter for a table, set data-filter=’true’ on the table element to generate a filter for table rows.

Table Filter
<!DOCTYPE html>
<html>
<head>
    <link href='http://code.jquery.com/mobile/1.4.1/jquery.mobile-1.4.1.min.css' rel='stylesheet'/>
    <script src='http://code.jquery.com/jquery-1.10.2.min.js'></script>
    <script src='http://code.jquery.com/mobile/1.4.1/jquery.mobile-1.4.1.min.js'></script>
    <title>Codecrawl</title>
</head>
<body>
<div data-role='page'>
    <div data-role='header'><h1>Filterable Table</h1></div>
    <br>

    <form>
            <input id='filterTable-input' data-type='search'>
    </form>
    <table data-role='table' data-filter='true' data-input='#filterTable-input' class='ui-responsive'>
            <thead>
            <tr>
                    <th data-priority='1'>Rank</th>
                    <th data-priority='persist'>Movie Title</th>
                    <th data-priority='2'>Year</th>
                    <th data-priority='3'>Rating</th>
                    <th data-priority='4'>Reviews</th>
                </tr>
            </thead>
                <tbody>
                <tr>
                        <th>1</th>
                        <td><a href='http://en.wikipedia.org/wiki/Citizen_Kane' data-rel='external'>Citizen Kane</a></td>
                        <td>1941</td>
                        <td>100%</td>
                        <td>74</td>
                    </tr>
                <tr>
                        <th>2</th>
                        <td><a href='http://en.wikipedia.org/wiki/Casablanca_(film)' data-rel='external'>Casablanca</a></td>
                        <td>1942</td>
                        <td>97%</td>
                        <td>64</td>
                    </tr>
                <tr>
                        <th>3</th>
                        <td><a href='http://en.wikipedia.org/wiki/The_Godfather' data-rel='external'>The Godfather</a></td>
                        <td>1972</td>
                        <td>97%</td>
                        <td>87</td>
                    </tr>
            </tbody>
            </table>

    <div data-role='footer' data-position='fixed'>
        <h1>Footer</h1>
    </div>
</div>
</body>
</html>
Try The Code

The main line that we need to look at is:

<table data-role='table' data-filter='true' data-input='#filterTable-input' class='ui-responsive'>

In the table html tag, we need to define data-filter=’true’.

The input box needs to have data-type=’search’ with an id pointing to the table as well.

jQuery Mobile: Filterable List

This entry is part 31 of 73 in the series jQuery Mobile

Using jQuery Mobile, it is easy to set up a filterable list.

The children of any element can be filtered by setting the attribute data-filter=’true’ on the element.

<!DOCTYPE html>
<html>
<head>
    <link href='http://code.jquery.com/mobile/1.4.1/jquery.mobile-1.4.1.min.css' rel='stylesheet'/>
    <script src='http://code.jquery.com/jquery-1.10.2.min.js'></script>
    <script src='http://code.jquery.com/mobile/1.4.1/jquery.mobile-1.4.1.min.js'></script>
    <title>Codecrawl</title>
</head>
<body>
<div data-role='page'>
    <div data-role='header'><h1>Filterable List</h1></div>
    <br>

    <form class='ui-filterable'>
        <input id='filterBasic_id' data-type='search'>
    </form>
    <ul data-role='listview' data-filter='true' data-input='#filterBasic_id'>
        <li>Pit Bull</li>
        <li>Kylie Minogue</li>
        <li>Depeche Mode</li>
        <li>Miley Cyrus</li>
        <li>Madonna</li>
    </ul>

    <div data-role='footer' data-position='fixed'>
        <h1>Footer</h1>
    </div>
</div>
</body>
</html>
Try The Code

Add class ui-filterable to the form in which you wrap the search input to the listview.

The input box needs to have data-type=’search’ with the id pointing towards the listview as in line 17.

jQuery: .filter()

This entry is part 29 of 33 in the series jQuery Tutorial

.filter() reduces the set of matched elements to those that match the selector or pass the function’s test.

<!doctype html>
<html>
<head>
    <title>eq demo</title>
    <script src='//code.jquery.com/jquery-1.10.2.js'></script>
    <style>
        li {
            background: yellow;
        }

        .blue {
            background: blue;
        }
    </style>
</head>
<body>

<ul>
    <li>list item 1</li>
    <li>list item 2</li>
    <li>list item 3</li>
    <li>list item 4</li>
    <li>list item 5</li>
    <li>list item 6</li>
</ul>

<script>
    $('li').filter(':even').addClass('blue');
</script>

</body>
</html>

Try The Code

There are 6 list items in the list and we have used :even selector to select the even number list.