-
Notifications
You must be signed in to change notification settings - Fork 0
/
resize.js
188 lines (181 loc) · 7.53 KB
/
resize.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
(function () {
var ResizeColumns = function (settings) {
this.options = {
target: '',
colMinWidth: 35,
noResizeClass: 'js-no-resize',
resizeClass: 'g-clip',
columnName: 'resize-column-name'
}
$.extend(this.options, settings);
if (this.options.target == '') {
console.error('please set resize tables!')
return;
} else {
var $table = $(this.options.target);
$table.data('resizeColumns', this.init($table));
}
};
$.extend(ResizeColumns.prototype, {
init: function ($table) {
var _this = this;
var $ths = $table.find('th:not(.' + _this.options.noResizeClass + '):visible');
var $tbodyTrs = $table.find('tbody').find('tr');
if ($tbodyTrs.length > 0 && $ths.length > 0) {
$ths.splice(0, 1);
$ths.addClass(_this.options.resizeClass);
$tbodyTrs.each(function () {
var $tds = $(this).find('td:not(.' + _this.options.noResizeClass + '):visible');
$tds.splice(0, 1);
$tds.addClass(_this.options.resizeClass);
})
}
_this.$table = $table;
_this.tableId = this.$table.data('resize-columns-id');
_this.mousedown = this.bind(this.mousedown, this);
_this.createHandles();
_this.restoreColumnWidths();
_this.syncHandleWidths();
$(window).on('resize.rc', (function () {
return _this.syncHandleWidths();
}))
return _this;
},
/**
* 销毁拖拽区域
*/
destroy: function () {
this.$handleContainer.remove();
this.$table.removeData('resizeColumns');
this.$table.css({ 'width': 'auto' });
this.$table.find('tr th').filter('.' + this.options.resizeClass).css({ 'width': '', 'min-width': '' });
return $(window).off('.rc');
},
/**
* 创建拖拽区域
*/
createHandles: function () {
var _this = this;
_this.$table.before((_this.$handleContainer = $('<div class="rc-handle-container"></div>')));
this.$table.find('tr th').filter('.' + this.options.resizeClass).each(function (i, el) {
var $handle = $('<div class="rc-handle"></div>');
$handle.data('th', $(el));
return $handle.appendTo(_this.$handleContainer);
})
_this.$table.width(10 + 'px');
return _this.$handleContainer.on('mousedown', '.rc-handle', _this.mousedown);
},
/**
* 鼠标点击事件
*/
mousedown: function (e) {
e.preventDefault();
var _this = this;
var $table = _this.$table;
var tableWidth = $table.width() - 1; // table宽度获取的时候减去1px宽度,否则拖动时会抖动
var $currentGrip = $(e.currentTarget);
var $leftColumn = $currentGrip.data('th');
var leftColumnStartWidth = $leftColumn.width();
_this.startPostion = e.pageX;
$(document).on('mousemove.rc', function (e) {
var difference = e.pageX - _this.startPostion;
var newLeftColumnWidth = leftColumnStartWidth + difference;
if ((parseInt($leftColumn.css('width')) < $leftColumn.width()) && (newLeftColumnWidth < $leftColumn.width())) {
return;
}
// 如果超过最小拖动距离
if (newLeftColumnWidth >= _this.options.colMinWidth) {
$leftColumn.width(newLeftColumnWidth);
_this.$table.width(tableWidth + difference);
return _this.syncHandleWidths();
}
});
return $(document).one('mouseup', function () {
$(document).off('mousemove.rc');
return _this.saveColumnWidths();
})
},
/**
* 同步拖拽区域
*/
syncHandleWidths: function () {
var _this = this;
_this.$handleContainer.width(_this.$table.width());
return _this.$handleContainer.find('.rc-handle').each(function (_, el) {
return $(el).css({
left: $(el).data('th').outerWidth() + ($(el).data('th').offset().left - _this.$handleContainer.offset().left - _this.$handleContainer.offset().left),
height: _this.$table.height()
})
})
},
/**
* 暂存拖拽信息
*/
saveColumnWidths: function () {
var _this = this;
var storage = window.localStorage;
var key = '', value = {}, valueJSON = '';
var path = location.pathname;
if (_this.isLocalStorage()) {
key = path + '-' + _this.$table.attr('id');
_this.$table.find('tr th').filter('.' + _this.options.resizeClass).each(function (_, el) {
var columnName = $(el).attr(_this.options.columnName);
if (columnName != '') {
value[columnName] = $(el).width();
} else {
console.log('表格th属性' + _this.options.columnName + '为空!')
return true;
}
})
if (value) {
valueJSON = JSON.stringify(value);
storage.setItem(key, valueJSON);
}
}
},
/**
* 根据暂存数据同步拖拽位置
*/
restoreColumnWidths: function () {
var _this = this;
var storage = window.localStorage;
var key = '', value = {}, valueJSON = '';
var path = location.pathname;
if (_this.isLocalStorage()) {
key = path + '-' + _this.$table.attr('id');
valueJSON = storage.getItem(key);
value = JSON.parse(valueJSON);
_this.$table.find('tr th').filter('.' + _this.options.resizeClass).each(function (_, el) {
var columnName = $(el).attr(_this.options.columnName);
if (value && columnName != '' && columnName != undefined && value[columnName]) {
$(el).css({ 'width': value[columnName] + 'px' });
} else {
var iMinWidth = parseInt($(el).css('min-width'));
iMinWidth = iMinWidth == 0 ? _this.options.colMinWidth : iMinWidth;
$(el).css({ 'width': iMinWidth + 'px' });
}
// 移除min-width IE下拖拽不能小于min-width
$(el).css({ 'min-width': 'auto' });
})
}
},
isLocalStorage: function () {
var testKey = 'test', testValue = 'testValue';
var storage = window.localStorage;
try {
storage.setItem(testKey, testValue);
storage.removeItem(testKey);
return true;
} catch (error) {
console.log('当前浏览器不支持localStorage');
return false;
}
},
bind: function (fn, me) {
return function () {
return fn.apply(me, arguments);
}
}
})
window.ResizeColumns = ResizeColumns;
})();