表格排序功能(不请求后台的方法)

这个方法不用请求后台       

  $(document).on('click','#contrast_list thead tr th',function(){
              var table = $(this).parents('table').eq(0);
              var rows = table.find('tr:gt(0)').toArray().sort(comparer($(this).index()));
              this.asc = !this.asc;
                $(this).removeClass("sx_an");//这个是上下的样式(默认是显示两个图标的样式)
                  $(this).addClass("sx_xia");
              if (!this.asc){
                  $(this).removeClass("sx_xia");
                  $(this).addClass("sx_shang");
                  rows = rows.reverse();}

              table.children('tbody').empty().html(rows);
          });
          function comparer(index) {
              return function(a, b) {
                  var valA = getCellValue(a, index), valB = getCellValue(b, index);
                  return $.isNumeric(valA) && $.isNumeric(valB) ?
                      valA - valB : valA.localeCompare(valB);
              };
          }
          function getCellValue(row, index){
              return $(row).children('td').eq(index).text();
          }

posted @ 2017-06-12 16:58  南城旧梦j.m  阅读(373)  评论(0编辑  收藏  举报