按字母顺序将行插入到表格中的某个单元格上
我正在使用 jQuery 在数据库插入后将行插入表中。当页面加载时,表格在特定单元格上按字母顺序排序(见下文)。当我使用 jQuery 插入新行时,我希望也能够按字母顺序插入它。
例如,我有一个包含与此类似的 元素的表:
<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>
当前,当将新类行插入到给定文档的 时,我使用
.append()
函数,插入新文档时我使用 .after()
。
使用 jQuery,我希望能够按类编号或文档名称的字母顺序插入类或文档(根据要插入的内容而定)。
例如,我希望能够为文档 C 插入一个新的 ,或者向文档 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 匹配的位置。
I am using jQuery to insert rows into a table after a database insert. When the page loads the table is ordered alphabetically on a particular cell (see below). When I insert a new row using jQuery I would like to be able to insert it alphabetically also.
So for example I have a table with <tbody>
elements similar to this:
<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>
Currently, when inserting a new class row to a given document's <tbody>
I use the .append()
function, and when inserting a new document I use .after()
.
Using jQuery I want to be able to insert a class or document in alphabetical order on class number or document name (as appropriate for what's being inserted).
So for instance I want to be able to insert a new <tbody>
for Document C, or add a new class to Document B with a class number of CLASS222.
How can I do this?
UPDATE:
Here's the code I currently use to insert a new class for one of the documents:
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>');
As you can see, the correct document is found with $('#docsTable a[name="' + docID + '"]').closest('tr')
, so I would need to look through the selected row's third td element, check the text and somehow determine where the variable classNumber fits alphabetically in with the other classNumbers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一种可能适合您的方法(未经测试并且是我的想法)。这仅适用于主标题“文档”行,但如果它适合您,我相信您也可以将这个想法应用到内部行。
首先,一个方便的函数来构建适合您上面模式的“文档”行。大概您已经有一些东西可以生成这些行:
接下来,一个添加新文档行的函数:
在您的代码中,当您想要插入新行时:
这肯定可以做得更好,未经测试,并且超出了我的头,但也许会有帮助。
编辑:把它扔进小提琴里。似乎有效。 http://jsfiddle.net/redler/fhkWT/
Here's an approach that might work for you (untested and off the top of my head). This applies only to the main header "document" rows, but if it works for you, I'm sure you can adapt the idea to the inner rows as well.
First, a convenience function to build a "document" row fitting your pattern above. Presumably you have something already to generate these rows:
Next, a function to add a new document row:
And in your code, when you want to insert a new row:
This can surely be made better, is untested, and off the top of my head, but perhaps it will be helpful.
Edit: Threw it into a Fiddle. Seems to work. http://jsfiddle.net/redler/fhkWT/
这是我在之前的项目之一中采用的一种方法。
只需将行附加到 tbody 即可。为每行提供索引作为数据属性。获取要以任何对象数组的形式对数据进行排序的列的数据值,如下所示。
通过提供自定义排序方法对此数组进行排序。排序后,您可以循环遍历该数组并将行附加到同一个表中。
Here is one approach which I had done in one of previous projects.
Just append the row to tbody. Give index to each row as a data attribute. Grab the data values of the column on which you want to sort the able in the form of any array of object as shown below.
Sort this array by providing a custom sort method. After sorting you can loop through this array and append the rows to the same table.