第27章 jQuery - フィルタ
jQuery(ジェイクエリー)は、ウェブブラウザ用のJavaScriptコードをより容易に記述できるようにするために設計されたJavaScriptライブラリである。
homepage
# **jQuery - フィルタ** *** jQueryを使って特定の要素をフィルタ/検索します。 ## **1.フィルタテーブル** テーブル内の項目に対して大文字と小文字を区別しない検索を実行します。 #### **例** [sample27-1.html] ``` <!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("#myInput").on("keyup", function() { var value = $(this).val().toLowerCase(); $("#myTable tr").filter(function() { $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1) }); }); }); </script> <style> table { font-family: arial, sans-serif; border-collapse: collapse; width: 100%; } td, th { border: 1px solid #dddddd; text-align: left; padding: 8px; } tr:nth-child(even) { background-color: #dddddd; } </style> </head> <body> <h2>Filterable Table</h2> <p>Type something in the input field to search the table for first names, last names or emails:</p> <input id="myInput" type="text" placeholder="Search.."> <br><br> <table> <thead> <tr> <th>Firstname</th> <th>Lastname</th> <th>Email</th> </tr> </thead> <tbody id="myTable"> <tr> <td>John</td> <td>Doe</td> <td>john@example.com</td> </tr> <tr> <td>Mary</td> <td>Moe</td> <td>mary@mail.com</td> </tr> <tr> <td>July</td> <td>Dooley</td> <td>july@greatstuff.com</td> </tr> <tr> <td>Anja</td> <td>Ravendale</td> <td>a_r@test.com</td> </tr> </tbody> </table> <p>Note that we start the search in tbody, to prevent filtering the table headers.</p> </body> </html> ``` #### **実行結果** <pre> <span class="nocode"> <iframe style="position:relative;width:100%;height:400px" src="javascript: '<html><script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("#myInput").on("keyup", function() { var value = $(this).val().toLowerCase(); $("#myTable tr").filter(function() { $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1) }); }); }); </script> <style> table { font-family: arial, sans-serif; border-collapse: collapse; width: 100%; } td, th { border: 1px solid #dddddd; text-align: left; padding: 8px; } tr:nth-child(even) { background-color: #dddddd; } </style> <body> <h2>Filterable Table</h2> <p>Type something in the input field to search the table for first names, last names or emails:</p> <input id="myInput" type="text" placeholder="Search.."> <br><br> <table> <thead> <tr> <th>Firstname</th> <th>Lastname</th> <th>Email</th> </tr> </thead> <tbody id="myTable"> <tr> <td>John</td> <td>Doe</td> <td>john@example.com</td> </tr> <tr> <td>Mary</td> <td>Moe</td> <td>mary@mail.com</td> </tr> <tr> <td>July</td> <td>Dooley</td> <td>july@greatstuff.com</td> </tr> <tr> <td>Anja</td> <td>Ravendale</td> <td>a_r@test.com</td> </tr> </tbody> </table> <p>Note that we start the search in tbody, to prevent filtering the table headers.</p> </body></html>'"></iframe></span> </pre> #### **例の説明:** jQueryを使用して各テーブル行をループし、入力フィールドの値と一致するテキスト値があるかどうかを確認します。この`toggle()`メソッドは`display:none`、検索に一致しないrow()を隠します。我々が使用する`toLowerCase()`検索の場合は鈍感ケースを下げるために、テキストを変換するために、DOMメソッドを(検索で「ジョン」、さらには「JOHN」を可能にします)。 <br><br> ## **2.フィルタリスト** リスト内の項目に対して大文字と小文字を区別しない検索を実行します。 #### **例** [sample27-2.html] ``` <!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("#myInput").on("keyup", function() { var value = $(this).val().toLowerCase(); $("#myList li").filter(function() { $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1) }); }); }); </script> </head> <body> <h2>Filterable List</h2> <p>Type something in the input field to search the list for specific items:</p> <input id="myInput" type="text" placeholder="Search.."> <br> <ul id="myList"> <li>First item</li> <li>Second item</li> <li>Third item</li> <li>Fourth</li> </ul> </body> </html> ``` #### **実行結果** <pre> <span class="nocode"> <iframe style="position:relative;width:100%;height:400px" src="javascript: '<html><script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("#myInput").on("keyup", function() { var value = $(this).val().toLowerCase(); $("#myList li").filter(function() { $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1) }); }); }); </script> <body> <h2>Filterable List</h2> <p>Type something in the input field to search the list for specific items:</p> <input id="myInput" type="text" placeholder="Search.."> <br> <ul id="myList"> <li>First item</li> <li>Second item</li> <li>Third item</li> <li>Fourth</li> </ul> </body> </html>'"></iframe></span> </pre>
content
戻る