添加新行后的表排序

时间:2014-10-03 09:05:21

标签: javascript html

Fiddle Example

任何人都可以告诉我在页面加载后动态添加到表中的行后触发初始排序会使用什么?

$('.tablesorter2').trigger('update');

var sorting = [[1,0]]; 

$('.tablesorter2').trigger('sorton',sorting);

sorton函数无法触发" Age"的初始排序。

这是完整的脚本:

var options = {
        theme: 'default',
        widgets: ['editable'],
        widgetOptions : {
          editable_columns       : [0,1,2],      
          editable_enterToAccept : true,          
          editable_autoAccept    : true,          
          editable_autoResort    : false,         
          editable_validate      : null,         
          editable_noEdit        : 'no-edit',     
          editable_editComplete  : 'editComplete'
       }
}
$('.tablesorter2').tablesorter(options).children('tbody').on('editComplete','td', function(event, config){
    var $this = $(this),
        tr = $this.parent('tr'),
        $allRows = config.$tbodies.children('tr'), 
        tid = tr.data('tid'),
        input="",
        name = $(this).data('name'),
        newContent = $(this).text();
        input = '<input value="'+newContent+'" name="'+name+'[]" type="hidden">';

        $this.find('.j_input').html(input);
    });

$('button').click(function(){
    var item_html = "";
    item_html += '<tr><td>Mary</td><td>5</td><td>Good</td><td>...</td><td><input type="checkbox"/></td></tr>';
   $('tbody').append(item_html);
    $('.tablesorter2').trigger('update');
    var sorting = [[1,0]]; 
    $('.tablesorter2').trigger('sorton',sorting);
});

HTML:

<button>Add</button>
<table class="tablesorter2">
    <thead>
    <tr data-tid='12'>
        <th>Name</th>
        <th>Age</th>
        <th>Conditions</th>
        <th>Notes</th>
        <th>Del</th>
    </tr>
    </thead>
    <tbody>
        <tr data-tid='13'>
            <td><div>Peter</div></td>
            <td contenteditable="true" data-name='age'>18<span class='j_input'></span></td>
            <td contenteditable="true" data-name='conditions'>Good<span class='j_input'></span></td>
            <td contenteditable="true" data-name='notes'>...<span class='j_input'></span></td>
            <td><input type='checkbox'/></td>
        </tr>
         <tr>
            <td>Tom</td>
            <td data-name='age'>12<span class='j_input'></span></td>
            <td contenteditable="true" data-name='conditions'>Good<span class='j_input'></span></td>
            <td contenteditable="true" data-name='notes'>On medication<span class='j_input'></span></td>
            <td><input type='checkbox'/></td>
        </tr>
    </tbody></table>

1 个答案:

答案 0 :(得分:1)

你试过这个:

$('.tablesorter2').trigger('sorton',[sorting]);