th hide when search table with jQuery

痞子三分冷 提交于 2020-06-28 06:58:16

问题


Based on the example given by css-tricks, I have added a search table with jQuery in my xsl. It works of course; but when I search in the table, th are hidden. From what I understand, it's because the id used in input is the same for all tr. I'm a neophyte in jQuery, thus I would very much appreciate some help in order to display th titles when the results of the search are displayed.

jQuery script:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"/>
                <script>
                    <![CDATA[
                            var allRows = $("tr");
                            $("input#search").on("keydown keyup", function() {
                            allRows.hide();
                            $("tr:contains('" + $(this).val() + "')").show();
                            });
                        ]]>
                </script>

xsl code [note that I have also a filter function sortable]

<script src="https://www.kryogenix.org/code/browser/sorttable/sorttable.js"/>
                <table class="sortable table-5" >
                    <caption>List of “What Result”
                        <input class="input-table-5" type="search" id="search" placeholder="Search.." aria-label="Search"/>
                    </caption>
                   <tr>
                       <th>Occur.</th>
                       <th>What Result</th>
                       <th>What Context</th>
                       <th>What Sphere</th>
                       <th>vs AE</th>
                       <th>vs clan</th>
                   </tr>
                    <xsl:for-each select="./key('persName', @ana)//ancestor-or-self::interp">
                    <tr>
                        <td><xsl:value-of select="./replace(replace(replace(tokenize(@ana)!substring-after(., '#'), '_', ':'), 'l', ''), 'ktu', 'KTU ')"/></td>
                        <td><xsl:value-of select=".//ref[@n='2']/stage/key('whatResult-interp', @ana)/catDesc"/></td>
                        <td><xsl:value-of select=".//ref[@n='5']/placeName/key('whatContext-interp', @ana)/catDesc"/></td>
                        <td><xsl:value-of select=".//ref[@n='6']/span/key('whatSphere-interp', @ana)/catDesc"/></td>
                        <td><xsl:value-of select=".//ref[@n='3-2a']/persName/key('person', tokenize(@ana, '\s+')[1])/persName/node()[@n = '1b']"/></td>
                        <td>
                            <!-- other data -->
                        </td>
                    </tr>
                    </xsl:for-each>
                </table>

In advance, thank you very much for your help.


回答1:


Change allRows so it only contains rows that have <td> elements:

var allRows = $("tr:has(td)");


来源:https://stackoverflow.com/questions/62417931/th-hide-when-search-table-with-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!