2010-12-22 50 views
7

我想通過信息我的第4和第5場排序我的輸入Attr值;的jQuery的tablesorter - 通過<輸入值=「值」>排序字段

這是我的html:

<table class=tablesorter width="764" border=1 cellpadding=0 cellspacing=0 id="objective3"> 
    <thead> 
    <tr> 
     <th bgcolor="#396FAE" class="divtopheader1">Strategy</th> 
     <th bgcolor="#396FAE" class="divtopheader1">Objective</th> 
     <th bgcolor="#396FAE" class="divtopheader1">Status</th> 
     <th bgcolor="#396FAE" class="divtopheader1">Target Date</th> 
     <th bgcolor="#396FAE" class="divtopheader1">Target</th> 
     <th bgcolor="#396FAE" class="divtopheader1">Actual</th> 
     <th bgcolor="#396FAE" class="divtopheader1">Cumulative</th> 
    </tr> 
    </thead> 
    <tbody> 
    <tr> 
     <td align="left" valign="top" class="tvertheadersm">Conservation</td> 
     <td width="27%" class="tvertheadersm">statutory authority.</td> 
     <td width="8%" align="center" valign="middle" class="tbody2"> 
      <input type=hidden value="1"> 
      <thewordIMGshouldgohere src="images/1" alt="Objective met" width=30 height=40 /> 
     </td> 
     <td width="11%" align=center class="tbody2"> 
      <input type=hidden value="092010">September<br>2010</td> 
      <td align=center class="tbody2">14 agencies</td> 
     <td align=center class="tbody2">14 agencies</td> 
     <td align=center class="tbody2">0 agencies</td> 
    </tr> 

這是我的jquery,在這裏我只嘗試第五個領域,但不工作:

$(document).ready(function() { 
    // add parser through the tablesorter addParser method 
    $.tablesorter.addParser({ 
     // set a unique id 
     id: 'input', 
     is: function(s) { 
      // return false so this parser is not auto detected 
      return false; 
     }, 
     format: function(s) { 
      // format your data for normalization 
      return $("td input",$(s)).attr("value"); 
     }, 
     // set type, either numeric or text 
     type: 'numeric' 
    }); 

    $("table").tablesorter({ 
     // pass the headers argument and assing a object 
     headers: { 
      // assign the secound column (we start counting zero) 
      5: { 
       sorter:'input' 
      } 
     } 
    }); 
}); 

任何幫助,歡迎!

節日快樂:-)

內斯托爾

回答

6

「S」 傳遞給Format()函數是與細胞內容的字符串。你應該重寫你的函數看起來像這樣

format: function(s) { 
    // format your data for normalization 
    return $($.trim(s)).val(); 
} 

更新:採取仔細看看你的代碼,並在tablesorter.addParser

它看起來像格式()被調用3個參數,第三個是一個它應用的單元格。考慮到這一點,代碼可以寫成這樣:

format: function(s, table, cell) { 
    return $('input', cell).val(); 
} 

http://jsfiddle.net/RyCWM/1/

+0

德語,感謝您的及時回覆。這是一個很棒的工具。我不知道這件事。謝謝你的聖誕老人! – user551799 2010-12-23 01:05:58

1

如果你想在當前輸入的內容進行排序,你應該在平變化事件調用INPUT $(「表」)。觸發器( 「更新」); 解析器必須然後看起來像這樣:

$(document).ready(function() { 
     $.tablesorter.addParser({ 
      // add parser through the tablesorter addParser method$.tablesorter.addParser({ 
      // set a unique id 
      id:'input', 
      is:function (s) { 
       // return false so this parser is not auto detected 
       return false; 
      }, 
      format:function (s) { 
       // format your data for normalization 
       var obj=$($.trim(s)); 
       var id=obj[0].id; 
       var text=$("#"+id)[0].value; 
       return text; 
      }, 
      // set type, either numeric or text 
      type:'text' 
     }); 

     $("table").tablesorter({ 
      // pass the headers argument and assing a object 
      headers:{ 
       3:{ 
        sorter:'input' 
       } 
      } 
     }); 
    });