我正在使用 jQuery 在数据库插入后将行插入到表中。当页面加载时,表格在特定单元格上按字母顺序排列(见下文)。当我使用 jQuery 插入新行时,我也希望能够按字母顺序插入它。
因此,例如,我有一个包含<tbody>
与此类似的元素的表:
<tbody>
<tr class="docRow" bgcolor="#EFE5D3" style="font-weight: bold; font-size: 1.1em;">
<td width="25px"><a class="docEditLink docAdminFormSubmit" name="38" href="#">Edit</a></td>
<td width="20px"><input type="checkbox" class="chkDeleteDocs" name="removeDocs[]" value="38" /></td>
<td>Document A</td>
<td>Description of Document A</td>
</tr>
<tr class="docClassesRow noClasses">
<td colspan="4">
<strong>No classes are currently associated with this document.</strong>
</td>
</tr>
</tbody>
<tbody>
<tr class="docRow" bgcolor="#EFE5D3" style="font-weight: bold; font-size: 1.1em;">
<td width="25px"><a class="docEditLink docAdminFormSubmit" name="35" href="#">Edit</a></td>
<td width="20px"><input type="checkbox" class="chkDeleteDocs" name="removeDocs[]" value="35" /></td>
<td>Document B</td>
<td>Description of Document B</td>
</tr>
<tr class="docClassesRow">
<td></td>
<td width="100px" align="right"><input type="checkbox" class="chkRemoveClasses" name="removeClasses[]" value="33-35" /></td>
<td width="200px">CLASS111</td>
<td width="600px">Description of CLASS101</td>
</tr>
<tr class="docClassesRow">
<td></td>
<td width="100px" align="right"><input type="checkbox" class="chkRemoveClasses" name="removeClasses[]" value="45-35" /></td>
<td width="200px">CLASS333</td>
<td width="600px">Description of CLASS333</td>
</tr>
</tbody>
<tbody>
<tr class="docRow" bgcolor="#EFE5D3" style="font-weight: bold; font-size: 1.1em;">
<td width="25px"><a class="docEditLink docAdminFormSubmit" name="46" href="#">Edit</a></td>
<td width="20px"><input type="checkbox" class="chkDeleteDocs" name="removeDocs[]" value="46" /></td>
<td>Document D</td>
<td>Description of Document D</td>
</tr>
<tr class="docClassesRow noClasses">
<td colspan="4">
<strong>No classes are currently associated with this document.</strong>
</td>
</tr>
</tbody>
目前,当向给定文档插入新的类行时,<tbody>
我使用该.append()
函数,而在插入新文档时,我使用.after()
.
使用 jQuery,我希望能够在类号或文档名称上按字母顺序插入一个类或文档(根据插入的内容而定)。
因此,例如,我希望能够<tbody>
为 Document C 插入一个新类,或者向 Document B 添加一个类号为 CLASS222 的新类。
我怎样才能做到这一点?
更新: 这是我目前用来为其中一个文档插入新类的代码:
newClassesRows += $('#docsTable a[name="' + docID + '"]').closest('tr').after('<tr class="docClassesRow">' +
'<td></td>' +
'<td width="100px" align="right"><input type="checkbox" class="chkRemoveClasses" name="removeClasses[]" value="' + classID + '-' + docID + '" /></td>' +
'<td width="200px">' + classNumber + '</td>' +
'<td width="600px">' + className + '</td>' +
'</tr>');
如您所见,使用 找到了正确的文档$('#docsTable a[name="' + docID + '"]').closest('tr')
,因此我需要查看所选行的第三个 td 元素,检查文本并以某种方式确定变量classNumber按字母顺序与其他 classNumber 匹配的位置。