确定驻留在单元格下方的表格列中的单元格
在下表中:
<table>
<thead>
<tr>
<th>Th1</th>
<th colspan='2'>Th23</th>
<th>Th4</th>
</tr>
</thead>
<tbody>
<tr>
<td>Td1</td>
<td>Td2</td>
<td>Td3</td>
<td>Td4</td>
</tr>
</tbody>
</table>
对于包含文本“Th23”的表格单元格,我想知道哪些单元格位于其下方。在这种情况下,答案将是分别包含文本“Td2”和“Td3”的单元格。
是否有任何 DOM 属性或内置函数可以帮助进行此类计算?
@Matt McDonald 有一个更通用的解决方案。
这就是我最终得到的结果:
// get tbody cell(s) under thead cell (first arg)
// if rowIndex===undefined, get from all rows; otherwise, only that row index
// NOTE: does NOT work if any cell.rowSpan != 1
var columnCells = function( th, rowIndex ) {
// get absolute column for th
for( var absCol=0, i=0; true; i++ ) {
if( th.parentNode.cells[i] == th ) break;
absCol += th.parentNode.cells[i].colSpan;
}
// look in tBody for cells; all rows or rowIndex
var tBody = th.parentNode.parentNode.nextSibling;
var cells = [];
for( var r=((rowIndex==undefined)?0:rowIndex); true; r++ ) {
if( rowIndex!==undefined && r>rowIndex ) break;
if( rowIndex==undefined && r>=tBody.rows.length ) break;
for( var c=0; true; c+=tBody.rows[r].cells[c].colSpan ) {
if( c < absCol ) continue;
if( c >= absCol+th.colSpan ) break;
cells.push(tBody.rows[r].cells[c]);
}
}
return cells;
}
in the following table:
<table>
<thead>
<tr>
<th>Th1</th>
<th colspan='2'>Th23</th>
<th>Th4</th>
</tr>
</thead>
<tbody>
<tr>
<td>Td1</td>
<td>Td2</td>
<td>Td3</td>
<td>Td4</td>
</tr>
</tbody>
</table>
For the table cell containing text "Th23", I'd like to know which cells reside beneath it. In this case, the answer would be the cells containing text "Td2", and "Td3" respectively.
Are there any DOM properties or built-ins that help with this type of calculation?
@Matt McDonald has a more general solution.
This is what I ended up with:
// get tbody cell(s) under thead cell (first arg)
// if rowIndex===undefined, get from all rows; otherwise, only that row index
// NOTE: does NOT work if any cell.rowSpan != 1
var columnCells = function( th, rowIndex ) {
// get absolute column for th
for( var absCol=0, i=0; true; i++ ) {
if( th.parentNode.cells[i] == th ) break;
absCol += th.parentNode.cells[i].colSpan;
}
// look in tBody for cells; all rows or rowIndex
var tBody = th.parentNode.parentNode.nextSibling;
var cells = [];
for( var r=((rowIndex==undefined)?0:rowIndex); true; r++ ) {
if( rowIndex!==undefined && r>rowIndex ) break;
if( rowIndex==undefined && r>=tBody.rows.length ) break;
for( var c=0; true; c+=tBody.rows[r].cells[c].colSpan ) {
if( c < absCol ) continue;
if( c >= absCol+th.colSpan ) break;
cells.push(tBody.rows[r].cells[c]);
}
}
return cells;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您立即需要做三件事:
id
属性以便于选择。id
属性,以便于选择。parentNode
(行)这三件事将使表格相关的计算变得更加容易。
接下来是一个获取指定单元格的伪属性的函数。在本例中,我们正在寻找其“起始索引”(以列为单位)、“结束索引”(以列为单位)及其“宽度”(结束 - 起始,也以列为单位)。
从那里,您可以遍历表的行并检查哪些单元格位于开始索引和结束索引之间。
HTML:
JS(耐心等待):
演示:http://jsbin.com/ohohew/edit# javascript,html
这将确定哪些单元格位于您要查找的特定列(包括示例)内。如果这不完全是您想要的功能,您可以自定义该功能以满足您的需求。
Right off the bat, you need to do three things:
id
attribute for easy selection.id
attribute for easy selection as well.parentNode
(row)These three things will enable easier table-related calculations.
Next up is a function that grabs pseudo-properties of the specified cell. In this case, we're looking for its "start index" (in terms of columns), its "end index" (in terms of columns), and its "width" (end - start, in columns as well).
From there, you can traverse through the table's rows and check which cells fall between the start and the end indexes.
HTML:
JS (bear with me):
Demo: http://jsbin.com/ohohew/edit#javascript,html
This will determine which cells reside inside the particular column you're looking for (including the example). You can customize the function to fit your needs if that's not exactly what you're looking for.
您需要知道单元格的列索引。我将其命名为
ci
。然后读取其colspan(如果为空,则将其设置为1)。然后在下一行中找到列索引 >=ci
和的单元格。
ci + colspan
。对于如此复杂的需求,使用JS框架是非常有用的。我假设您可以使用 JQuery,因为它是最常用的。计算列索引有 SO 上的几种解决方案< /a>.
读取 colspan 属性只需使用 jQuery
cell.attr('colspan')
即可。查找下一行是
cell.closest('tr').next('tr')
。最后一步是迭代该行的每个元素并计算它们的列索引。您可以使用与上面相同的函数,但如果它不够高效,应该很容易调整其代码,使其不返回整数,而是将元素添加到数组中。
You need to know the column index of your cell. I'll name it
ci
. Then read its colspan (if empty, set it to 1). Then find the cells on the next line that have a column index >=ci
and <ci + colspan
. For such a complex need, using a JS framework is very useful. I'll suppose you can use JQuery, since it's the most frequently used.Computing the colum index has several solutions on SO.
Reading the colspan attribute is just
cell.attr('colspan')
with jQuery.Finding the next row is
cell.closest('tr').next('tr')
.The last step is to iterate over every element of the line and compute their column index. You could use the same function as above, but if it's not efficient enough, it should be easy to adapt its code so that it does not return an integer, but add elements to an array.