Skip to content

Commit 13cd106

Browse files
author
黄书伟
committed
修复在IE9 下的bug
1 parent a69bdaa commit 13cd106

File tree

6 files changed

+121
-33
lines changed

6 files changed

+121
-33
lines changed

libs/src/utils/dom.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
exports.hasClass = hasClass;
7+
exports.addClass = addClass;
8+
exports.removeClass = removeClass;
9+
function hasClass(el, cls) {
10+
if (!el || !cls) return false;
11+
if (cls.indexOf(' ') !== -1) throw new Error('className should not contain space.');
12+
if (el.classList) {
13+
return el.classList.contains(cls);
14+
} else {
15+
return (' ' + el.className + ' ').indexOf(' ' + cls + ' ') > -1;
16+
}
17+
};
18+
19+
function addClass(el, cls) {
20+
if (!el || !cls) return;
21+
22+
if (el.classList) {
23+
el.classList.add(cls);
24+
} else {
25+
26+
var clsArr = el.className.split(" ");
27+
28+
if (clsArr.indexOf(cls) === -1) {
29+
el.className += " " + cls;
30+
}
31+
}
32+
};
33+
34+
function removeClass(el, cls) {
35+
if (!el || !cls) return;
36+
37+
if (el.classList) {
38+
el.classList.remove(cls);
39+
} else {
40+
41+
var reg = new RegExp('(\\s|^)' + cls + '(\\s|$)');
42+
el.className = el.className.replace(reg, ' ');
43+
}
44+
};

libs/v-table/src/cell-edit-mixin.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ var _utils = require('../../src/utils/utils.js');
88

99
var _utils2 = _interopRequireDefault(_utils);
1010

11+
var _dom = require('../../src/utils/dom.js');
12+
1113
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
1214

1315
exports.default = {
@@ -28,11 +30,11 @@ exports.default = {
2830
target = target.parentNode;
2931
}
3032

31-
if (target.classList.contains('cell-editing')) {
33+
if ((0, _dom.hasClass)(target, 'cell-editing')) {
3234
return false;
3335
}
3436

35-
target.classList.add('cell-editing');
37+
(0, _dom.addClass)(target, 'cell-editing');
3638

3739
oldVal = target.innerText;
3840

@@ -58,19 +60,20 @@ exports.default = {
5860

5961
_actionFun = function actionFun(e) {
6062

61-
if (typeof e.keyCode === 'undefined' || e.keyCode == 13) {
63+
if (typeof e.keyCode === 'undefined' || e.keyCode === 0 || e.keyCode == 13) {
64+
65+
if ((0, _dom.hasClass)(target, 'cell-editing')) {
6266

63-
if (target.classList.contains('cell-editing')) {
64-
target.classList.remove('cell-editing');
67+
(0, _dom.removeClass)(target, 'cell-editing');
6568
} else {
6669
return false;
6770
}
6871

69-
formatterVal = self.cellEditFormatter && self.cellEditFormatter(this.value, oldVal, rowIndex, rowData, field);
72+
formatterVal = self.cellEditFormatter && self.cellEditFormatter(editInput.value, oldVal, rowIndex, rowData, field);
7073

71-
target.innerHTML = formatterVal && formatterVal.length > 0 ? formatterVal : this.value;
74+
target.innerHTML = formatterVal && formatterVal.length > 0 ? formatterVal : editInput.value;
7275

73-
callback(this.value, oldVal);
76+
callback(editInput.value, oldVal);
7477

7578
_utils2.default.unbind(editInput, 'blur', _actionFun);
7679
_utils2.default.unbind(editInput, 'keydown', _actionFun);

libs/v-table/src/table.vue

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -760,11 +760,10 @@
760760
761761
// 对外暴露(隐藏显示切换时)
762762
resize(){
763-
764-
this.$nextTick(x => {
763+
// fixed bug in IE9 #17
764+
setTimeout(x=>{
765765
766766
this.tableResize();
767-
768767
})
769768
}
770769
},

packages/src/utils/dom.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
// has class
3+
export function hasClass(el, cls) {
4+
if (!el || !cls) return false;
5+
if (cls.indexOf(' ') !== -1) throw new Error('className should not contain space.');
6+
if (el.classList) {
7+
return el.classList.contains(cls);
8+
} else {
9+
return (' ' + el.className + ' ').indexOf(' ' + cls + ' ') > -1;
10+
}
11+
};
12+
13+
// add class
14+
export function addClass(el, cls) {
15+
if (!el || !cls) return;
16+
17+
if (el.classList) {
18+
el.classList.add(cls);
19+
20+
}else{
21+
22+
var clsArr = el.className.split(" ");
23+
24+
if (clsArr.indexOf(cls) === -1) {
25+
el.className += " " + cls;
26+
}
27+
}
28+
};
29+
30+
// remove class
31+
export function removeClass(el, cls) {
32+
if (!el || !cls) return;
33+
34+
if (el.classList) {
35+
el.classList.remove(cls);
36+
} else {
37+
38+
var reg = new RegExp('(\\s|^)' + cls + '(\\s|$)');
39+
el.className = el.className.replace(reg, ' '); // For IE9 and earlier
40+
}
41+
};

packages/v-table/src/cell-edit-mixin.js

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import utils from '../../src/utils/utils.js'
2+
import {hasClass, addClass, removeClass} from '../../src/utils/dom.js'
3+
24
export default {
35

46
methods: {
57
// cell edit
6-
cellEdit(e,callback,rowIndex,rowData,field){
8+
cellEdit(e, callback, rowIndex, rowData, field){
79

810
let target = e.target,
911
self = this,
@@ -18,15 +20,15 @@ export default {
1820
target = target.parentNode;
1921
}
2022

21-
if (target.classList.contains('cell-editing')) {
23+
if (hasClass(target, 'cell-editing')) {
2224
return false
2325
}
2426

25-
target.classList.add('cell-editing');
27+
addClass(target, 'cell-editing');
2628

2729
oldVal = target.innerText;
2830

29-
if (target.style.textAlign){
31+
if (target.style.textAlign) {
3032

3133
textAlign = target.style.textAlign;
3234
}
@@ -50,45 +52,45 @@ export default {
5052

5153
actionFun = function (e) {
5254

53-
if (typeof e.keyCode === 'undefined' || e.keyCode == 13){
54-
55+
if (typeof e.keyCode === 'undefined' || e.keyCode === 0 || e.keyCode == 13) {
5556

56-
if (target.classList.contains('cell-editing')) {
57-
target.classList.remove('cell-editing');
57+
if (hasClass(target, 'cell-editing')) {
5858

59-
}else{
59+
removeClass(target, 'cell-editing');
60+
} else {
6061
return false;
6162
}
6263

63-
formatterVal = self.cellEditFormatter && self.cellEditFormatter(this.value,oldVal,rowIndex,rowData,field);
64+
formatterVal = self.cellEditFormatter && self.cellEditFormatter(editInput.value, oldVal, rowIndex, rowData, field);
6465

65-
target.innerHTML = formatterVal && formatterVal.length > 0 ? formatterVal :this.value;
66+
target.innerHTML = formatterVal && formatterVal.length > 0 ? formatterVal : editInput.value;
6667

67-
callback(this.value,oldVal)
68+
// fixed this.value bug in IE9
69+
callback(editInput.value, oldVal)
6870

6971
utils.unbind(editInput, 'blur', actionFun);
70-
utils.unbind(editInput, 'keydown',actionFun);
72+
utils.unbind(editInput, 'keydown', actionFun);
7173
}
7274
};
7375

7476

7577
utils.bind(editInput, 'blur', actionFun);
76-
utils.bind(editInput, 'keydown',actionFun);
78+
utils.bind(editInput, 'keydown', actionFun);
7779
},
7880

7981
// 单元格点击
80-
cellEditClick(e,isEdit,rowData,field,rowIndex){
82+
cellEditClick(e, isEdit, rowData, field, rowIndex){
8183

82-
if (isEdit){
84+
if (isEdit) {
8385

8486
let self = this;
8587
// 单元格内容变化后的回调
86-
let onCellEditCallBack = function (newValue,oldVal) {
88+
let onCellEditCallBack = function (newValue, oldVal) {
8789

88-
self.cellEditDone(newValue,oldVal,rowIndex,rowData,field);
90+
self.cellEditDone(newValue, oldVal, rowIndex, rowData, field);
8991
}
9092

91-
this.cellEdit(e,onCellEditCallBack,rowIndex,rowData,field)
93+
this.cellEdit(e, onCellEditCallBack, rowIndex, rowData, field)
9294
}
9395
},
9496
}

packages/v-table/src/table.vue

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -760,11 +760,10 @@
760760
761761
// 对外暴露(隐藏显示切换时)
762762
resize(){
763-
764-
this.$nextTick(x => {
763+
// fixed bug in IE9 #17
764+
setTimeout(x=>{
765765
766766
this.tableResize();
767-
768767
})
769768
}
770769
},

0 commit comments

Comments
 (0)