【发布时间】:2016-01-16 21:30:36
【问题描述】:
我正在创建一个响应式表格:
<h2>Workflows</h2>
<table class="tablesaw" data-tablesaw-mode="columntoggle" data-tablesaw-minimap>
<thead>
<tr>
<th scope="col" data-tablesaw-sortable-col data-tablesaw-priority="persist">Workflow Name</th>
<th scope="col" data-tablesaw-sortable-col data-tablesaw-priority="1">Title</th>
<th scope="col" data-tablesaw-sortable-col data-tablesaw-sortable-default-col data-tablesaw-priority="2">Assigned To</th>
<th scope="col" data-tablesaw-sortable-col data-tablesaw-priority="3">Status</th>
<th scope="col" data-tablesaw-sortable-col data-tablesaw-priority="3">Due Date</th>
<th scope="col" data-tablesaw-sortable-col data-tablesaw-priority="2">Button Status</th>
<th scope="col" data-tablesaw-sortable-col data-tablesaw-priority="1">Created</th>
<th scope="col" data-tablesaw-sortable-col data-tablesaw-priority="4">Description</th>
<th scope="col" data-tablesaw-sortable-col data-tablesaw-priority="4">Modified</th>
<th scope="col" data-tablesaw-sortable-col data-tablesaw-priority="4">Pedecessors</th>
<th scope="col" data-tablesaw-sortable-col data-tablesaw-priority="4">Priority</th>
<th scope="col" data-tablesaw-sortable-col data-tablesaw-priority="4">Related Items</th>
<th scope="col" data-tablesaw-sortable-col data-tablesaw-priority="4">Start Date</th>
<th scope="col" data-tablesaw-sortable-col data-tablesaw-priority="4">Task Group</th>
<th scope="col" data-tablesaw-sortable-col data-tablesaw-priority="4">Created By</th>
<th scope="col" data-tablesaw-sortable-col data-tablesaw-priority="4">Modified By</th>
<th scope="col" data-tablesaw-sortable-col data-tablesaw-priority="4">% Complete</th>
</tr>
</thead>
<tbody>
<tr>
<td class="title"><a href="#"></a></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
在附加特定输入时,我正在使用此脚本在表格单元格中循环:
$(document).ready(function(){
$('table.tablesaw tbody tr').each(function(){
$('tr td').each(function(){
$('.title').append().html('<input type="text" />')
.next().append().html('<input type="text" />')
.next().append().html('<select><option></option><option></option><option></option></select>')
.next().append().html('<select><option></option><option></option><option></option></select>')
.next().append().html('<input type="datetime" value="Select Date" />')
.next().append().html('<select><option></option><option></option><option></option></select>')
.next().append().html('<input type="datetime" value="Select Date" />')
.next().append().html('<input type="text" />')
.next().append().html('<input type="datetime" value="Date" />')
.next().append().html('<input type="text" />')
.next().append().html('<select><option></option><option></option><option></option></select>')
.next().append().html('<input type="text" />')
.next().append().html('<input type="datetime" value="Date" />')
.next().append().html('<input type="text" />')
.next().append().html('<input type="text" />')
.next().append().html('<input type="text" />')
.next().append().html('<input type="range" />');
});
});
});
我的问题: 这本质上是在添加表格行时完成动态修改单元格的最有效和最高效的方法吗?
【问题讨论】:
-
我试图避免使用 html 代码指定表格单元格内容,我宁愿将单元格留空并通过 jQuery 追加,除非另一种方法更有效地完成任务。此外,如果需要,我可以包含任何额外的代码,但基于这个简单的函数,它似乎无关紧要。
-
不是真的。这是很多 dom 摆弄。您最好将这些行的副本保留在某个隐藏/分离的节点中,然后简单地将其克隆到必要的位置。那将是 dom 的一个单一模式。
-
我同意。使用预期的输入创建一行并克隆它。
标签: jquery html html-table