jquery - 按字母顺序将行插入到表格中的某个单元格上

标签 jquery html-table insert row alphabetical

我正在使用 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>对于文档 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 匹配的位置。

最佳答案

这是一种可能适合您的方法(未经测试且是我的想法)。这仅适用于主标题“文档”行,但如果它适合您,我相信您也可以将这个想法应用于内部行。

首先,一个方便的函数来构建适合您上面模式的“文档”行。想必您已经有一些东西可以生成这些行:

var makeDocRow = function(name, description) {
    return $('<tbody><tr class="docRow" bgcolor="#EFE5D3" style="font-weight: bold; font-size: 1.1em;">\
        <td width="25px"><a class="docEditLink docAdminFormSubmit" name="whatever" href="#">Edit</a></td>\
        <td width="20px"><input type="checkbox" class="chkDeleteDocs" name="removeDocs[]" value="whatever" /></td>\
        <td>' + name + '</td>\
        <td>' + description + '</td>\
    </tr></tbody>');
};

接下来是添加新文档行的函数:

var addDocRow = function(name, description) {
  var counter = 0; // so we know when we've reached the end
  // assumes it's already sorted
  $('#main').find('tr.docRow').each(function() {
    counter++;
    if ($(this).find('td:eq(2)').html() >= name) { 
    /* Have we found the right slot for the new document by name?
       td:eq(2) refers to the table cell containing the name for comparison
       Assumes the table is always sorted to start */
      $(this).parent().before(makeDocRow(name, description)); // build/add row
      return false; // break out of each() since we're done
    }

    // Handle case where we've reached the end, but we're still in the loop.
    // This means the new row is alphabetically last, so insert after
    if ($(this).closest('table').find('tr.docRow').length === counter ) {
      $(this).parent().after(makeDocRow(name, description));
    }
  });
};

在您的代码中,当您想要插入新行时:

addDocRow('Document C','Document C Description');
addDocRow('Document Z','Document Z Description');

这肯定可以做得更好,未经测试,并且是我的想法,但也许它会有所帮助。

<小时/>

编辑:把它扔进 fiddle 里。似乎有效。 http://jsfiddle.net/redler/fhkWT/

关于jquery - 按字母顺序将行插入到表格中的某个单元格上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6429220/

相关文章:

javascript - 如何正确编写递归 jquery promise

jQuery - 选项卡内的选项卡?

javascript - "Button"和 ":button"选择器的问题

r - 使用 XML R 包用图像抓取 html 表

mysql - 在mysql中将数据从一个表插入另一个表

MySQL插入记录

javascript - 使用 javascript 调用内联函数时出错

表单输入的 HTML 表格与列表

Javascript 从 html 表的特定行获取列的文本值

java - 使用 astyanax 在 cassandra 中插入列表