index.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <template>
  2. <div class="basic-table">
  3. <cip-table :offset="offset"
  4. :data="modelValue"
  5. @update:data="updateData"
  6. :columns="options"
  7. :depend-on-values="dependOnValues"
  8. :border="hideBorder"
  9. :field-key="fieldKey"
  10. :tableHeaderLabel="config.tableHeaderLabel"
  11. :hide-index="config.hideIndex"
  12. :index-fixed="config.indexFixed ?? true"
  13. :show-summary="config.showSummary"
  14. :span-method="config.spanMethod"
  15. size="small"
  16. :stripe="true"
  17. :in-form="true">
  18. <el-table-column width="80px" v-if="!config?.hideDelete" fixed="right" label="操作">
  19. <template #default="{$index}">
  20. <cip-table-button @click="insertItem($index)">
  21. <i class="el-icon-circle-plus-outline cip-primary-color handler-size"/>
  22. </cip-table-button>
  23. <cip-table-button @click="deleteItem($index)">
  24. <i class="el-icon-remove-outline cip-danger-color handler-size"/>
  25. </cip-table-button>
  26. </template>
  27. </el-table-column>
  28. </cip-table>
  29. <div style="text-align: center" v-if="!config?.hideAdd">
  30. <el-button size="medium" type="text" @click="addItem">
  31. <i class="el-icon-circle-plus create-handle" />
  32. </el-button>
  33. </div>
  34. </div>
  35. </template>
  36. <script>
  37. import { computed, defineAsyncComponent, inject } from 'vue'
  38. import { formInputProps } from '@cip/components/cip-form-input/form-input-props'
  39. import { ElButton, ElMessageBox, ElTableColumn } from 'element-plus'
  40. import { useFormInput } from '@cip/components/hooks/form-input'
  41. import CipTableButton from '@cip/components/cip-table-button'
  42. import { setOptionWritable } from './util'
  43. export default {
  44. name: 'basic-table',
  45. components: {
  46. CipTable: defineAsyncComponent(() => import('@cip/components/cip-table')),
  47. CipTableButton,
  48. ElButton,
  49. ElTableColumn
  50. },
  51. props: {
  52. ...formInputProps,
  53. type: {
  54. default: 'input'
  55. }
  56. },
  57. setup (props, context) {
  58. const elFormItem = inject('elFormItem', {})
  59. const formInput = useFormInput(props, context)
  60. const { emitInput } = formInput
  61. // if (!isArray(props.modelValue)) {
  62. // emitInput([])
  63. // }
  64. const offset = computed(() => {
  65. return props.config.hideIndex ? undefined : 0
  66. })
  67. const hideBorder = computed(() => {
  68. return !props.config.hideBorder
  69. })
  70. const options = computed(() => {
  71. let options = props.config.options || []
  72. if (props.config.tableColumnStatus === 'writable') {
  73. options = setOptionWritable(options, true)
  74. }
  75. return options
  76. })
  77. const addItem = () => {
  78. const val = Array.isArray(props.modelValue) ? props.modelValue : []
  79. val.push({})
  80. emitItemValidate(val)
  81. emitInput(val)
  82. }
  83. const insertItem = (index) => {
  84. const val = props.modelValue
  85. val.splice(index, 0, {})
  86. emitItemValidate(val)
  87. emitInput(val)
  88. }
  89. const deleteItem = (index) => {
  90. ElMessageBox.confirm('确实删除此列', '提示').then(() => {
  91. const val = props.modelValue
  92. val.splice(index, 1)
  93. emitItemValidate(val)
  94. emitInput(val)
  95. }).catch(() => {})
  96. }
  97. const emitItemValidate = (val) => {
  98. // eslint-disable-next-line no-unused-expressions
  99. elFormItem.formItemMitt?.emit('el.form.change', [val])
  100. }
  101. const updateData = (val) => {
  102. formInput.emitModelValue(val)
  103. }
  104. return {
  105. offset,
  106. hideBorder,
  107. options,
  108. insertItem,
  109. deleteItem,
  110. addItem,
  111. updateData
  112. }
  113. }
  114. }
  115. </script>
  116. <style lang="less">
  117. .el-form-item.is-error{
  118. .basic-table{
  119. outline: 1px solid @danger;
  120. }
  121. }
  122. .basic-table{
  123. overflow: hidden;
  124. width: 0;
  125. flex-grow: 2;
  126. .handler-size{
  127. font-size: 16px;
  128. }
  129. .create-handle{
  130. font-size: 24px;
  131. }
  132. }
  133. </style>