|
@@ -1,10 +1,11 @@
|
|
|
-import { computed, defineComponent, ref, toRefs, unref, watch } from 'vue';
|
|
|
|
|
|
|
+import { computed, defineComponent, nextTick, onMounted, ref, toRefs, unref, watch } from 'vue';
|
|
|
import { SmartTableProps, SmartTable } from '@/components/SmartTable';
|
|
import { SmartTableProps, SmartTable } from '@/components/SmartTable';
|
|
|
import { propTypes } from '@/utils/propTypes';
|
|
import { propTypes } from '@/utils/propTypes';
|
|
|
import { type VxePulldownInstance, VxeGridInstance } from 'vxe-table';
|
|
import { type VxePulldownInstance, VxeGridInstance } from 'vxe-table';
|
|
|
import { Icon } from '@/components/Icon';
|
|
import { Icon } from '@/components/Icon';
|
|
|
|
|
|
|
|
import './style/SmartPullownTable.less';
|
|
import './style/SmartPullownTable.less';
|
|
|
|
|
+import { useRuleFormItem } from '@/hooks/component/useFormItem';
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 下拉表格
|
|
* 下拉表格
|
|
@@ -22,11 +23,18 @@ export default defineComponent({
|
|
|
valueField: propTypes.string.isRequired,
|
|
valueField: propTypes.string.isRequired,
|
|
|
// 是否每次都加载
|
|
// 是否每次都加载
|
|
|
alwaysLoad: propTypes.bool.def(false),
|
|
alwaysLoad: propTypes.bool.def(false),
|
|
|
|
|
+ // 是否立即加载
|
|
|
|
|
+ immediate: propTypes.bool.def(true),
|
|
|
|
|
+ api: {
|
|
|
|
|
+ type: Function as PropType<() => Promise<any>>,
|
|
|
|
|
+ },
|
|
|
},
|
|
},
|
|
|
- emits: ['select', 'change'],
|
|
|
|
|
|
|
+ emits: ['select', 'change', 'update:value'],
|
|
|
setup(props, { emit, slots, attrs }) {
|
|
setup(props, { emit, slots, attrs }) {
|
|
|
const { showField, dropdownWidth, value: valueRef } = toRefs(props);
|
|
const { showField, dropdownWidth, value: valueRef } = toRefs(props);
|
|
|
const pulldownRef = ref<VxePulldownInstance>();
|
|
const pulldownRef = ref<VxePulldownInstance>();
|
|
|
|
|
+ const dataLoadingRef = ref<boolean>(false);
|
|
|
|
|
+ const tableDataRef = ref<Recordable[]>([]);
|
|
|
const tableRef = ref();
|
|
const tableRef = ref();
|
|
|
// 选中的行
|
|
// 选中的行
|
|
|
const selectRowRef = ref<Recordable | null>(null);
|
|
const selectRowRef = ref<Recordable | null>(null);
|
|
@@ -45,6 +53,34 @@ export default defineComponent({
|
|
|
return selectRow[showFieldValue];
|
|
return selectRow[showFieldValue];
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 加载表格数据函数
|
|
|
|
|
+ */
|
|
|
|
|
+ const loadTableData = async () => {
|
|
|
|
|
+ if (props.api) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ dataLoadingRef.value = true;
|
|
|
|
|
+ tableDataRef.value = await props.api();
|
|
|
|
|
+ } finally {
|
|
|
|
|
+ dataLoadingRef.value = false;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ };
|
|
|
|
|
+ onMounted(async () => {
|
|
|
|
|
+ if (props.immediate) {
|
|
|
|
|
+ await loadTableData();
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ const emitData = ref<Recordable[]>([]);
|
|
|
|
|
+ const [state] = useRuleFormItem(props, 'value', 'change', emitData);
|
|
|
|
|
+ watch(
|
|
|
|
|
+ () => state.value,
|
|
|
|
|
+ (v) => {
|
|
|
|
|
+ emit('update:value', v);
|
|
|
|
|
+ },
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 下拉容器样式
|
|
* 下拉容器样式
|
|
|
*/
|
|
*/
|
|
@@ -54,22 +90,22 @@ export default defineComponent({
|
|
|
};
|
|
};
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
- watch(valueRef, (value) => {
|
|
|
|
|
|
|
+ watch([valueRef, tableDataRef], ([value, tableData]) => {
|
|
|
const tableInstance: VxeGridInstance = unref(tableRef)?.tableAction.getTableInstance();
|
|
const tableInstance: VxeGridInstance = unref(tableRef)?.tableAction.getTableInstance();
|
|
|
- let row = null;
|
|
|
|
|
- if (!tableInstance) {
|
|
|
|
|
- return false;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ let row: Recordable | null = null;
|
|
|
if (!value) {
|
|
if (!value) {
|
|
|
- tableInstance.clearCurrentRow();
|
|
|
|
|
|
|
+ tableInstance && tableInstance.clearCurrentRow();
|
|
|
|
|
+ selectRowRef.value = null;
|
|
|
|
|
+ return;
|
|
|
}
|
|
}
|
|
|
- const fullData = tableInstance.getTableData().fullData;
|
|
|
|
|
- const selectRows = fullData.filter((item) => item[props.valueField] === value);
|
|
|
|
|
|
|
+ const selectRows = tableData.filter(
|
|
|
|
|
+ (item) => item[props.valueField].toString() === value.toString(),
|
|
|
|
|
+ );
|
|
|
if (selectRows.length === 0) {
|
|
if (selectRows.length === 0) {
|
|
|
- tableInstance.clearCurrentRow();
|
|
|
|
|
|
|
+ tableInstance && tableInstance.clearCurrentRow();
|
|
|
} else {
|
|
} else {
|
|
|
row = selectRows[0];
|
|
row = selectRows[0];
|
|
|
- tableInstance.setCurrentRow(selectRows[0]);
|
|
|
|
|
|
|
+ tableInstance && tableInstance.setCurrentRow(selectRows[0]);
|
|
|
}
|
|
}
|
|
|
selectRowRef.value = row;
|
|
selectRowRef.value = row;
|
|
|
});
|
|
});
|
|
@@ -77,11 +113,16 @@ export default defineComponent({
|
|
|
/**
|
|
/**
|
|
|
* 显示弹窗
|
|
* 显示弹窗
|
|
|
*/
|
|
*/
|
|
|
- const handleShow = () => {
|
|
|
|
|
|
|
+ const handleShow = async () => {
|
|
|
unref(pulldownRef)?.showPanel();
|
|
unref(pulldownRef)?.showPanel();
|
|
|
if (props.alwaysLoad) {
|
|
if (props.alwaysLoad) {
|
|
|
- unref(tableRef)?.tableAction.query();
|
|
|
|
|
|
|
+ await loadTableData();
|
|
|
}
|
|
}
|
|
|
|
|
+ nextTick(() => {
|
|
|
|
|
+ const tableInstance: VxeGridInstance = unref(tableRef)?.tableAction.getTableInstance();
|
|
|
|
|
+ const row = unref(selectRowRef);
|
|
|
|
|
+ row && tableInstance && tableInstance.setCurrentRow(row);
|
|
|
|
|
+ });
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
const dropdownTableProps = {
|
|
const dropdownTableProps = {
|
|
@@ -92,14 +133,19 @@ export default defineComponent({
|
|
|
isCurrent: true,
|
|
isCurrent: true,
|
|
|
},
|
|
},
|
|
|
onCellClick: ({ row }) => {
|
|
onCellClick: ({ row }) => {
|
|
|
- emit('select', row);
|
|
|
|
|
- emit('change', row[props.valueField]);
|
|
|
|
|
|
|
+ changeValue(row);
|
|
|
unref(pulldownRef)?.hidePanel();
|
|
unref(pulldownRef)?.hidePanel();
|
|
|
},
|
|
},
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
|
|
+ const changeValue = (row?: Recordable) => {
|
|
|
|
|
+ emit('select', row);
|
|
|
|
|
+ state.value = row?.[props.valueField];
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
const inputIconSlots = {
|
|
const inputIconSlots = {
|
|
|
addonAfter: () => <Icon onClick={handleShow} icon="ant-design:table-outlined" />,
|
|
addonAfter: () => <Icon onClick={handleShow} icon="ant-design:table-outlined" />,
|
|
|
|
|
+ clearIcon: () => <Icon onClick={() => changeValue()} icon="ant-design:close-circle-filled" />,
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
const pulldownSlots = {
|
|
const pulldownSlots = {
|
|
@@ -110,7 +156,7 @@ export default defineComponent({
|
|
|
),
|
|
),
|
|
|
dropdown: () => (
|
|
dropdown: () => (
|
|
|
<div style={unref(computedDropdownContainerStyle)}>
|
|
<div style={unref(computedDropdownContainerStyle)}>
|
|
|
- <SmartTable ref={tableRef} {...dropdownTableProps} />
|
|
|
|
|
|
|
+ <SmartTable data={unref(tableDataRef)} ref={tableRef} {...dropdownTableProps} />
|
|
|
</div>
|
|
</div>
|
|
|
),
|
|
),
|
|
|
...slots,
|
|
...slots,
|