JavaScript练习三之简易表格排序

项目中有要用到对表格进行排序的操作,比如现价,涨幅等等,之前的想法很简单,点一下表头,然后清空,再对这些数据进行重新排序,最后再渲染一遍,非常麻烦。后来转念一想,DOM节点也是对象啊,似乎可以也可以排序,试了一下,bingo!

感悟 : 改变思维,快速排序


 // @charset "utf-8";
 /**
 * 表格排序(练习)
 * @author  应杲臻(yinggaozhen@myhexin.com)
 * @create  2015-11-12 09:34
 */
 (function($, window) {
     var ext               = 'gSort';
     var bodyItemSuffix  = 'bdData';
     
     var defaultOptions = {
             sortHeadDom     : '',
             sortBodyDom     : '',
             sortHeadTag     : 'g_sort_tag',
             sortBodyItem     : 'tr',
             onLoadSort      : 'desc',
             defaultClassSortMap : {
                 'up'          : 'asc',
                 'down'      : 'desc'
             }
     }
     
      function isExsitDom(o) {
         return $(o).length > 0 ? true : false;
      }    
     
     var gSort = function(options) {
         var me = this,
             options = $.extend({}, defaultOptions, options);
         
         if (!isExsitDom(options.sortBodyDom) || !isExsitDom(options.sortHeadDom)) return;
         me.options = options;
         return me;
     };
     
     gSort.prototype = {
         init : function() {
             var me = this;
             me.$hEl = $(me.options.sortHeadDom);
             me.$bEl = $(me.options.sortBodyDom);
             
             me.recordBodyItems();
             me.bindHeadTagClickEvt();
             return me;
         },
         recordBodyItems : function() {
             var me = this;
             
             if (typeof me.$bEl.data(bodyItemSuffix) == 'undefined') {
                 var $bodyItems = me.$bEl.find(me.options.sortBodyItem);
                 
                 if (!isExsitDom($bodyItems)) return;
                 me.$bEl.data(bodyItemSuffix, $bodyItems);
             }
             return me;
         },
         bindHeadTagClickEvt : function () {
             var me = this,
                 headTag = me.options.sortHeadTag || '';
             
             var $tagHeads = me.$hEl.find('[' + headTag + ']');  
             if (!isExsitDom($tagHeads)) return;
             $tagHeads.unbind().bind('click', function() {
                 var $choseHead = $(this),
                     tagIndex = $choseHead.parent().index();
                 
                 me.gSort(tagIndex, me.opearteHeadStatus($choseHead, 'change'));
             })
         },
         opearteHeadStatus : function(o, opearte) {
             var me = this,
                 opearte = opearte || 'get';
             
             if (opearte == 'change') {
                 var status = me.opearteHeadStatus(o);
                 status == 'down' ? o.find('i').attr('class', 'up') : o.find('i').attr('class', 'down');
                 return status;
              }
             return o.find('i').attr('class') || '';
         },
          gSort : function(index, sortRule) {
             var me = this,
                  sortRule = me.options.defaultClassSortMap[sortRule] || 'asc',
                 $reDta = me.$bEl.data(bodyItemSuffix);
             
             var sortSymbol = sortRule == 'asc' ? 1 : -1;
             $reDta.sort(function(a, b) {
                 var cpreA = me.getCpreDta(a, index),
                     cpreB = me.getCpreDta(b, index);
                 
                 if (cpreA > cpreB) return 1 * sortSymbol;
                 if (cpreA < cpreB) return -1  * sortSymbol;
                 return 0;
             });
             me.$bEl.html($reDta);
         },
         getCpreDta : function(o, index) {
             return parseFloat($(o).find('td').eq(index).text());
         }
     }
     
     return window[ext] = gSort;
 })($, window);



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值