2014-10-03 93 views
0

Fiddle Example表排列添加

誰能告訴我用什麼後行動態添加到頁面加載一個表來觸發初始排序?

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

var sorting = [[1,0]]; 

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

sorton函數無法觸發「年齡」上的初始排序。

以下是完整的腳本:

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

你試試這個:

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