123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- <template>
- <el-dialog
- v-model="isShowTable"
- :title="tableTitle"
- width="1200"
- :before-close="handleClose"
- >
- <avue-crud
- ref="crudRef"
- v-model:search="search"
- v-model="form"
- :data="data"
- :option="option"
- v-model:page="page"
- @row-click="rowClick"
- @row-save="createRow"
- @row-update="updateRow"
- @row-del="deleteRow"
- @search-change="searchChange"
- @search-reset="resetChange"
- @size-change="dataList"
- @current-change="dataList"
- @selection-change="selectionChange"
- />
- <template #footer>
- <div class="dialog-footer">
- <el-button @click="handleClose">取消</el-button>
- <el-button type="primary" @click="onSelected"> 确定 </el-button>
- </div>
- </template>
- </el-dialog>
- </template>
- <script setup>
- import { ref, getCurrentInstance } from "vue";
- import { useCrud } from "@/hooks/userCrud";
- import { tableConfig } from "./configs/tableConfig";
- const props = defineProps({
- tableTitle: {
- default: "",
- type: String,
- },
- tableType: {
- default: "",
- type: String,
- },
- multiple: {
- default: false,
- type: Boolean,
- },
- // 用于多选的返回值
- multipleKey: {
- default: "id",
- type: String,
- },
- });
- const isShowTable = ref(false);
- const startSelect = async (param) => {
- if (param) {
- commonConfig.value.params = param;
- }
- dataList();
- isShowTable.value = true;
- };
- /**
- * propName 要跟字典配置的key一致
- * dictData 字典数据, 注意props的value和label
- * */
- const refreshDictData = (propName, dictData, keyName = "value") => {
- nextTick(() => {
- if (dictData && dictData.length > 0 && crudRef.value) {
- crudRef.value.updateDic(propName, dictData);
- search.value[propName] = dictData[0][keyName];
- }
- });
- };
- /**
- * op 对象的字段要跟option实际字段一直 才能进行合并操作
- */
- const mergeOption = (op) => {
- for (const key of Object.keys(op)) {
- option.value[key] = op[key];
- }
- };
- // 传入一个url,后面不带/
- const {
- url,
- form,
- data,
- option,
- search,
- page,
- toDeleteIds,
- Methords,
- Utils,
- commonConfig,
- } = useCrud({
- src: tableConfig[props.tableType].url,
- multipleSelectKey: props.multipleKey,
- });
- const { dataList, createRow, updateRow, deleteRow, searchChange, resetChange } =
- Methords; //增删改查
- const { selectionChange, multipleDelete } = Methords; //选中和批量删除事件
- const crudRef = ref(null); //crudRef.value 获取avue-crud对象
- const commonTableEmits = defineEmits(["selectedSure", "selectMultipleSure"]);
- const calculateColumnWidth = (column) => {
- if (!column.label) {
- return column.minWidth;
- }
- // 字宽度
- const fontSize = 17;
- let width = fontSize * (column.label.length + 2);
- // 如果开启过滤
- if (column.filters) {
- width += 12;
- }
- // 如果开启排序
- if (column.sortable) {
- width += 24;
- }
- return width;
- };
- onMounted(() => {
- url.value = tableConfig[props.tableType].url;
- option.value = Object.assign(option.value, {
- menu: false,
- highlightCurrentRow: true,
- selection: props.multiple,
- addBtn: false,
- height: "500",
- overHidden: true,
- indexWidth: "120px",
- column: tableConfig[props.tableType].column,
- });
- // 列处理
- if (option.value.column) {
- for (let col of option.value.column) {
- col.minWidth = calculateColumnWidth(col);
- }
- }
- // dataList();
- });
- watch(
- () => props.tableType,
- () => {
- url.value = tableConfig[props.tableType].url;
- option.value = Object.assign(option.value, {
- menu: false,
- highlightCurrentRow: true,
- selection: props.multiple,
- addBtn: false,
- height: "500",
- overHidden: true,
- column: tableConfig[props.tableType].column,
- });
- // 列处理
- if (option.value.column) {
- for (let col of option.value.column) {
- col.minWidth = calculateColumnWidth(col);
- }
- }
- //dataList();
- }
- );
- const selectRowValue = ref({});
- const rowClick = (row) => {
- selectRowValue.value = row;
- };
- const handleClose = () => {
- isShowTable.value = false;
- };
- const onSelected = () => {
- if (props.multiple) {
- commonTableEmits("selectMultipleSure", toDeleteIds.value);
- } else {
- commonTableEmits("selectedSure", selectRowValue.value);
- }
- isShowTable.value = false;
- };
- defineExpose({ startSelect, refreshDictData, mergeOption });
- </script>
|