公共部分
import Vue from 'vue'
import $ from 'jquery';
function supplement(str) {
let regExp = new RegExp("(?<=\/|-|\\.|\:|\\b|T)\\d{1}(?=\/|-|\\.|\:|\\b|T)", "g")
return str.replace(regExp, "0$&");
}
时间日期选择器 - (年-月-日)
<el-form-item ref="birthday" prop="birthday">
<el-date-picker v-model="birthday" type="date" v-dateYMDFormat value-format="yyyy-MM-dd"> </el-date-picker>
</el-form-item>
Vue.directive('dateYMDFormat', {
inserted: function (el, binding, vnode) {
const { value: _obj } = binding
const { context: _this, data } = vnode
const { expression: key } = data.model
let arr = []
const modelValue = function (value, len) {
value = value.replace(/[^0-9]/g, '')
if (value.length > 5 && value.length < 9) {
value = value.replace(/^(\d{4})\D*(\d{1,2})\D*(\d{1,2})\D*/, '$1-$2-$3')
} else if (value.length > 9 && value.length < 13) {
value = value.replace(/^(\d{4})\D*(\d{1,2})\D*(\d{1,2})\D*(\d{1,2})\D*(\d{1,2})\D*/, '$1-$2-$3 $4:$5')
} else if (value.length > 12) {
value = value.replace(/^(\d{4})\D*(\d{1,2})\D*(\d{1,2})\D*(\d{1,2})\D*(\d{1,2})\D*(\d{1,2})\D*/, '$1-$2-$3 $4:$5:$6')
} else {
return false
}
value = supplement(value)
const time = value && value.constructor == String ? value : ''
let keys = key.split('.')
if (arr.length === len) {
arr = [];
}
arr.push(time)
if (!_obj) {
if (keys && keys.length >= 2) {
const [key1, key2, key3, key4] = keys
if (key4) {
_this[key1][key2][key3][key4] = len === 2 ? arr : time;
} else if (key3) {
_this[key1][key2][key3] = len === 2 ? arr : time;
} else {
_this[key1][key2] = len === 2 ? arr : time;
}
} else {
_this[key] = len === 2 ? arr : time;
}
} else {
let objKey = _obj.obj.split('.')
if (objKey && objKey.length >= 2) {
const [flag1, flag2, flag3, flag4] = objKey;
if (flag4) {
_this[flag1][flag2][flag3][flag4][_obj.index][_obj.modelName] = len === 2 ? arr : time;
} else if (flag3) {
_this[flag1][flag2][flag3][_obj.index][_obj.modelName] = len === 2 ? arr : time;
} else {
_this[flag1][flag2][_obj.index][_obj.modelName] = len === 2 ? arr : time;
}
} else {
_this[objKey][_obj.modelName] = len === 2 ? arr : time;
}
}
};
if (_this && _this._isVue) {
const $this = $($(el).children('input')[0])
const $this2 = $($(el).children('input')[1])
if ($(el).children('input').length > 1) {
$this.on('change', function () {
let value = $this.val()
modelValue(value, 2);
})
$this2.on('change', function () {
let value = $this2.val()
modelValue(value, 2);
})
} else {
$this.on('change', function () {
let value = $this.val()
modelValue(value, 1);
})
}
}
}
})
时间月份选择器(年-月-01) - 格式转换正则
value = value.replace(/[^0-9]/g, '')
if (value.length > 5 && value.length < 7) {
value = value + '01'
value = value.replace(/^(\d{4})\D*(\d{1,2})\D*(\d{1,2})\D*/, '$1-$2-$3')
} else {
return false
}
时间月份选择器(年-月) - 格式转换正则
value = value.replace(/[^0-9]/g, '')
if (value.length === 5) {
value = value.replace(/^(\d{4})\D*(\d{1})\D*/, '$1-0$2')
} else if (value.length > 5) {
value = value.replace(/^(\d{4})\D*(\d{1,2})\D*/, '$1-$2')
} else {
return false
}
时间月份选择器(年-01-01) - 格式转换正则
value = value.replace(/[^0-9]/g, '')
if (value.length <= 4) {
value = value + '0101'
value = value.replace(/^(\d{4})\D*(\d{1,2})\D*(\d{1,2})\D*/, '$1-$2-$3')
} else if (value.length <= 4) {
value = value.substring(0, 4) + '0101'
value = value.replace(/^(\d{4})\D*(\d{1,2})\D*(\d{1,2})\D*/, '$1-$2-$3')
} else {
return false
}
时间范围控件 - (年-月-日)
<el-date-picker v-model="DateArr" type="daterange" v-daterangeYMDFormat="DateArr" value-format="yyyy-MM-dd" range-separator="至" start-placeholder="开始" end-placeholder="结束"></el-date-picker>
Vue.directive('daterangeYMDFormat', {
inserted: function (el, binding, vnode) {
let value = binding.value;
const { context: _this, data } = vnode
const modelValue = function (value, len) {
value = value.replace(/[^0-9]/g, '')
if (value.length > 5 && value.length < 9) {
value = value.replace(/^(\d{4})\D*(\d{1,2})\D*(\d{1,2})\D*/, '$1-$2-$3')
} else if (value.length > 9 && value.length < 13) {
value = value.replace(/^(\d{4})\D*(\d{1,2})\D*(\d{1,2})\D*(\d{1,2})\D*(\d{1,2})\D*/, '$1-$2-$3 $4:$5')
} else if (value.length > 12) {
value = value.replace(/^(\d{4})\D*(\d{1,2})\D*(\d{1,2})\D*(\d{1,2})\D*(\d{1,2})\D*(\d{1,2})\D*/, '$1-$2-$3 $4:$5:$6')
} else {
return false
}
value = supplement(value)
const time = value && value.constructor == String ? value : ''
return time
}
el.addEventListener('change', () => {
value[0] = $($(el).children('input')[0]).val()
value[1] = $($(el).children('input')[1]).val()
value = value.map(date => !!date ? modelValue(date) : '')
vnode.child.$emit('input', value);
});
}
});
表格内编辑时间控件 - (年-月-日)
<el-table-column prop="time" label="时间" width="140">
<template slot-scope="scope">
<el-date-picker v-if="!!scope.row.addNew || !!scope.row.editRow" :size="elTableTreeFormSize" type="date" v-model="scope.row.time" v-tableYMDFormat value-format="yyyy-MM-dd" @keypress.enter.native="saveRow(scope.row)"></el-date-picker>
<span v-else>{{ scope.row.time }}</span>
</template>
</el-table-column>
Vue.directive('tableYMDFormat', {
inserted: function (el, binding, vnode) {
const { value: _obj } = binding
const { context: _this, data } = vnode
const modelValue = function (value, len) {
value = value.replace(/[^0-9]/g, '')
if (value.length > 5 && value.length < 9) {
value = value.replace(/^(\d{4})\D*(\d{1,2})\D*(\d{1,2})\D*/, '$1-$2-$3')
} else if (value.length > 9 && value.length < 13) {
value = value.replace(/^(\d{4})\D*(\d{1,2})\D*(\d{1,2})\D*(\d{1,2})\D*(\d{1,2})\D*/, '$1-$2-$3 $4:$5')
} else if (value.length > 12) {
value = value.replace(/^(\d{4})\D*(\d{1,2})\D*(\d{1,2})\D*(\d{1,2})\D*(\d{1,2})\D*(\d{1,2})\D*/, '$1-$2-$3 $4:$5:$6')
} else {
return false
}
value = supplement(value)
const time = value && value.constructor == String ? value : ''
vnode.child.$emit('input', time);
}
if (_this && _this._isVue) {
const $this = $($(el).children('input')[0])
$this.on('change', function () {
let value = $this.val()
modelValue(value, 1);
})
}
}
});