表格横向拖动

controlColumnWidthNew = function(config) {
    var config = config;
    var _createGrips = function _createGrips(config) {
        $('.JCLRgrips:visible').remove();
        var div = '<div class="JCLRgrips"></div>';
        var tableTd = $(config.el).find('tr:eq(0) td');
        var height = $(config.el).height();
        if (!tableTd.length) return;
        var length = tableTd.length;
        var verticalBars = [];
        $.each(tableTd, function(index) {
            if (index == length - 1) return false;
            var left = 0;
            for (var i = 0; i < index + 1; i++) {
                left += $(tableTd[i]).outerWidth();
            }
            left -= $('.headerFrozen:visible').outerWidth() || 0;
            var verticalBar = '<div class="JCLRgrip" style="left: ' + left + 'px; height:' + height + 'px"><div class="JColResizer"></div></div>';
            verticalBars.push(verticalBar);
        });
        $(config.el).parent().prepend('<div class="JCLRgrips">' + verticalBars.join('') + '</div>');
        var mouseEvent = function mouseEvent(e) {
            switch (e.type) {
                case 'mousedown':
                    $(this).parent().addClass('dragging');
                    $(this).parent().prev().addClass('dragging-before');
                    $(document).bind('mousemove', mouseEvent);
                    var dragging = $('.dragging');
                    var index = dragging.index();
                    var width = 0;
                    if (index > 0) {
                        width = dragging.offset().left - dragging.parent().find('.JCLRgrip:eq(' + (index - 1) + ')').offset().left;
                    } else {
                        width = dragging.offset().left - $(config.el).offset().left;
                    }
                    $.each($(config.el).find('tr'), function(_, itm) {
                        var dataWidth = $(itm).find('td:eq(' + index + ')').data('width');
                        if (!dataWidth) {
                            $(itm).find('td:eq(' + index + ')').data('width', width);
                        } else {
                            return false;
                        }
                    });
                    document.onmousedown = function() {
                        return false;
                    };
                    break;
                case 'mouseup':
                    var dragging = $('.dragging');
                    var index = dragging.index();
                    if (!dragging.length) return;
                    if (!$(config.el).find('tr:eq(0) td:eq(' + index + ')').hasClass('frozen') && !$(config.el).find('tr:eq(0) td:eq(' + index + ')').hasClass('checkedRow')) {
                        var width = 0;
                        var columnKey
                        if (index > 0) {
                            width = dragging.offset().left - dragging.parent().find('.JCLRgrip:eq(' + (index - 1) + ')').offset().left;
                        } else {
                            width = dragging.offset().left - $(config.el).offset().left;
                        }
                        $.each($(config.el).find('tr'), function(_, itm) {
                            // var dataWidth = $(itm).find('td:eq(' + index + ')').data('width')
                            var dataWidth = 20;
                            if (width < dataWidth) width = dataWidth;
                            $(itm).find('td:eq(' + index + ')').css({
                                'min-width': width,
                                'max-width': width,
                                width: width
                            });
                            console.log("itm:",itm)
                            columnKey = $(itm).find('td:eq(' + index + ')').attr('data-key');
                            console.log("$(itm).find('td:eq(' + index + ')'):",$(itm))
                        });
                        $(config.el).parents('table').find('thead th:eq(' + index + ')').css({
                            'min-width': width,
                            'max-width': width,
                            width: width
                        });
                        if (config.callback) {
                            config.callback();
                        }
                        var postData = {
                            width: width,
                            columnKey: columnKey,
                            tableName: config.tableName
                        }
                        console.log('columnKey3',columnKey)
                        _.util.ajax_get('/table/updateTableWidth', postData, function(res) {
                            if (res.success && config.callBackSetWidth) {
                                config.callBackSetWidth(columnKey, width)
                            }
                        })
                        $(window).resize();
                    }
                    _createGrips(config);

                    $.each($('.dragging').parent().find('.JCLRgrip'), function(index, item) {
                        var left = 0;
                        for (var i = 0; i < index + 1; i++) {
                            left += $('.dragging').parents('.admin-table').find('.sticky-enabled tbody tr:eq(0) td:eq(' + i + ')').outerWidth();
                        }
                        $(item).css({
                            left: left
                        });
                    });
                    $('.dragging').removeClass('dragging');
                    $('.dragging-before').removeClass('dragging-before');
                    document.onmousedown = function() {
                        return true;
                    };
                    $(document).unbind('mousemove');
                    break;
                case 'mousemove':
                    if ($('.dragging').length) {
                        var left = e.clientX - $(config.el).offset().left;
                        $('.dragging').css({
                            left: left
                        });
                    }
                    break;
            }
        };
        $(config.el).parent().find('.JColResizer').on('mousedown', mouseEvent);
        $(config.el).parent().unbind('mouseup').on('mouseup', mouseEvent);
        $.each($('.admin-table').find('.sticky-enabled thead tr:last th'), function(index, item) {
            var width = $(item).outerWidth();
            $('.dragging').parents('.admin-table').find('.sticky-thead thead tr:last th:eq(' + index + ')').css({
                'min-width': width,
                'max-width': width,
                width: width
            });
        });
    };
    _createGrips(config);
};

使用:

//controlColumnWidthNew({
//		el: "#'表格id' tbody" 
//})
controlColumnWidthNew({
	el: '#document-operate-table tbody'   
})
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值