Browse Source

1、下拉表格增加设置高度与宽度的属性。

2、下拉表格改变事件返回的内容增加行数据记录。
3、下拉列表增加可选行的函数配置(需要修改表格,目前的模式不友好)
4、下拉列表增加设置显示值的API
main
likunming 4 months ago
parent
commit
d93954d606
  1. 186
      io.sc.platform.core.frontend/src/platform/components/select/WGridSelect.vue

186
io.sc.platform.core.frontend/src/platform/components/select/WGridSelect.vue

@ -1,8 +1,8 @@
<template> <template>
<div v-show="fieldMethodsClass.getShow(props, modelValue)"> <div v-show="fieldMethodsClass.getShow(props, modelValue)">
<q-input <q-field
ref="textSelectRef" ref="textSelectRef"
v-model="displayValueComputed" v-model="modelValue"
:hide-bottom-space="true" :hide-bottom-space="true"
:hide-hint="true" :hide-hint="true"
:outlined="true" :outlined="true"
@ -10,28 +10,26 @@
:autogrow="true" :autogrow="true"
v-bind="attrs" v-bind="attrs"
:bottom-slots="counter" :bottom-slots="counter"
:stack-label="!Tools.isEmpty(displayValueComputed)"
type="text" type="text"
:rules="fieldMethodsClass.getRules(props, { value: modelValue, displayValue: displayValueComputed }, textSelectRef, undefined)" :rules="fieldMethodsClass.getRules(props, { value: modelValue, displayValue: displayValueComputed }, textSelectRef, undefined)"
:readonly="fieldMethodsClass.getReadOnly(props, { value: modelValue, displayValue: displayValueComputed })" :readonly="fieldMethodsClass.getReadOnly(props, { value: modelValue, displayValue: displayValueComputed })"
:disable="fieldMethodsClass.getDisable(props, { value: modelValue, displayValue: displayValueComputed })" :disable="fieldMethodsClass.getDisable(props, { value: modelValue, displayValue: displayValueComputed })"
:clearable="false" :clearable="false"
@focus="
() => {
textSelectRef?.blur();
}
"
> >
<template #control>
<div>{{ displayValueComputed }}</div>
</template>
<!-- label --> <!-- label -->
<template #label><w-label :required="fieldMethodsClass.getRequired(props, modelValue)" :label="attrs.label"></w-label></template> <template #label><w-label :required="fieldMethodsClass.getRequired(props, modelValue)" :label="attrs.label"></w-label></template>
<template v-if="!props['form'] || (props['form'] && props['form'].getStatus() !== 'view')" #append> <template v-if="!props['form'] || (props['form'] && props['form'].getStatus() !== 'view')" #append>
<q-btn v-if="!Tools.isEmpty(displayValueComputed)" flat square unelevated dense icon="cancel" @click="fieldMethodsClass.clearValue"></q-btn> <q-btn v-if="!Tools.isEmpty(displayValueComputed)" flat square unelevated dense icon="cancel" @click="fieldMethodsClass.clearValue"></q-btn>
<q-btn flat square unelevated dense icon="manage_search"> <q-btn flat square unelevated dense icon="manage_search">
<q-popup-proxy v-model:model-value="isShow" anchor="bottom right" self="top right" :offset="[0, 10]"> <q-popup-proxy v-model:model-value="isShow" anchor="bottom right" self="top right" :offset="[0, 10]">
<div style="width: 700px; height: 300px"> <div :style="popupProxyStyleComputed">
<w-grid <w-grid
ref="gridRef" ref="gridRef"
v-bind="{ ...props.grid, onRowClick: undefined }" v-bind="{ ...props.grid, onRowClick: undefined, height: undefined, width: undefined }"
db-click-operation="none" db-click-operation="none"
:checkbox-selection="props.multiple || false" :checkbox-selection="props.multiple || false"
:dense="true" :dense="true"
@ -52,33 +50,42 @@
<template v-if="counter" #counter> <template v-if="counter" #counter>
<div>{{ modelValue?.length }}</div> <div>{{ modelValue?.length }}</div>
</template> </template>
</q-input> </q-field>
</div> </div>
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { ref, computed, useAttrs, toRaw, watch, onMounted } from 'vue'; import { ref, computed, useAttrs, toRaw, watch, onMounted, nextTick } from 'vue';
import { Tools, axios } from '@/platform'; import { Tools, axios } from '@/platform';
import { FormFieldProps } from '@/platform/components/form/FormField.ts'; import { FormFieldProps } from '@/platform/components/form/FormField.ts';
import { FormFieldMethods } from '../form/FormField'; import { FormFieldMethods } from '../form/FormField';
const textSelectRef = ref(); const textSelectRef = ref();
const gridRef = ref();
const attrs = useAttrs(); const attrs = useAttrs();
const modelValue = defineModel<string | Array<string>>(); const modelValue = defineModel<string | Array<string>>(); //
const modelDisplayValue = ref(''); //
const customDisplayValue = ref(''); // setDisplayValue
const modelObjectValue = ref(<any>[]); // const modelObjectValue = ref(<any>[]); //
const isShow = ref(false); //
const needFetchData = ref(true); //
// { value: xxx, displayvalue: xxx } /**
// { value: xxx, displayvalue: xxx } * 当用户调用setDisplayValue后以customDisplayValue变量为显示值否则使用customDisplayValue
*/
const gridRef = ref(); const displayValueComputed = computed(() => {
if (!Tools.isEmpty(customDisplayValue.value)) {
const isShow = ref(false); return customDisplayValue.value;
}
return modelDisplayValue.value;
});
interface FieldProps extends FormFieldProps { interface FieldProps extends FormFieldProps {
multiple?: boolean; multiple?: boolean;
counter?: boolean; counter?: boolean;
displayValue: string | ((args: object) => ''); displayValue: string | ((args: object) => '');
grid: object; // grid: object; //
selectableIf?: (args: object) => '';
} }
const props = withDefaults(defineProps<FieldProps>(), { const props = withDefaults(defineProps<FieldProps>(), {
showIf: true, showIf: true,
@ -93,6 +100,7 @@ const props = withDefaults(defineProps<FieldProps>(), {
* } * }
*/ */
displayValue: '', displayValue: '',
selectableIf: undefined,
}); });
class FieldMethods extends FormFieldMethods { class FieldMethods extends FormFieldMethods {
// //
@ -101,6 +109,7 @@ class FieldMethods extends FormFieldMethods {
props['onUpdateValue']({ props['onUpdateValue']({
value: value_, value: value_,
displayValue: displayValueComputed.value, displayValue: displayValueComputed.value,
row: getRow(value_),
form: props['form'], form: props['form'],
}); });
} }
@ -111,14 +120,12 @@ class FieldMethods extends FormFieldMethods {
}; };
// //
setValue = (value) => { setValue = async (value) => {
if (props.multiple && Array.isArray(value) && Array.isArray(modelValue.value)) { if (props.multiple && Array.isArray(value) && Array.isArray(modelValue.value)) {
fieldMethodsClass.clearValue(); fieldMethodsClass.clearValue();
modelValue.value.push(...value); modelValue.value = [...value];
setObjectValueByValue(value);
} else if (!props.multiple && !Array.isArray(value)) { } else if (!props.multiple && !Array.isArray(value)) {
modelValue.value = value; modelValue.value = value;
setObjectValueByValue(value);
} else { } else {
console.info('error========模型值不匹配'); console.info('error========模型值不匹配');
} }
@ -127,6 +134,10 @@ class FieldMethods extends FormFieldMethods {
getValue = () => { getValue = () => {
return modelValue.value; return modelValue.value;
}; };
//
setDisplayValue = (value) => {
customDisplayValue.value = value;
};
// //
getObjectValue = () => { getObjectValue = () => {
return modelObjectValue.value; return modelObjectValue.value;
@ -134,21 +145,43 @@ class FieldMethods extends FormFieldMethods {
// //
clearValue = () => { clearValue = () => {
if (props.multiple && Array.isArray(modelValue.value)) { if (props.multiple && Array.isArray(modelValue.value)) {
modelValue.value.splice(0, modelValue.value.length); modelValue.value = [];
} else { } else {
modelValue.value = undefined; modelValue.value = undefined;
} }
fieldMethodsClass.clearObjectValue(); fieldMethodsClass.clearObjectValue();
_setDisplayValue();
customDisplayValue.value = '';
}; };
// //
clearObjectValue = () => { clearObjectValue = () => {
modelObjectValue.value.splice(0, modelObjectValue.value.length); modelObjectValue.value = [];
}; };
} }
const fieldMethodsClass = new FieldMethods(); const fieldMethodsClass = new FieldMethods();
const valueUseColumnName = props.grid['primaryKey'] || 'id'; const valueUseColumnName = props.grid['primaryKey'] || 'id';
const displayValueComputed = computed(() => { const popupProxyStyleComputed = computed(() => {
let height = '300px';
let width = '700px';
if (!Tools.isEmpty(props.grid['height'])) {
if (typeof props.grid['height'] === 'number') {
height = props.grid['height'] + 'px';
} else {
height = props.grid['height'];
}
}
if (!Tools.isEmpty(props.grid['width'])) {
if (typeof props.grid['width'] === 'number') {
width = props.grid['width'] + 'px';
} else {
width = props.grid['width'];
}
}
return { height, width };
});
const _setDisplayValue = () => {
let result = ''; let result = '';
if (modelObjectValue.value.length > 0) { if (modelObjectValue.value.length > 0) {
modelObjectValue.value.forEach((item) => { modelObjectValue.value.forEach((item) => {
@ -156,11 +189,27 @@ const displayValueComputed = computed(() => {
}); });
result = result.substring(1, result.length); result = result.substring(1, result.length);
} }
return result; modelDisplayValue.value = result;
}); };
//
const getRow = (value_) => {
if (Array.isArray(value_)) {
const row = {};
value_.forEach((v) => {
const findRow = modelObjectValue.value.find((item) => item.value === v);
row[v] = findRow?.row;
});
return row;
} else {
const findRow = modelObjectValue.value.find((item) => item.value === value_);
const row = findRow?.row;
return row;
}
};
// //
const getActualDisplayValue = (row) => { const formatDisplayValue = (row) => {
if (!Tools.isEmpty(props.displayValue) && typeof props.displayValue === 'function') { if (!Tools.isEmpty(props.displayValue) && typeof props.displayValue === 'function') {
return props.displayValue({ return props.displayValue({
data: toRaw(row), data: toRaw(row),
@ -174,17 +223,34 @@ const getActualDisplayValue = (row) => {
// checkbox // checkbox
const updateTickeds = (args) => { const updateTickeds = (args) => {
if (props.selectableIf && typeof props.selectableIf === 'function') {
if (!props.selectableIf(args)) {
return;
}
}
needFetchData.value = false;
customDisplayValue.value = '';
fieldMethodsClass.clearValue(); fieldMethodsClass.clearValue();
if (args.value && Array.isArray(modelValue.value)) { if (args.value && Array.isArray(modelValue.value)) {
const rows = gridRef.value.getRows(); const rows = gridRef.value.getRows();
const tickeds = <any>[];
rows.forEach((item) => { rows.forEach((item) => {
modelValue.value.push(item[valueUseColumnName]); tickeds.push(item[valueUseColumnName]);
modelObjectValue.value.push({ value: item[valueUseColumnName], displayValue: getActualDisplayValue(item) }); modelObjectValue.value.push({ value: item[valueUseColumnName], displayValue: formatDisplayValue(item), row: toRaw(item) });
}); });
modelValue.value = tickeds;
} }
_setDisplayValue();
}; };
// checkbox // checkbox
const updateTicked = (args) => { const updateTicked = (args) => {
if (props.selectableIf && typeof props.selectableIf === 'function') {
if (!props.selectableIf(args)) {
return;
}
}
needFetchData.value = false;
customDisplayValue.value = '';
if (Array.isArray(modelValue.value)) { if (Array.isArray(modelValue.value)) {
if (props.grid['tree']) { if (props.grid['tree']) {
fieldMethodsClass.clearValue(); fieldMethodsClass.clearValue();
@ -192,8 +258,8 @@ const updateTicked = (args) => {
const rows = gridRef.value.getTickedRows(); const rows = gridRef.value.getTickedRows();
if (rows?.length > 0) { if (rows?.length > 0) {
rows.forEach((item) => { rows.forEach((item) => {
modelValue.value.push(item[valueUseColumnName]); modelValue.value = [...modelValue.value, item[valueUseColumnName]];
modelObjectValue.value.push({ value: item[valueUseColumnName], displayValue: getActualDisplayValue(item) }); modelObjectValue.value.push({ value: item[valueUseColumnName], displayValue: formatDisplayValue(item), row: toRaw(item) });
}); });
} }
} else { } else {
@ -202,26 +268,38 @@ const updateTicked = (args) => {
}); });
if (args.row['ticked']) { if (args.row['ticked']) {
if (index < 0) { if (index < 0) {
modelValue.value.push(args.row[valueUseColumnName]); modelValue.value = [...modelValue.value, args.row[valueUseColumnName]];
modelObjectValue.value.push({ value: args.row[valueUseColumnName], displayValue: getActualDisplayValue(args.row) }); modelObjectValue.value.push({ value: args.row[valueUseColumnName], displayValue: formatDisplayValue(args.row), row: toRaw(args.row) });
} }
} else { } else {
modelValue.value.splice(index, 1); removeItem(index);
modelObjectValue.value.splice(index, 1); modelObjectValue.value.splice(index, 1);
} }
} }
_setDisplayValue();
} }
}; };
const removeItem = (index) => {
const result = modelValue.value.filter((item, index_) => index !== index_);
modelValue.value = result;
};
const rowClick = (args) => { const rowClick = (args) => {
if (props.selectableIf && typeof props.selectableIf === 'function') {
if (!props.selectableIf(args)) {
return;
}
}
needFetchData.value = false;
customDisplayValue.value = '';
const modelValue_ = args.row[valueUseColumnName]; const modelValue_ = args.row[valueUseColumnName];
if (props.multiple && Array.isArray(modelValue.value)) { if (props.multiple && Array.isArray(modelValue.value)) {
fieldMethodsClass.clearValue(); fieldMethodsClass.clearValue();
modelValue.value.push(modelValue_); modelValue.value = [modelValue_];
modelObjectValue.value.push({ value: modelValue_, displayValue: getActualDisplayValue(args.row) }); modelObjectValue.value.push({ value: modelValue_, displayValue: formatDisplayValue(args.row), row: toRaw(args.row) });
} else if (!props.multiple) { } else if (!props.multiple) {
fieldMethodsClass.clearValue(); fieldMethodsClass.clearValue();
modelValue.value = modelValue_; modelValue.value = modelValue_;
modelObjectValue.value.push({ value: modelValue_, displayValue: getActualDisplayValue(args.row) }); modelObjectValue.value.push({ value: modelValue_, displayValue: formatDisplayValue(args.row), row: toRaw(args.row) });
} else { } else {
console.info('error========模型值不匹配'); console.info('error========模型值不匹配');
} }
@ -229,31 +307,23 @@ const rowClick = (args) => {
if (props.grid['onRowClick'] && typeof props.grid['onRowClick'] === 'function') { if (props.grid['onRowClick'] && typeof props.grid['onRowClick'] === 'function') {
props.grid['onRowClick'](args); props.grid['onRowClick'](args);
} }
_setDisplayValue();
}; };
watch( watch(
() => modelValue.value, () => modelValue.value,
(newVal, oldVal) => { (newVal, oldVal) => {
if (newVal !== oldVal) { fieldMethodsClass.updateValue(newVal);
fieldMethodsClass.updateValue(newVal);
}
if (Tools.isEmpty(newVal)) { if (Tools.isEmpty(newVal)) {
fieldMethodsClass.clearObjectValue(); fieldMethodsClass.clearObjectValue();
} else if (newVal !== oldVal) { } else if (newVal !== oldVal && needFetchData.value) {
if (modelObjectValue.value.length > 0) { fetchData(newVal);
const tempValue = modelObjectValue.value.find((item) => item.value === newVal);
if (!tempValue) {
setObjectValueByValue(newVal);
}
} else {
setObjectValueByValue(newVal);
}
} }
}, },
); );
// //
const setObjectValueByValue = async (value) => { const fetchData = async (value) => {
if ((Array.isArray(value) && value.length > 0) || (typeof value === 'string' && !Tools.isEmpty(value))) { if ((Array.isArray(value) && value.length > 0) || (typeof value === 'string' && !Tools.isEmpty(value))) {
fieldMethodsClass.clearObjectValue(); fieldMethodsClass.clearObjectValue();
const urlSearchParams = new URLSearchParams(); const urlSearchParams = new URLSearchParams();
@ -272,25 +342,27 @@ const setObjectValueByValue = async (value) => {
const responseData = resp.data; const responseData = resp.data;
if (Array.isArray(responseData) && responseData.length > 0) { if (Array.isArray(responseData) && responseData.length > 0) {
responseData.forEach((item) => { responseData.forEach((item) => {
modelObjectValue.value.push({ value: item[valueUseColumnName], displayValue: getActualDisplayValue(item) }); modelObjectValue.value.push({ value: item[valueUseColumnName], displayValue: formatDisplayValue(item), row: item });
}); });
} else if (typeof responseData === 'object' && responseData.content?.length > 0) { } else if (typeof responseData === 'object' && responseData.content?.length > 0) {
responseData.content.forEach((item) => { responseData.content.forEach((item) => {
modelObjectValue.value.push({ value: item[valueUseColumnName], displayValue: getActualDisplayValue(item) }); modelObjectValue.value.push({ value: item[valueUseColumnName], displayValue: formatDisplayValue(item), row: item });
}); });
} }
} }
_setDisplayValue();
} }
}; };
onMounted(() => { onMounted(() => {
setObjectValueByValue(modelValue.value); fetchData(modelValue.value);
}); });
defineExpose({ defineExpose({
validate: fieldMethodsClass.validate, validate: fieldMethodsClass.validate,
setValue: fieldMethodsClass.setValue, setValue: fieldMethodsClass.setValue,
getValue: fieldMethodsClass.getValue, getValue: fieldMethodsClass.getValue,
setDisplayValue: fieldMethodsClass.setDisplayValue,
getObjectValue: fieldMethodsClass.getObjectValue, getObjectValue: fieldMethodsClass.getObjectValue,
clearValue: fieldMethodsClass.clearValue, clearValue: fieldMethodsClass.clearValue,
}); });

Loading…
Cancel
Save