Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <template>
  2. <div style="opacity: 1;background-color: #FFF;padding:20px">
  3. <!-- 搜索框组件 -->
  4. <search-operation v-show="SSearch" :searchCondition="searchCondition" @RefreshLeft="RefreshLeft">
  5. <!-- 具名插槽 -->
  6. <slot name="search" :searchCondition="searchCondition" />
  7. </search-operation>
  8. <!-- CRUD组件 -->
  9. <crud-operation v-if="false" style="margin-top:10px" @add="add" @edit="edit" @remove="remove" @refresh="refresh"
  10. @search="search" :DEdit="EStart" :DDelete="DStart" :SSearch="SSearch">
  11. </crud-operation>
  12. <!-- 表单组件 -->
  13. <el-table ref="multipleTableRef" :data="testData" @selection-change="handleSelectionChange"
  14. style="width: 100%;margin: 20px 0 20px 0" :border="tableFrom.border">
  15. <!-- 表头拓展 -->
  16. <el-table-column v-for="(item,index) in tableFrom.extend" :key="index" :type="item.type" :width="item.width"
  17. :label="item.label" />
  18. <!-- 字段内容 -->
  19. <el-table-column v-for="(item,index) in tableFrom.field" :key="index" :prop="item.prop" :label="item.label"
  20. :width="item.width" :show-overflow-tooltip="!item.overflow" />
  21. <el-table-column :fixed="tableFrom.operateFixed ? 'right' : 'false'" :label="tableFrom.operateTitle"
  22. :width="tableFrom.operateWidth ? tableFrom.operateWidth : '150px'">
  23. <!-- 默认插槽值 -->
  24. <template #default="scope">
  25. <ud-operation :scope="scope" :data="testData" :isEdit="tableFrom.operate.edit"
  26. :isDelete="tableFrom.operate.delete" @handleEdit="handleEdit" @handleDelete="handleDelete">
  27. </ud-operation>
  28. </template>
  29. </el-table-column>
  30. </el-table>
  31. <!-- 分页组件 -->
  32. <el-pagination v-if="false" :page-size="20" :pager-count="11" layout="prev, pager, next" :total="1000" />
  33. <!-- 对话框 -->
  34. <el-dialog v-model="dialogFormVisible" :title="`${title}`">
  35. <!-- 是否自定义Dialog -->
  36. <div v-if="tableFrom.customDialog">
  37. <slot name="search" :searchCondition="searchCondition" />
  38. </div>
  39. <div v-else>
  40. <!-- 系统内容 -->
  41. <el-form ref="ruleFormRef" :rules="rules" :model="form">
  42. <el-form-item v-for="(item,index) in tableFrom.field" :label="item.label" :prop="item.prop"
  43. :label-width="formLabelWidth">
  44. <!-- 输入框 -->
  45. <div style="width: 100%;" v-if="item.form.type === 'input'">
  46. <el-input v-model="form[item.prop]" :type="item.itemType"
  47. :placeholder="item.form.placeholder" />
  48. </div>
  49. <!-- 选择框 -->
  50. <div style="width: 100%;" v-else-if="item.form.type === 'select'">
  51. <el-select style="width: 100%;" v-model="form[item.prop]"
  52. :placeholder="item.form.placeholder">
  53. <el-option v-for="(itemData,index) in item.form.listData" :label="itemData.label"
  54. :value="itemData.value" />
  55. </el-select>
  56. </div>
  57. <!-- 日期框 -->
  58. <div style="width: 100%;" v-else-if="item.form.type === 'date'">
  59. <el-date-picker style="width: 100%;" v-model="form[item.prop]"
  60. :value-format="item.form.valueFormat" :type="item.form.itemType"
  61. :placeholder="item.form.placeholder" />
  62. </div>
  63. </el-form-item>
  64. </el-form>
  65. </div>
  66. <template v-if="!tableFrom.customDialog" #footer>
  67. <span class="dialog-footer">
  68. <el-button @click="cancel">取消</el-button>
  69. <el-button type="primary" @click="affirm(ruleFormRef)">提交</el-button>
  70. </span>
  71. </template>
  72. </el-dialog>
  73. </div>
  74. </template>
  75. <script lang="ts" setup>
  76. import crudOperation from '@/crud/components/CRUD.operation.vue'
  77. import searchOperation from '@/crud/components/Search.operation.vue'
  78. import udOperation from '@/crud/components/UD.operation.vue'
  79. import {
  80. reactive,
  81. ref,
  82. toRaw
  83. } from 'vue'
  84. // 声明事件
  85. const emit = defineEmits(['add', 'remove', 'refresh', 'edit', 'search', 'submit', 'handleEdit', 'handleDelete'])
  86. // 声明props
  87. const props = defineProps({
  88. homeData: { //表单配置
  89. type: Object,
  90. default: function () {
  91. return {}
  92. }
  93. },
  94. tableData: { //表单数据
  95. type: Array,
  96. default: function () {
  97. return [{
  98. name: '测试数据'
  99. }, {
  100. name: '测试数据'
  101. }, {
  102. name: '测试数据'
  103. }]
  104. }
  105. }
  106. })
  107. const tableFrom = props.homeData ? props.homeData.table : [] //表单字段
  108. const multipleTableRef = ref() //表单ref(清空全选)
  109. const testData = ref(props.tableData) //表单数据
  110. const visible = ref(false) //删除提示框是否显示
  111. const dialogFormVisible = ref(false) //表单弹框是否显示
  112. const formLabelWidth = '100px' //默认表单宽度
  113. const title = ref('添加') //表单标题
  114. const DStart = ref(true) //删除按钮状态
  115. const EStart = ref(true) //编辑按钮状态
  116. const SSearch = ref(true) //是否打开搜索框
  117. let index = -1
  118. let indexs = []
  119. //表单校验
  120. const ruleFormRef = ref()
  121. let initRules = {}
  122. let initForm = {}
  123. //初始化数据
  124. if (tableFrom) {
  125. tableFrom.field.forEach(element => {
  126. initForm[element.prop] = ''
  127. if (element.form.required) {
  128. initRules[element.prop] = [{
  129. required: true,
  130. message: element.label + '不能为空',
  131. trigger: 'blur'
  132. }]
  133. }
  134. });
  135. }
  136. const rules = reactive(initRules)
  137. //搜索条件
  138. let searchCondition = reactive(props.homeData ? props.homeData.search : [])
  139. //表单字段
  140. const form = ref(initForm)
  141. //重置
  142. const RefreshLeft = () => {
  143. Object.keys(searchCondition).forEach(key => (searchCondition[key] = ''));
  144. }
  145. //添加
  146. const add = () => {
  147. console.log('点击了添加按钮');
  148. form.value = {}
  149. title.value = '添加'
  150. dialogFormVisible.value = true
  151. emit('add')
  152. }
  153. //删除
  154. const remove = () => {
  155. console.log('点击了删除按钮');
  156. emit('remove')
  157. }
  158. //刷新
  159. const refresh = () => {
  160. console.log('点击了刷新按钮');
  161. emit('refresh')
  162. }
  163. //编辑
  164. const edit = () => {
  165. console.log('点击了修改按钮');
  166. if (index != -1) {
  167. title.value = '编辑'
  168. form.value = JSON.parse(JSON.stringify(testData.value[index]))
  169. dialogFormVisible.value = true
  170. emit('edit')
  171. }
  172. }
  173. //搜索
  174. const search = () => {
  175. console.log('点击了搜索按钮');
  176. SSearch.value = !SSearch.value
  177. emit('search')
  178. }
  179. //头部多选处理
  180. const handleSelectionChange = (val) => {
  181. if (val) {
  182. index = -1
  183. DStart.value = true
  184. EStart.value = true
  185. if (val.length === 1) {
  186. testData.value.forEach((element, idx) => {
  187. if (element === val[0]) {
  188. index = idx
  189. }
  190. });
  191. EStart.value = false
  192. DStart.value = false
  193. } else if (val.length > 0) {
  194. DStart.value = false
  195. }
  196. }
  197. }
  198. //表单提交
  199. const affirm = (formEl) => {
  200. if (!formEl) {
  201. return
  202. }
  203. formEl.validate((valid, fields) => {
  204. if (valid) { //是否通过表单验证
  205. if (index !== -1) {
  206. testData.value.splice(index, 1, form.value)
  207. } else {
  208. testData.value.push(form.value)
  209. }
  210. emit('search', form.value)
  211. index = -1 //重置标识
  212. dialogFormVisible.value = false
  213. multipleTableRef.value.clearSelection() //清空多选
  214. } else {
  215. return false
  216. }
  217. })
  218. }
  219. //取消
  220. const cancel = () => {
  221. dialogFormVisible.value = false
  222. }
  223. //编辑
  224. const handleEdit = (idx, row) => {
  225. console.log('点击了编辑按钮');
  226. title.value = '编辑'
  227. form.value = JSON.parse(JSON.stringify(toRaw(row)))
  228. dialogFormVisible.value = true
  229. index = idx
  230. emit('handleEdit', idx, row)
  231. }
  232. //删除
  233. const handleDelete = (idx, row) => {
  234. console.log('点击了删除按钮');
  235. testData.value.splice(idx, 1)
  236. row.visible = false
  237. emit('handleDelete', idx, row)
  238. }
  239. </script>
  240. <style lang="scss">
  241. </style>