Files
vxe-table/lib/tools/src/utils.js
xuliangzhan d5cd113d36 update
2019-07-23 12:53:13 +08:00

189 lines
5.8 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = exports.UtilTools = void 0;
var _xeUtils = _interopRequireDefault(require("xe-utils"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var columnUniqueId = 0;
var ColumnConfig = function ColumnConfig(_vm) {
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
renderHeader = _ref.renderHeader,
renderCell = _ref.renderCell,
renderData = _ref.renderData;
_classCallCheck(this, ColumnConfig);
Object.assign(this, {
// 基本属性
id: "col--".concat(++columnUniqueId),
type: _vm.type,
prop: _vm.prop,
property: _vm.field || _vm.prop,
title: _vm.title,
label: _vm.label,
width: _vm.width,
minWidth: _vm.minWidth,
resizable: _vm.resizable,
fixed: _vm.fixed,
align: _vm.align,
headerAlign: _vm.headerAlign,
showOverflow: _vm.showOverflow,
showHeaderOverflow: _vm.showHeaderOverflow,
indexMethod: _vm.indexMethod,
formatter: _vm.formatter,
sortable: _vm.sortable,
sortBy: _vm.sortBy,
remoteSort: _vm.remoteSort,
filters: (_vm.filters || []).map(function (_ref2) {
var label = _ref2.label,
value = _ref2.value,
data = _ref2.data;
return {
label: label,
value: value,
data: data,
_data: data,
checked: false
};
}),
filterMultiple: _xeUtils.default.isBoolean(_vm.filterMultiple) ? _vm.filterMultiple : true,
filterMethod: _vm.filterMethod,
filterRender: _vm.filterRender,
treeNode: _vm.treeNode,
columnKey: _vm.columnKey,
editRender: _vm.editRender,
// 自定义参数
params: _vm.params,
// 渲染属性
visible: true,
level: 1,
rowSpan: 1,
colSpan: 1,
order: null,
renderWidth: 0,
renderHeight: 0,
resizeWidth: 0,
renderLeft: 0,
model: {},
renderHeader: renderHeader || _vm.renderHeader,
renderCell: renderCell || _vm.renderCell,
renderData: renderData,
// 单元格插槽,只对 grid 有效
slots: _vm.slots,
own: _vm
});
};
var UtilTools = {
getSize: function getSize(_ref3) {
var size = _ref3.size,
$parent = _ref3.$parent;
return size || ($parent && ['medium', 'small', 'mini'].indexOf($parent.size) > -1 ? $parent.size : null);
},
getRowKey: function getRowKey($table) {
// let { rowKey, selectConfig = {}, treeConfig = {}, expandConfig = {}, editConfig = {} } = $table
// if (!rowKey) {
// rowKey = selectConfig.key || treeConfig.key || expandConfig.key || editConfig.key
// }
// return rowKey
return $table.rowKey || $table.rowId;
},
getRowPrimaryKey: function getRowPrimaryKey($table, row) {
var rowId = _xeUtils.default.get(row, UtilTools.getRowKey($table));
return rowId ? encodeURIComponent(rowId) : '';
},
// 触发事件
emitEvent: function emitEvent(_vm, type, args) {
if (_vm.$listeners[type]) {
_vm.$emit.apply(_vm, [type].concat(args));
}
},
// 获取所有的列,排除分组
getColumnList: function getColumnList(columns) {
var result = [];
columns.forEach(function (column) {
if (column.children && column.children.length) {
result.push.apply(result, UtilTools.getColumnList(column.children));
} else {
result.push(column);
}
});
return result;
},
formatText: function formatText(value, placeholder) {
return '' + (value === null || value === void 0 ? placeholder ? ' ' : '' : value);
},
getCellValue: function getCellValue(row, column) {
return _xeUtils.default.get(row, column.property);
},
getCellLabel: function getCellLabel(row, column, params) {
var formatter = column.formatter;
var cellValue = UtilTools.getCellValue(row, column);
if (params && formatter) {
if (_xeUtils.default.isString(formatter)) {
return _xeUtils.default[formatter](cellValue);
} else if (_xeUtils.default.isArray(formatter)) {
return _xeUtils.default[formatter[0]].apply(_xeUtils.default, [cellValue].concat(formatter.slice(1)));
}
return formatter(Object.assign({
cellValue: cellValue
}, params));
}
return cellValue;
},
setCellValue: function setCellValue(row, column, value) {
return _xeUtils.default.set(row, column.property, value);
},
getColumnConfig: function getColumnConfig(_vm, options) {
return _vm instanceof ColumnConfig ? _vm : new ColumnConfig(_vm, options);
},
// 组装列配置
assemColumn: function assemColumn(_vm) {
var $table = _vm.$table,
$parent = _vm.$parent,
columnConfig = _vm.columnConfig;
var parentColumnConfig = $parent.columnConfig;
columnConfig.slots = _vm.$scopedSlots;
if (parentColumnConfig && $parent.$children.length > 0) {
if (!parentColumnConfig.children) {
parentColumnConfig.children = [];
}
parentColumnConfig.children.splice([].indexOf.call($parent.$el.children, _vm.$el), 0, columnConfig);
} else {
$table.collectColumn.splice([].indexOf.call($table.$refs.hideColumn.children, _vm.$el), 0, columnConfig);
}
},
// 销毁列
destroyColumn: function destroyColumn(_vm) {
var $table = _vm.$table,
columnConfig = _vm.columnConfig;
var matchObj = _xeUtils.default.findTree($table.collectColumn, function (column) {
return column === columnConfig;
});
if (matchObj) {
matchObj.items.splice(matchObj.index, 1);
}
},
hasChildrenList: function hasChildrenList(item) {
return item && item.children && item.children.length > 0;
}
};
exports.UtilTools = UtilTools;
var _default = UtilTools;
exports.default = _default;