表分拣机与分隔轨道

时间:2013-09-16 21:04:14

标签: jquery ruby-on-rails sorting html-table tablesorter

我有一个包含一个名为Goal的列的表,该表会像这样填充ruby:

<table  data-sorted="myTable">
  <thead>
    <tr>
      <th><a rel="tooltip" title="Name">Name</a></th>
      <th>subname</th>
      <th>Days</th>
      <th>%</th>
      <th>Goal</th>
      <th>achieved</th>
    </tr>
  </thead>
<tbody>
 <% @results.each do |id, rows| %>
 <% rows.each do |row| %>
    <tr>
      <td>
        <b><%= row[:name] %></b></td>
      <td><%= row[:subname] %></td>
      <td><%= row[:days] %></td>
      <td class="<%= status_indicator(row[:percentage].to_f) %>"><%= number_to_percentage(row[:percentage], :precision => 2)%></td>
      <td class="{sorter: 'thousands'}"="<%= row[:goal].to_i %>"><%=number_with_delimiter(row[:goal].to_i) %></td>
      <td class="achieved"><%= row[:achieved].to_i %></td>
    </tr>
  <% end %>
<% end %>
  </tbody>
</table>

根据我的在线研究,我在jquery上有这个。

$.tablesorter.addParser({ 
    // set a unique id 
    id: 'thousands',
    is: function(s) { 
        // return false so this parser is not auto detected 
        return /^[0-9]?[0-9,\.]*$/.test(s);
    }, 
    format: function(s) {
        // format your data for normalization 
        return jQuery.tablesorter.formatFloat( s.replace(/,/g,'') );
    }, 
    // set type, either numeric or text 
    type: 'numeric' 
}); 

$(function() {
    $("[data-sorted=myTable]").tablesorter({
        headers: {
            6: {//zero-based column index
                sorter:'thousands'
            }
        }
    });
});

排序适用于表格的所有列,但是在我有一千个分隔符的那些列中,我得到了这个结果

  1. 这是表格加载时的方式
  2. this is how the table starts on load

    1. 这是表格在排序时的呈现方式
    2. this is how the table renders when sorted

1 个答案:

答案 0 :(得分:1)

看起来header选项将“千位”分拣机分配给第7列(从零开始的索引)

headers: {
    6: {//zero-based column index
        sorter:'thousands'
    }
}

元数据将其分配到第5列的位置

<td class="{sorter: 'thousands'}"...

因此,您需要添加meta-data plugin以使类名生效,或者更改标题选项以定位正确的列:

headers: {
    4: {//zero-based column index
        sorter:'thousands'
    }
}
相关问题