選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

index.vue 10KB

3年前
3年前
3年前
3年前
3年前
3年前
3年前
3年前
3年前
3年前
3年前
3年前
3年前
3年前
3年前
3年前
3年前
3年前
3年前
3年前
3年前
3年前
3年前
3年前
3年前
3年前
3年前
3年前
3年前
3年前
3年前
3年前
3年前
3年前
3年前
3年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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"
  10. @refresh="refresh" @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"
  17. :width="item.width" :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 : ''">
  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. <!-- 具名插槽(自定义操作按钮) -->
  29. <slot name="operation" :scope="scope" :tableFrom="dialogFormVisible" />
  30. </template>
  31. </el-table-column>
  32. </el-table>
  33. <!-- 分页组件 -->
  34. <el-pagination v-if="false" :page-size="20" :pager-count="11" layout="prev, pager, next" :total="1000" />
  35. <!-- 对话框 -->
  36. <el-dialog v-model="dialogFormVisible" :title="tableFrom.titleDialog ? tableFrom.titleDialog : `${title}`">
  37. <!-- 是否自定义Dialog -->
  38. <div v-if="tableFrom.customDialog">
  39. <slot name="dialog" :form="form" />
  40. </div>
  41. <div v-else>
  42. <!-- 系统内容 -->
  43. <el-form ref="ruleFormRef" :rules="rules" :model="form">
  44. <el-form-item v-for="(item, index) in tableFrom.field" :key="index" :label="item.label"
  45. :prop="item.prop" :label-width="formLabelWidth">
  46. <!-- 输入框 -->
  47. <div style="width: 100%" v-if="item.form.type === 'input'">
  48. <el-input :disabled="item.form.disabled" v-model="form[item.prop]" :type="item.itemType"
  49. :placeholder="item.form.placeholder" />
  50. </div>
  51. <!-- 选择框 -->
  52. <div style="width: 100%" v-else-if="item.form.type === 'select'">
  53. <el-select style="width: 100%" v-model="form[item.prop]" :disabled="item.form.disabled"
  54. :placeholder="item.form.placeholder">
  55. <el-option v-for="(itemData, index) in item.form.listData" :key="index"
  56. :label="itemData.label" :value="itemData.value" />
  57. </el-select>
  58. </div>
  59. <!-- 日期框 -->
  60. <div style="width: 100%" v-else-if="item.form.type === 'date'">
  61. <el-date-picker style="width: 100%" v-model="form[item.prop]" :disabled="item.form.disabled"
  62. :value-format="item.form.valueFormat" :type="item.form.itemType"
  63. :placeholder="item.form.placeholder" />
  64. </div>
  65. </el-form-item>
  66. </el-form>
  67. </div>
  68. <template #footer>
  69. <span v-if="!tableFrom.footerDialog">
  70. <el-button @click="cancel">取消</el-button>
  71. <el-button type="primary" @click="affirm(ruleFormRef)">提交</el-button>
  72. </span>
  73. <slot name="footer" />
  74. </template>
  75. </el-dialog>
  76. </div>
  77. </template>
  78. <script lang="ts" setup>
  79. import crudOperation from '@/crud/components/CRUD.operation.vue'
  80. import searchOperation from '@/crud/components/Search.operation.vue'
  81. import udOperation from '@/crud/components/UD.operation.vue'
  82. import { reactive, ref, toRaw } from 'vue'
  83. // 声明事件
  84. const emit = defineEmits([
  85. 'add',
  86. 'remove',
  87. 'refresh',
  88. 'edit',
  89. 'search',
  90. 'submit',
  91. 'handleEdit',
  92. 'handleDelete',
  93. ])
  94. // 声明props
  95. const props = defineProps({
  96. homeData: {
  97. //表单配置
  98. type: Object,
  99. default: function () {
  100. return {}
  101. },
  102. },
  103. tableData: {
  104. //表单数据
  105. type: Array,
  106. default: function () {
  107. return [
  108. {
  109. one: '范晓东',
  110. two: '贵ZEY123',
  111. three: '蓝色',
  112. four: '52011328220201499572',
  113. five: '正常',
  114. six: '储值卡',
  115. seven: '52011328220201499572',
  116. eight: '正常',
  117. data: '2020-12-24',
  118. },
  119. {
  120. one: '李丽霞',
  121. two: '贵ANS989',
  122. three: '蓝色',
  123. four: '52011328220201499572',
  124. five: '正常',
  125. six: '储值卡',
  126. seven: '52011328220200034040',
  127. eight: '正常',
  128. data: '2021-08-24',
  129. },
  130. {
  131. one: '沈波',
  132. two: '贵ABB123',
  133. three: '蓝色',
  134. four: '52011328220201499572',
  135. five: '正常',
  136. six: '储值卡',
  137. seven: '52011328220201499572',
  138. eight: '正常',
  139. data: '2022-01-22',
  140. },
  141. ]
  142. },
  143. },
  144. })
  145. const tableFrom = props.homeData ? props.homeData.table : [] //表单字段
  146. const multipleTableRef = ref() //表单ref(清空全选)
  147. const testData = ref(props.tableData) //表单数据
  148. const visible = ref(false) //删除提示框是否显示
  149. const dialogFormVisible = ref(false) //表单弹框是否显示
  150. const formLabelWidth = '100px' //默认表单宽度
  151. const title = ref('添加') //表单标题
  152. const DStart = ref(true) //删除按钮状态
  153. const EStart = ref(true) //编辑按钮状态
  154. const SSearch = ref(true) //是否打开搜索框
  155. let index = -1
  156. let indexs = []
  157. //表单校验
  158. const ruleFormRef = ref()
  159. let initRules: any = {}
  160. let initForm: any = {}
  161. //初始化数据
  162. if (tableFrom) {
  163. tableFrom.field.forEach((element: any) => {
  164. initForm[element.prop] = ''
  165. if (element.form.required) {
  166. initRules[element.prop] = [
  167. {
  168. required: true,
  169. message: element.label + '不能为空',
  170. trigger: 'blur',
  171. },
  172. ]
  173. }
  174. })
  175. }
  176. const rules = reactive(initRules)
  177. //搜索条件
  178. let searchCondition = reactive(props.homeData ? props.homeData.search : [])
  179. //表单字段
  180. const form: any = ref(initForm)
  181. form.value.four = '52011328220201499572' //赋值一条测试数据
  182. //重置
  183. const RefreshLeft = () => {
  184. Object.keys(searchCondition).forEach((key) => (searchCondition[key] = ''))
  185. }
  186. //添加
  187. const add = () => {
  188. console.log('点击了添加按钮')
  189. form.value = {}
  190. title.value = '添加'
  191. dialogFormVisible.value = true
  192. emit('add')
  193. }
  194. //删除
  195. const remove = () => {
  196. console.log('点击了删除按钮')
  197. emit('remove')
  198. }
  199. //刷新
  200. const refresh = () => {
  201. console.log('点击了刷新按钮')
  202. emit('refresh')
  203. }
  204. //编辑
  205. const edit = () => {
  206. console.log('点击了修改按钮')
  207. if (index != -1) {
  208. title.value = '编辑'
  209. form.value = JSON.parse(JSON.stringify(testData.value[index]))
  210. dialogFormVisible.value = true
  211. emit('edit')
  212. }
  213. }
  214. //搜索
  215. const search = () => {
  216. console.log('点击了搜索按钮')
  217. SSearch.value = !SSearch.value
  218. emit('search')
  219. }
  220. //头部多选处理
  221. const handleSelectionChange = (val: any) => {
  222. if (val) {
  223. index = -1
  224. DStart.value = true
  225. EStart.value = true
  226. if (val.length === 1) {
  227. testData.value.forEach((element, idx) => {
  228. if (element === val[0]) {
  229. index = idx
  230. }
  231. })
  232. EStart.value = false
  233. DStart.value = false
  234. } else if (val.length > 0) {
  235. DStart.value = false
  236. }
  237. }
  238. }
  239. //表单提交
  240. const affirm = (formEl: any) => {
  241. if (!formEl) {
  242. return
  243. }
  244. formEl.validate((valid: any, fields: any) => {
  245. if (valid) {
  246. //是否通过表单验证
  247. if (index !== -1) {
  248. testData.value.splice(index, 1, form.value)
  249. } else {
  250. testData.value.push(form.value)
  251. }
  252. emit('search', form.value)
  253. index = -1 //重置标识
  254. dialogFormVisible.value = false
  255. multipleTableRef.value.clearSelection() //清空多选
  256. } else {
  257. return false
  258. }
  259. })
  260. }
  261. //取消
  262. const cancel = () => {
  263. dialogFormVisible.value = false
  264. }
  265. //编辑
  266. const handleEdit = (idx: any, row: any) => {
  267. console.log('点击了编辑按钮')
  268. title.value = '编辑'
  269. form.value = JSON.parse(JSON.stringify(toRaw(row)))
  270. dialogFormVisible.value = true
  271. index = idx
  272. emit('handleEdit', idx, row)
  273. }
  274. //删除
  275. const handleDelete = (idx: any, row: any) => {
  276. console.log('点击了删除按钮')
  277. testData.value.splice(idx, 1)
  278. row.visible = false
  279. emit('handleDelete', idx, row)
  280. }
  281. //将需要暴露出去的数据与方法都可以暴露出去
  282. defineExpose({
  283. dialogFormVisible,
  284. title,
  285. })
  286. </script>
  287. <style lang="scss">
  288. </style>