2010-12-22 54 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' 
       } 
      } 
     }); 
    });