简体   繁体   English

HTML 表使用 Javascript Arrays

[英]HTML Table using Javascript Arrays

Is there a way to search a table using keywords through javascript arrays?有没有办法通过 javascript arrays 使用关键字搜索表? My search only finds the form based on the wording in the existing table.我的搜索仅根据现有表格中的措辞找到表格。 Would it be possible to add keywords to display certain results?是否可以添加关键字来显示某些结果? For example, if I search for "Apple", it should display the same results as if I searched for "ABC".例如,如果我搜索“Apple”,它应该显示与搜索“ABC”相同的结果。 See my table below.请参阅下面的表格。

Note that I am not able to edit HTML table (add class to it) as the table is auto generated with Content Management System (CMS).请注意,我无法编辑 HTML 表(将 class 添加到其中),因为该表是使用内容管理系统 (CMS) 自动生成的。

 function myFunction() { const userInput = document.getElementById("myInput").value.toUpperCase(); const tableRows = document.querySelectorAll("table tr"); for (let i = 0; i < tableRows.length; i++) { const rowTextContent = tableRows[i].innerText.toUpperCase(); tableRows[i].style.display = rowTextContent.toUpperCase().includes(userInput)? "": "none"; } }
 table.table_brdr td { padding: 8px 10px; border: none; } table.table_brdr th { background-color: #a6a6a6; color: black; } tr:nth-of-type(odd) { background-color#D3D3D3; } #myInput { background-image: url('/css/searchicon.png'); background-position: 10px 10px; background-repeat: no-repeat; width: 20%; font-size: 16px; padding: 12px 20px 12px 40px; border: 1px solid #ddd; margin-bottom: 12px; }
 <p><input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search forms list" title="Search forms list"></p> <table class="table_brdr" id="myTable"> <tr> <th>Column1</th> <th><strong>Column2</th> <th>Column3</th> </tr> <tr> <td>abc</td> <td>xyz</td> <td>03/30/2017</td> </tr> <tr> <td>test12</td> <td>https://www.yahoo.com/ </td> <td>03/30/2017</td> </tr> <tr> <th>Column1</th> <th> New Column</th> <th>Column3</th> </tr> <tr> <td>John</td> <td>abctd <td> <td>09/30/2019</td> </tr> <tr> <th>Column1</th> <th> New Column2</th> <th>Column3</th> </tr> <tr> <td>Doe</td> <td>abctd </td> <td>06/30/2019</td> </tr> </tbody></table>

Using object is possible like this, it will work, replace your js code with this js code: (This is case sensitive)像这样使用 object 是可能的,它会起作用,用这个 js 代码替换你的 js 代码:(这是区分大小写的)

const obj = {
  'Apple':'ABC',
};
function myFunction() {
  let userInput = document.getElementById("myInput").value;
  if(obj[userInput]) userInput = obj[userInput];
  userInput = userInput.toUpperCase();
  const tableRows = document.querySelectorAll("table tr");
  for (let i = 0; i < tableRows.length; i++) {
    const rowTextContent = tableRows[i].innerText.toUpperCase();
    tableRows[i].style.display = rowTextContent.toUpperCase().includes(userInput) ? "" : "none";
  }
}

EDIT: What is the structure of your array?编辑:你的数组的结构是什么? I will modify the code for array.我将修改数组的代码。

Assuming what you're asking for is associating a "keyword" with a specific <tr> tag directly in html, the most standards-compliant way to do this is probably to use data attributes .假设您要的是直接在 html 中将“关键字”与特定的<tr>标签相关联,那么最符合标准的方法可能是使用data attributes

eg例如

<tr data-keywords="Apple anotherkeyword">
<td>John</td>
<td>abctd </td>
<td>09/30/2019</td>
</tr>

Then check it like:然后检查它:

for (let i = 0; i < tableRows.length; i++) {
    const rowTextContent = tableRows[i].innerText + tableRows[i].dataset.keywords;
    tableRows[i].style.display = 
        rowTextContent.toUpperCase().includes(userInput) ?  "" : "none";
}

You can use HTML 5 Data Attributes to "hide" the keywords into the rows that they belong to.您可以使用HTML 5 数据属性将关键字“隐藏”到它们所属的行中。

The HTML for a <tr> would become like this: <tr>的 HTML 会变成这样:

<tr data-keyword="Apple">
    ....
</tr>

The next step is to write some JS code to use the contents of the data attribute using tableRow.dataset.keyword .下一步是编写一些 JS 代码来使用tableRow.dataset.keyword使用数据属性的内容。

Here is your code, extended with the data-keyword= attribute and matching JS code to use it when searching:这是您的代码,扩展了data-keyword=属性和匹配的 JS 代码以在搜索时使用它:

 function myFunction() { const userInput = document.getElementById("myInput").value.toUpperCase(); const tableRows = document.querySelectorAll("table tr"); for (let i = 0; i < tableRows.length; i++) { const isKeywordMatch = tableRows[i].dataset.keyword?.toUpperCase().includes(userInput); const isTextMatch = tableRows[i].innerText.toUpperCase().includes(userInput); tableRows[i].style.display = isKeywordMatch || isTextMatch? "": "none"; } }
 .keyword { display: none; } table.table_brdr td { padding: 8px 10px; border: none; } table.table_brdr th { background-color: #a6a6a6; color: black; } tr:nth-of-type(odd) { background-color#D3D3D3; } #myInput { background-image: url('/css/searchicon.png'); background-position: 10px 10px; background-repeat: no-repeat; width: 20%; font-size: 16px; padding: 12px 20px 12px 40px; border: 1px solid #ddd; margin-bottom: 12px; }
 <p><input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search forms list" title="Search forms list"></p> <table class="table_brdr" id="myTable"> <tbody> <tr> <th>Column1</th> <th><strong>Column2</strong></th> <th>Column3</th> </tr> <tr data-keyword="Apple"> <td>abc</td> <td>xyz</td> <td>03/30/2017</td> </tr> <tr> <td>test12</td> <td>https://www.yahoo.com/ </td> <td>03/30/2017</td> </tr> <tr> <th>Column1</th> <th>New Column</th> <th>Column3</th> </tr> <tr> <td>John</td> <td>abctd</td> <td>09/30/2019</td> </tr> <tr> <th>Column1</th> <th>New Column2</th> <th>Column3</th> </tr> <tr> <td>Doe</td> <td>abctd </td> <td>06/30/2019</td> </tr> </tbody> </table>

PS - I also fixed some errors in your HTML code: PS - 我还修复了 HTML 代码中的一些错误:

  • <strong> without a matching closing tag <strong>没有匹配的结束标记
  • at one place there is a <td> where a </td> should be在一个地方有一个<td> </td>
  • you had </tbody> but not <tbody> .你有</tbody>但没有<tbody>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM