jquery Tablesorter - 按<input value =“value”/>排序字段

时间:2010-12-22 22:59:51

标签: jquery

我想通过INPUT attr VALUE上的信息对第4和第5字段进行排序

这是我的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,在这里我只尝试第5个字段但是没有工作:

$(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'
            }
        }
    });
});

欢迎任何帮助!!!

节日快乐: - )

内斯托尔

2 个答案:

答案 0 :(得分:6)

传递给format()函数的“s”是一个包含单元格内容的字符串。你应该重写你的功能看起来像这样

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

更新:仔细查看您的代码和tablesorter.addParser

看起来format()用3个参数调用,第3个是它应用的单元格。考虑到这一点,代码可以像这样重写:

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

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

答案 1 :(得分:1)

如果要对当前输入的内容进行排序,则应该在onchange事件上调用INPUT $(“table”)。trigger(“update”); 然后解析器必须看起来像这样:

 $(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'
                }
            }
        });
    });