简体   繁体   English

使用jQuery或Javascript进行自定义表排序

[英]Custom table sort using jquery or Javascript

I have the following table with three columns (ID, Date, and Source). 我的下表有三列(ID,日期和源)。 I want to sort this table based on the Source Column . 我想根据“ 源列”对该表进行排序。 I don't want to sort either alphabetic or numeric. 我不想按字母或数字排序。 I want them sorted like all the rows from ON sources will appear first in the table, after then all the rows from MB source, and so on using either Javascript or jquery . 我希望它们的排序方式像ON源中的所有行都将首先出现在表中,然后MB源中的所有行都将出现在表中,依此类推,使用Javascriptjquery I searched almost all the threads related to sorting. 我搜索了几乎所有与排序相关的线程。 But I failed to solve this problem. 但是我没有解决这个问题。 I am new to this so please help me to get this done. 我对此并不陌生,所以请帮助我完成这项工作。 Your help is really appreciated. 非常感谢您的帮助。

<table>
 <tr>
    <th>ID</th>
    <th>Date</th>
    <th>Source</th>
  </tr>
  <tr>
    <td>50</td>
    <td>2012-01-01</td>
    <td>ON</td>
  </tr>
  <tr>
    <td>94</td>
    <td>2013-06-05</td>
    <td>MB</td>
  </tr>
  <tr>
    <td>80</td>
    <td>2011-07-08</td>
    <td>AB</td>
  </tr>
  <tr>
    <td>50</td>
    <td>2012-01-01</td>
    <td>ON</td>
  </tr>
  <tr>
    <td>50</td>
    <td>2012-01-01</td>
    <td>MB</td>
  </tr>
</table>

Can make array of the order since source is known and use that array index to sort by 由于来源已知,可以制作顺序数组,并使用该数组索引进行排序

var sorters =['ON','AB','MB']

var $rows = $('tr:gt(0)').sort(function(a, b){
    var aSrcIdx =sorters.indexOf( $(a).find('td:last').text() );
    var bSrcIdx = sorters.indexOf( $(b).find('td:last').text());

    return aSrcIdx >  bSrcIdx;    
});

$('table').append($rows);

I left the html alone but it could certainly be improved with data attributes to make this more efficient 我独自留下了html,但是可以肯定地通过数据属性对其进行改进,以使其更加高效

DEMO 演示

Okay here's how I've done something like this. 好的,这就是我做这样的事情的方式。 Given the element a data-* attribute with an integer value of the priority. 给定元素一个data- *属性,其优先级为整数。

Then sort them using js' sort() function like so: 然后使用js的sort()函数对它们进行sort()如下所示:

$(elementsToBeSorted).sort(function(a, b){
    return $(a).attr('data-priority') - $(b).attr('data-priority');
}).appendTo('body');

In your case, you could do something like this: 在您的情况下,您可以执行以下操作:

$('tr').not($('.th').parents('tr')).sort(function(a, b){
    return $(a).children('td').last().text() - $(b).children('td').last().text();
}).appendTo('table');

This sorts all rows that are do not contain headings by text within the last table-data element. 这将按最后一个表数据元素中的文本对不包含标题的所有行进行排序。

I'd recommend to sort source data before putting it into the DOM. 我建议先将源数据排序后再放入DOM。 Assuming data is array to display, you can write something like this: 假设data是要显示的数组,则可以编写如下内容:

var data = [
    {id: 50, date: '2012-01-01', source: 'ON'},
    {id: 94, date: '2013-06-05', source: 'MB'}
];

var sortedData = data.sort(function (a, b) {
    var weights = ['ON','MB'];

    return weights.indexOf(a.source) - weights.indexOf(b.source);
});

This way array sortedData will be ordered according the order of elements in weights . 这样,数组sortedData将根据weights中元素的顺序进行排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM