简体   繁体   English

如何用 jQuery 替换表中的值(在每一行中)?

[英]How to replace <td> values in a table with jQuery (in every Rows)?

I have a table with multiple columns, one column named: 'Type'.我有一个包含多列的表,其中一列名为:'Type'。 The values in Type column could be: 1 or 2. I want to replace the value “1” to “Information” and the value “2” to “Problem” in every row with jQuery, how can I do that?类型列中的值可能是:1 或 2。我想用 jQuery 将每一行中的值“1”替换为“信息”,将值“2”替换为“问题”,我该怎么做?

Here in this demo you'll find a function transformTableData() that takes the table existing in the document and will:在此演示中,您将找到一个 function transformTableData() ,它获取文档中存在的表格并将:

  • find where is located the field having as header the string "Type";查找具有 header 字符串“类型”的字段的位置;
  • loop through all its rows and change the value of the corresponding field as the result coming out of the map defined on top.循环遍历其所有行并更改相应字段的值,作为顶部定义的 map 的结果。 So according to the default map I defined, if the field value is '1' it will be transformed to 'Information' and if the value is '2' it will be transformed to 'Problem';所以根据我定义的默认map,如果字段值为'1',它将转换为'Information',如果值为'2',它将转换为'Problem';
  • If there's no corresponding value in the map, the value will be untouched;如果 map 中没有对应的值,则该值不变;

The function runs when you click the button on the bottom of the page. function 在您单击页面底部的按钮时运行。 Of course the same function could be called on document ready.当然,可以在准备好文档时调用相同的 function。

 function transformTableData(){ const map = { '1': 'Information', '2': 'Problem', } const typeHeaderCell = $('table thead tr th:contains(Type)'); const typeHeaderIndex = $(typeHeaderCell).index(); $('table tbody tr').each((i, row)=>{ const rowCell = $(row).find(`td:nth-child(${typeHeaderIndex+1})`); const value = rowCell.text(); rowCell.text( map?.[value] ); }); }
 table, tr, th, td{ border: solid 1px black; padding: 1rem; } button{ margin-top: 1rem; padding: 1rem; font-size: 1.25rem; cursor: pointer; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table> <thead> <tr> <th>Column1</th> <th>Column2</th> <th>...</th> <th>Type</th> <th>...</th> <th>ColumnN</th> </tr> </thead> <tbody> <tr> <td></td> <td></td> <td></td> <td>1</td> <td></td> <td></td> </tr> <tr> <td></td> <td></td> <td></td> <td>2</td> <td></td> <td></td> </tr> <tr> <td></td> <td></td> <td></td> <td>INVALID</td> <td></td> <td></td> </tr> </tbody> </table> <button onclick="transformTableData();">Transform Table Data</button>

There are many ways to achieve something like this.有很多方法可以实现这样的目标。 Here is one example.这是一个例子。 It first looks for the index by comparing the text of each cell in the table header.它首先通过比较表 header 中每个单元格的文本来查找索引。 then it gets all cells in the table body with the index in each table row and replaces the content if it is "1" or "2".然后它获取表体中的所有单元格以及每个表行中的索引,如果它是“1”或“2”,则替换内容。 There are for sure even shorter or faster methods.肯定有更短或更快的方法。

 // Find index of column with "Type" let index = -1; let th = $('#myTable thead tr th'); for (let i=0; i<th.length; i++) { if ($(th[i]).text() == 'Type') { index = i; break; } } // If index is greater then -1 we found the column if (index > -1) { // Get all the table cells in each row at the specific index (need to add +1 to the index) let td = $('#myTable tbody tr td:nth-child(' + (index+1) + ')'); for (let i=0; i<td.length; i++) { // Compare content and replace it if ($(td[i]).text() == '1') { $(td[i]).text('Information'); } else if ($(td[i]).text() == '2') { $(td[i]).text('Problem'); } } }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table id="myTable"> <thead> <tr> <th>ID</th> <th>Type</th> <th>Name</th> </tr> </thead> <tbody> <tr> <td>0</td> <td>1</td> <td>John</td> </tr> <tr> <td>1</td> <td>2</td> <td>Maria</td> </tr> <tr> <td>2</td> <td>2</td> <td>Walter</td> </tr> <tr> <td>3</td> <td>1</td> <td>Julia</td> </tr> </tbody> </table>

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

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