123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- import _ from 'lodash';
- import moment from 'moment';
- /* 字段定义 */
- export const FieldMeta = (meta, index) => {
- let { field, slots, listOpts, dict, pattern } = meta;
- const { rules, formOpts, order = 0, filterOpts = {} } = meta;
- filterOpts.order = filterOpts.order || order; // 设置filter字段排序
- if (field === undefined) {
- const { name, label, required = false, readonly = false, editable = true, insertable = true } = meta;
- field = { name, label, required, readonly, editable, insertable };
- }
- if (Array.isArray(field)) {
- const [name, label, required = false, readonly = false, editable = true, insertable = true] = field;
- field = { name, label, required, readonly, editable, insertable };
- }
- if (slots === undefined) {
- // 从meta对象中提取slots属性
- const { filter = false, list = true, form = true, nonce = false } = meta;
- slots = { filter, list, form };
- } else if (Array.isArray(slots)) {
- slots = {
- filter: slots.includes('filter'),
- list: slots.includes('list'),
- form: slots.includes('form') || slots.includes('form.nonce'),
- nonce: slots.includes('form.nonce'),
- ext: slots.includes('ext'),
- };
- } else if (_.isObject(slots)) {
- // 处理slots默认值
- const { filter = false, list = true, form = true, nonce = false } = slots;
- slots = { filter, list, form, nonce };
- }
- // 处理formatter
- let { formatter } = meta;
- if (!_.isFunction(formatter) && (_.isObject(formatter) || _.isString(formatter) || Array.isArray(formatter))) {
- let { name, param } = _.isObject(formatter) ? formatter : {};
- if (_.isString(formatter)) {
- [name, param] = formatter.split(':', 2);
- } else if (Array.isArray(formatter)) {
- [name, param] = formatter;
- }
- if (name === undefined) {
- formatter = undefined;
- } else if (name === 'dict' && param === undefined) {
- console.warn(`use ${formatter} formatter must set param,example: 'dict:status' `);
- formatter = undefined;
- } else {
- formatter = { name, param };
- }
- }
- return { index, field, rules, slots, order, formOpts, listOpts, filterOpts, formatter, dict, pattern };
- };
- /* 列表操作列定义 */
- export const Operation = meta => {
- let items = Object.entries(meta);
- if (Array.isArray(meta)) {
- items = Object.values(meta);
- }
- return items.map(item => {
- if (Array.isArray(item)) {
- let [event, label, icon, confirm = false] = item;
- if (_.isBoolean(icon)) {
- confirm = icon;
- icon = undefined;
- }
- return { event, label, icon, confirm };
- }
- return item;
- });
- };
- // 预置formatter函数
- export const formatters = {
- date: param => (row, column, cellValue, index) => {
- if (_.isNumber(cellValue) && cellValue <= 0) {
- return cellValue;
- }
- if (cellValue) {
- const m = moment(cellValue);
- if (m.isValid()) {
- return m.format(param || 'YYYY-MM-DD');
- }
- }
- return cellValue;
- },
- dict: param => {
- return function(row, column, cellValue, index) {
- if (_.isString(cellValue)) {
- return this.$dict(param, cellValue);
- }
- return cellValue;
- };
- },
- bool: param => (row, column, cellValue, index) => {
- if (cellValue === undefined) {
- return '';
- }
- return cellValue ? '是' : '否';
- },
- };
- export const Formatter = (meta, _this) => {
- // 处理formatter
- let { formatter } = meta;
- if (!_.isFunction(formatter) && _.isObject(formatter)) {
- const { name, param } = formatter;
- formatter = formatters[name](param);
- if (_this !== undefined) {
- formatter = formatter.bind(_this);
- }
- }
- if (!_.isFunction(formatter)) {
- formatter = undefined;
- }
- return formatter;
- };
- export const MergeFilters = (meta, _this) => {
- // 生成column filters
- let { formatter, listOpts } = meta;
- if (listOpts && listOpts.filterable && _.isObject(formatter) && formatter.name === 'dict') {
- let items = _this.$dict(formatter.param);
- if (!items || items.length > 20) return undefined;
- const filterMethod = (value, row, column) => {
- const property = column['property'];
- return row[property] === value;
- };
- const filters = items.map(p => ({ text: p.name, value: p.code }));
- return { filters, filterMethod, ...listOpts };
- }
- return listOpts;
- };
|