数据字典前端组件增加设置只读的方法

master
Guo XIn 1 year ago
parent 029daa7cfd
commit 16ee8294cc
  1. 57
      shandan-common/src/main/resources/static/js/common/layui/extend/dict.js

@ -158,18 +158,19 @@ layui.define(['form'], function (exports) {
} }
// 设置只读 // 设置只读
this.setReadonly(); if (this.readonly) {
this.setReadonly();
}
} }
/** /**
* 设置组件已读 * 设置组件已读
*/ */
setReadonly() { setReadonly() {
if (this.readonly) { // 设置延迟执行,确保在所有组件渲染完成后再设置只读,否则被layui的render方法执行后会将该只读设置重写覆盖而导致失效
// 设置延迟执行,确保在所有组件渲染完成后再设置只读,否则被layui的render方法执行后会将该只读设置重写覆盖而导致失效 setTimeout(() => {
setTimeout(() => { // 先添加用于覆盖layui样式的内部样式到head中
// 先添加用于覆盖layui样式的内部样式到head中 $('head').append(`
$('head').append(`
<style> <style>
/* 单选按钮的只读样式 */ /* 单选按钮的只读样式 */
.layui-form-radio.radio-readonly i{color: #c2c2c2} .layui-form-radio.radio-readonly i{color: #c2c2c2}
@ -184,24 +185,23 @@ layui.define(['form'], function (exports) {
.layui-form-select.select-readonly input{color: #757575} .layui-form-select.select-readonly input{color: #757575}
</style>`); </style>`);
let _elem = this.getElement(); let _elem = this.getElement();
switch (this.type) { switch (this.type) {
case "radio": case "radio":
case "checkbox": case "checkbox":
let elements = _elem.parent().find(`input[type="${this.type}"][name="${this.name}"]`); let elements = _elem.parent().find(`input[type="${this.type}"][name="${this.name}"]`);
$.each(elements, function (index) { $.each(elements, function (index) {
let lay_elem = $(this).next(); let lay_elem = $(this).next();
// 移除click事件监听 // 移除click事件监听
lay_elem.off('click').addClass('radio-readonly').css('cursor', 'auto'); lay_elem.off('click').addClass('radio-readonly').css('cursor', 'auto');
}); });
break; break;
case "select": case "select":
let lay_elem = _elem.parent().find(`select[name="${this.name}"]`).next(); let lay_elem = _elem.parent().find(`select[name="${this.name}"]`).next();
lay_elem.addClass('select-readonly'); lay_elem.addClass('select-readonly');
lay_elem.find('.layui-select-title').off('click').children().css('cursor', 'auto'); lay_elem.find('.layui-select-title').off('click').children().css('cursor', 'auto');
} }
}, 150); }, 150);
}
} }
/** /**
@ -386,6 +386,15 @@ layui.define(['form'], function (exports) {
console.error('组件未定义:' + id); console.error('组件未定义:' + id);
} }
}, },
setReadonly: function (id) {
if (id) {
_cache.get(id).setReadonly();
} else {
for (const dict of _cache.values()) {
dict.setReadonly();
}
}
},
/** /**
* 获取字典项的描述 * 获取字典项的描述
* @param type 字典类型 * @param type 字典类型