index.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <template>
  2. <el-dialog
  3. v-model="isShowTable"
  4. :before-close="handleClose"
  5. :title="tableTitle"
  6. width="1200"
  7. append-to-body
  8. destroy-on-close
  9. >
  10. <avue-crud
  11. ref="crudRef"
  12. v-model="form"
  13. v-model:page="page"
  14. v-model:search="search"
  15. :data="data"
  16. :row-style="tableRowClassName"
  17. :option="option"
  18. @row-click="rowClick"
  19. @row-save="createRow"
  20. @row-update="updateRow"
  21. @row-del="deleteRow"
  22. @search-change="searchChange"
  23. @search-reset="resetChange"
  24. @size-change="dataList"
  25. @current-change="dataList"
  26. @selection-change="selectionChange"
  27. />
  28. <template #footer>
  29. <div class="dialog-footer">
  30. <el-button @click="handleClose">取消</el-button>
  31. <el-button type="primary" @click="onSelected"> 确定</el-button>
  32. </div>
  33. </template>
  34. </el-dialog>
  35. </template>
  36. <script setup>
  37. import { ref, getCurrentInstance } from "vue";
  38. import { useCrud } from "@/hooks/userCrud";
  39. import { tableConfig } from "./configs/tableConfig";
  40. const props = defineProps({
  41. tableTitle: {
  42. default: "",
  43. type: String,
  44. },
  45. tableType: {
  46. default: "",
  47. type: String,
  48. },
  49. multiple: {
  50. default: false,
  51. type: Boolean,
  52. },
  53. multipleRow: {
  54. default: false,
  55. type: Boolean,
  56. },
  57. // 用于多选的返回值
  58. multipleKey: {
  59. default: "id",
  60. type: String,
  61. },
  62. });
  63. const isShowTable = ref(false);
  64. const startSelect = async (param) => {
  65. if (param) {
  66. commonConfig.value.params = param;
  67. }
  68. dataList();
  69. isShowTable.value = true;
  70. };
  71. const tableRowClassName = ({ row, rowIndex }) => {
  72. if (row.rowStyleChange) {
  73. return { backgroundColor: '#58F195FF'};
  74. }
  75. return '';
  76. };
  77. /**
  78. * propName 要跟字典配置的key一致
  79. * dictData 字典数据, 注意props的value和label
  80. * */
  81. const refreshDictData = (propName, dictData, keyName = "value") => {
  82. nextTick(() => {
  83. if (dictData && dictData.length > 0 && crudRef.value) {
  84. crudRef.value.updateDic(propName, dictData);
  85. search.value[propName] = dictData[0][keyName];
  86. }
  87. });
  88. };
  89. /**
  90. * op 对象的字段要跟option实际字段一直 才能进行合并操作
  91. */
  92. const mergeOption = (op) => {
  93. for (const key of Object.keys(op)) {
  94. option.value[key] = op[key];
  95. }
  96. };
  97. // 传入一个url,后面不带/
  98. const {
  99. url,
  100. form,
  101. data,
  102. option,
  103. search,
  104. page,
  105. toDeleteIds,
  106. selectedRows,
  107. Methords,
  108. Utils,
  109. commonConfig,
  110. } = useCrud({
  111. src: tableConfig[props.tableType].url,
  112. multipleSelectKey: props.multipleKey,
  113. });
  114. const { dataList, createRow, updateRow, deleteRow, searchChange, resetChange } =
  115. Methords; //增删改查
  116. const { selectionChange, multipleDelete } = Methords; //选中和批量删除事件
  117. const crudRef = ref(null); //crudRef.value 获取avue-crud对象
  118. const commonTableEmits = defineEmits(["selectedSure", "selectMultipleSure"]);
  119. const calculateColumnWidth = (column) => {
  120. if (!column.label) {
  121. return column.minWidth;
  122. }
  123. // 字宽度
  124. const fontSize = 17;
  125. let width = fontSize * (column.label.length + 2);
  126. // 如果开启过滤
  127. if (column.filters) {
  128. width += 12;
  129. }
  130. // 如果开启排序
  131. if (column.sortable) {
  132. width += 24;
  133. }
  134. return width;
  135. };
  136. onMounted(() => {
  137. url.value = tableConfig[props.tableType].url;
  138. option.value = Object.assign(option.value, {
  139. menu: false,
  140. highlightCurrentRow: true,
  141. selection: props.multiple,
  142. addBtn: false,
  143. height: "500",
  144. overHidden: true,
  145. indexWidth: "120px",
  146. column: tableConfig[props.tableType].column,
  147. });
  148. // 列处理
  149. if (option.value.column) {
  150. for (let col of option.value.column) {
  151. col.minWidth = calculateColumnWidth(col);
  152. }
  153. }
  154. // dataList();
  155. });
  156. watch(
  157. () => props.tableType,
  158. () => {
  159. url.value = tableConfig[props.tableType].url;
  160. option.value = Object.assign(option.value, {
  161. menu: false,
  162. highlightCurrentRow: true,
  163. selection: props.multiple,
  164. addBtn: false,
  165. height: "500",
  166. overHidden: true,
  167. column: tableConfig[props.tableType].column,
  168. });
  169. // 列处理
  170. if (option.value.column) {
  171. for (let col of option.value.column) {
  172. col.minWidth = calculateColumnWidth(col);
  173. }
  174. }
  175. //dataList();
  176. }
  177. );
  178. const selectRowValue = ref({});
  179. const rowClick = (row) => {
  180. selectRowValue.value = row;
  181. };
  182. const handleClose = () => {
  183. isShowTable.value = false;
  184. };
  185. const onSelected = () => {
  186. if (props.multiple) {
  187. commonTableEmits("selectMultipleSure", toDeleteIds.value);
  188. } else {
  189. if (props.multipleRow) {
  190. commonTableEmits("selectedSure", selectedRows.value);
  191. } else {
  192. commonTableEmits("selectedSure", selectRowValue.value);
  193. }
  194. }
  195. isShowTable.value = false;
  196. };
  197. defineExpose({ startSelect, refreshDictData, mergeOption });
  198. </script>