过滤树状可折叠内容

时间:2015-05-07 14:23:30

标签: javascript jquery jquery-mobile

我有一个包含多个可折叠内容的列表视图(树状)。过滤器会自动展开可折叠集中的最后一个块,而不是搜索的真正的块/ div(来自可折叠div的值)。

DEMO

以下是代码:

<script>
$("#mineralslist").on("filterablefilter", function (event, ui) {
    if ($("#mineralslist .ui-screen-hidden").length == 0) {
        $('#mineralslist [data-role="collapsible"]').collapsible("collapse");
    } else {
        $('#mineralslist [data-role="collapsible"]').collapsible("expand");

    }

});
</script>

<div data-role="content" id="iama">
    <ul data-role="listview" id="mineralslist" data-inset='true' data-filter="true" data-filter-placeholder="Search mineral...">
        <li>
            <div data-role="collapsible" data-inset='false'>
                 <h3>Native elements</h3>

                <div data-role="collapsible-set">
                    <div data-role="collapsible">
                         <h3>Gold group</h3>

                        <div data-role="collapsible-set">
                            <div data-role="collapsible">
                                 <h4>Gold</h4>

                                <table data-role="table" id="mineral-table" class="ui-shadow table-stroke">
                                    <thead>
                                        <tr>
                                            <th class="label">Formula</th>
                                            <th class="label">System</th>
                                            <th class="label">Colour</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <tr>
                                            <td>Au</td>
                                            <td>Isometric</td>
                                            <td>Rich yellow</td>
                                        </tr>
                                    </tbody>
                                </table>
                            </div>
                            <div data-role="collapsible">
                                 <h4>Silver</h4>

                                <table data-role="table" id="mineral-table" class="ui-shadow table-stroke">
                                    <thead>
                                        <tr>
                                            <th class="label">Formula</th>
                                            <th class="label">System</th>
                                            <th class="label">Colour</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <tr>
                                            <td>Ag</td>
                                            <td>Isometric</td>
                                            <td>Silver-white</td>
                                        </tr>
                                    </tbody>
                                </table>
                            </div>
                            <div data-role="collapsible">
                                 <h4>Copper</h4>

                                <table data-role="table" id="mineral-table" class="ui-shadow table-stroke">
                                    <thead>
                                        <tr>
                                            <th class="label">Formula</th>
                                            <th class="label">System</th>
                                            <th class="label">Colour</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <tr>
                                            <td>Cu</td>
                                            <td>Isometric</td>
                                            <td>Brown, copper red, light pink, red</td>
                                        </tr>
                                    </tbody>
                                </table>
                            </div>
                            <div data-role="collapsible">
                                 <h4>Lead</h4>

                                <table data-role="table" id="mineral-table" class="ui-shadow table-stroke">
                                    <thead>
                                        <tr>
                                            <th class="label">Formula</th>
                                            <th class="label">System</th>
                                            <th class="label">Colour</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <tr>
                                            <td>Pb</td>
                                            <td>Isometric</td>
                                            <td>Grey, but often coated</td>
                                        </tr>
                                    </tbody>
                                </table>
                            </div>
                            <div data-role="collapsible">
                                 <h4>Aluminum</h4>

                                <table data-role="table" id="mineral-table" class="ui-shadow table-stroke">
                                    <thead>
                                        <tr>
                                            <th class="label">Formula</th>
                                            <th class="label">System</th>
                                            <th class="label">Colour</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <tr>
                                            <td>Al</td>
                                            <td>Isometric</td>
                                            <td>Grayish-White</td>
                                        </tr>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                    <div data-role="collapsible">
                         <h3>Platinum group</h3>

                        <div data-role="collapsible-set">
                            <div data-role="collapsible">
                                 <h4>Platinum</h4>

                                <table data-role="table" id="mineral-table" class="ui-shadow table-stroke">
                                    <thead>
                                        <tr>
                                            <th class="label">Formula</th>
                                            <th class="label">System</th>
                                            <th class="label">Colour</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <tr>
                                            <td>Pt</td>
                                            <td>Isometric</td>
                                            <td>Steel grey to dark grey</td>
                                        </tr>
                                    </tbody>
                                </table>
                            </div>
                            <div data-role="collapsible">
                                 <h4>Iridium</h4>

                                <table data-role="table" id="mineral-table" class="ui-shadow table-stroke">
                                    <thead>
                                        <tr>
                                            <th class="label">Formula</th>
                                            <th class="label">System</th>
                                            <th class="label">Colour</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <tr>
                                            <td>(Ir,Os,Ru)</td>
                                            <td>Isometric</td>
                                            <td>White</td>
                                        </tr>
                                    </tbody>
                                </table>
                            </div>
                            <div data-role="collapsible">
                                 <h4>Palladium</h4>

                                <table data-role="table" id="mineral-table" class="ui-shadow table-stroke">
                                    <thead>
                                        <tr>
                                            <th class="label">Formula</th>
                                            <th class="label">System</th>
                                            <th class="label">Colour</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <tr>
                                            <td>(Pd,Pt)</td>
                                            <td>Isometric</td>
                                            <td>White</td>
                                        </tr>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                    <div data-role="collapsible">
                         <h3>Carbon Polymorph group</h3>

                        <div data-role="collapsible-set">
                            <div data-role="collapsible">
                                 <h4>Diamond</h4>

                                <table data-role="table" id="mineral-table" class="ui-shadow table-stroke">
                                    <thead>
                                        <tr>
                                            <th class="label">Formula</th>
                                            <th class="label">System</th>
                                            <th class="label">Colour</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <tr>
                                            <td>C</td>
                                            <td>Isometric</td>
                                            <td>Colourless</td>
                                        </tr>
                                    </tbody>
                                </table>
                            </div>
                            <div data-role="collapsible">
                                 <h4>Graphite</h4>

                                <table data-role="table" id="mineral-table" class="ui-shadow table-stroke">
                                    <thead>
                                        <tr>
                                            <th class="label">Formula</th>
                                            <th class="label">System</th>
                                            <th class="label">Colour</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <tr>
                                            <td>C</td>
                                            <td>Hexagonal</td>
                                            <td>Iron black to steel-grey</td>
                                        </tr>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </li>
        <li>
            <div data-role="collapsible" data-inset='false'>
                 <h3>Organic compounds</h3>

                <p>Answer.</p>
            </div>
        </li>
    </ul>
</div>

示例:我想搜索 Iridium 并获取以下内容: enter image description here

我想要获得的只是扩展过滤后的块。就像在这张图片中: enter image description here

1 个答案:

答案 0 :(得分:1)

尝试jQ:contains()选择器的前缀

var s = $('input').val() // itidium
var list = $('#mineralslist').find('h4:contains("'+s+'")').css({'display':'block','opacity':1})

之后必须显示列表的所有父元素