Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用jquery对HTML表中的列重新排序_Javascript_Jquery_Html Table - Fatal编程技术网

Javascript 使用jquery对HTML表中的列重新排序

Javascript 使用jquery对HTML表中的列重新排序,javascript,jquery,html-table,Javascript,Jquery,Html Table,有一个html表,其中创建为 <table width="100%" border="1" cellpadding="0" cellspacing="0"> <tr> <th>Sl.No</th> <th>Name</th> <th>Dec 2013</th> <th>Feb 2014</th>

有一个html表,其中创建为

<table width="100%" border="1" cellpadding="0" cellspacing="0">
    <tr>
        <th>Sl.No</th>
        <th>Name</th>
        <th>Dec 2013</th>
        <th>Feb 2014</th>
        <th>Jan 2014</th>
        <th>Mar 2014</th>
        <th>Nov 2013</th>
        <th>Total</th>
    </tr>
    <tr>
        <td>1</td>
        <td>foo</td>
        <td>4</td>
        <td>7</td>
        <td>3</td>
        <td>5</td>
        <td>2</td>
        <td>21</td>
    </tr>
    <tr>
        <td>2</td>
        <td>bar</td>
        <td>6</td>
        <td>1</td>
        <td>5</td>
        <td>8</td>
        <td>3</td>
        <td>23</td>
    </tr>
</table>

序号
名称
2013年12月
2014年2月
2014年1月
2014年3月
2013年11月
全部的
1.
福
4.
7.
3.
5.
2.
21
2.
酒吧
6.
1.
5.
8.
3.
23
如何使用jquery对列进行重新排序,以便新表中列的顺序变为
Sl.No,Name,2013年11月,2013年12月,2014年1月,2014年2月,2014年3月,总计
。服务器根据日期选择动态生成月份列(
日期)


为什么不在服务器上对数据进行排序?我建议您在用于检索数据的查询中使用
orderby
!如果客户端有JS负载,这将减少JS负载@未定义-服务器端的Me排序数据。但在客户端,它是按字母顺序排列的。比如2013年12月
2014年2月
等。
var arr = $('th').sort(function(a, b) {
   return new Date(a.innerHTML) > new Date(b.innerHTML);
}).map(function() { return this.cellIndex }).get();

$('tr').each(function() {
    $(this.cells).sort(function(a, b) {
        a = $.inArray(a.cellIndex, arr);
        b = $.inArray(b.cellIndex, arr);
        return a > b;
    }).prependTo(this);
});