import { Component, Vue, Prop } from 'vue-property-decorator'
import moment from 'moment';
@Component
export default class ColumnContent extends Vue {
@Prop() scope: any;
@Prop() config: any;
/**
* 数据更新
*/
onDataUpdate(options: any) {
this.scope.row[this.config.key] = options.value;
}
protected render() {
const { row, $index, column } = this.scope;
const { key, type } = (this.config || {}) as any;
// 处理样式格式
const style: any = {};
let currentValue = row[key];
if (this.config.formatter) {
const currentDetail = this.config.formatter(row, column, $index);
if (currentDetail && currentDetail.color) {
style.borderColor = currentDetail.color;
style.color = currentDetail.color;
style.background = currentDetail.background;
}
if (currentDetail && currentDetail.value) currentValue = currentDetail.value
}
if (currentValue instanceof Array) {
currentValue = currentValue.join(' ' + this.config.split + ' ' || ', ');
}
// 如果是输入框,编辑中情况
if (this.config && (this.config.isInput || this.config.inputType) && row.isEdit) {
const inputConfig = {
...this.config,
type: this.config.inputType || 'text',
key: 'data.' + $index + '.' + key,
label: ""
};
return (
)
}
switch (type) {
case 'link': {
const { url, query } = this.config.linkUrl(row);
const addQuery = (query) => {
if (query && Object.keys(query).length >= 1) {
const str = '&';
let str2 = '';
for (const k in query) {
let querystring = `${k}=${query[k]}`;
querystring = querystring + str;
str2 += querystring;
}
return str2.slice(0, -1);
}
};
return ({currentValue})
}
case 'route': {
const formatter = this.config.formatter ? this.config.formatter(row) : "路由跳转";
const { url, query } = this.config.routerUrl(row);
// @todo 有待优化,对url和params进行拼接
const toRoute = { path: url, query: query };
return ({formatter.value})
}
case 'text': {
return ({currentValue});
}
case 'date': {
const currentFormat = this.config.format
? this.config.format.replace(/y/g, 'Y').replace(/d/g, 'D') : 'YYYY-MM-DD HH:mm:ss';
return ({currentValue ? moment(currentValue).format(currentFormat) : '-'});
}
case 'img': {
return (
);
}
case 'tag': {
const config = {
text: currentValue,
size: this.config.fontSize,
radiusable: this.config.radiusable || undefined,
type: this.config.tagType
};
return (
);
}
case 'icon': {
const style = this.config.size ? { fontSize: this.config.size } : {};
return ();
}
case 'input': {
const config = { ...this.config, type: this.config.inputType, key: 'data.' + $index + '.' + key, label: "" };
return (!row.isEdit || !this.config) ? ({currentValue})
: ();
}
case 'operation': {
return ();
}
default:
return ({currentValue});
}
}
}