WorkTable.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <template>
  2. <el-tag
  3. type="primary"
  4. size="large"
  5. effect="plain"
  6. v-if="!tableData.length && !onlyRead"
  7. @click="onAddItem(0)"
  8. >增加一行</el-tag
  9. >
  10. <el-table
  11. class="detail-table"
  12. :header-cell-style="{
  13. background: '#F2F4F8',
  14. color: '#000000'
  15. }"
  16. :data="tableData"
  17. v-else
  18. >
  19. <el-table-column prop="workLocation" label="工作单位">
  20. <template #default="scope">
  21. <el-input v-model="scope.row.workLocation" v-if="scope.row.isEdit" />
  22. <span v-else>{{ scope.row.workLocation }}</span>
  23. </template>
  24. </el-table-column>
  25. <el-table-column prop="post" label="职位">
  26. <template #default="scope">
  27. <el-input v-model="scope.row.post" v-if="scope.row.isEdit" />
  28. <span v-else>{{ scope.row.post }}</span>
  29. </template>
  30. </el-table-column>
  31. <el-table-column prop="startTime" label="开始时间">
  32. <template #default="scope">
  33. <el-date-picker
  34. v-model="scope.row.startTime"
  35. type="date"
  36. value-format="YYYY-MM-DD"
  37. v-if="scope.row.isEdit"
  38. />
  39. <span v-else>{{ scope.row.startTime }}</span>
  40. </template>
  41. </el-table-column>
  42. <el-table-column prop="endTime" label="结束时间">
  43. <template #default="scope">
  44. <el-date-picker
  45. v-model="scope.row.endTime"
  46. type="date"
  47. value-format="YYYY-MM-DD"
  48. v-if="scope.row.isEdit"
  49. />
  50. <span v-else>{{ scope.row.endTime }}</span>
  51. </template>
  52. </el-table-column>
  53. <el-table-column prop="resignReason" label="离职原因">
  54. <template #default="scope">
  55. <el-input v-model="scope.row.resignReason" v-if="scope.row.isEdit" />
  56. <span v-else>{{ scope.row.resignReason }}</span>
  57. </template>
  58. </el-table-column>
  59. <el-table-column prop="jyxy" label="是否有竞业限制/未尽法律事宜">
  60. <template #default="scope">
  61. <el-input v-model="scope.row.jyxy" v-if="scope.row.isEdit" />
  62. <span v-else>{{ scope.row.jyxy }}</span>
  63. </template>
  64. </el-table-column>
  65. <el-table-column prop="competitionDetail" label="具体条款">
  66. <template #default="scope">
  67. <el-input v-model="scope.row.competitionDetail" v-if="scope.row.isEdit" />
  68. <span v-else>{{ scope.row.competitionDetail }}</span>
  69. </template>
  70. </el-table-column>
  71. <el-table-column fixed="right" label="操作" width="140">
  72. <template #default="scope">
  73. <el-button
  74. link
  75. type="primary"
  76. size="small"
  77. @click="onSaveItem(scope.$index)"
  78. v-if="scope.row.isEdit"
  79. >
  80. 保存
  81. </el-button>
  82. <el-button link type="primary" size="small" @click="onEditItem(scope.$index)" v-else>
  83. 编辑
  84. </el-button>
  85. <el-button link type="primary" size="small" @click="deleteRow(scope.$index)">
  86. 删除
  87. </el-button>
  88. <el-button link type="primary" size="small" @click="onAddItem(scope.$index)">
  89. 新增
  90. </el-button>
  91. </template>
  92. </el-table-column>
  93. </el-table>
  94. </template>
  95. <script lang="ts" setup>
  96. /**
  97. * @description 工作经历
  98. */
  99. interface ITable {
  100. id?: string
  101. userId?: string
  102. workLocation?: string // 工作单位
  103. post?: string // 职位
  104. startTime?: string // 开始时间
  105. endTime?: string // 结束时间
  106. resignReason?: string // 离职原因
  107. jyxy?: string // 是否有竞业限制/未尽法律事宜
  108. competitionDetail?: string // 具体条款
  109. isEdit?: boolean // 是否可编辑
  110. }
  111. const tableData = ref<ITable[]>([])
  112. const $emit = defineEmits<{
  113. (e: 'onSave', v: any): void
  114. }>()
  115. interface IProps {
  116. defaultData: any[]
  117. onlyRead?: boolean
  118. }
  119. const props = defineProps<IProps>()
  120. const { defaultData, onlyRead } = props
  121. onMounted(() => {
  122. if (defaultData && defaultData.length) {
  123. tableData.value = defaultData.map((item: any) => {
  124. return {
  125. ...item,
  126. isEdit: false
  127. }
  128. })
  129. }
  130. })
  131. // 新增行
  132. const onAddItem = (index: number) => {
  133. tableData.value.splice(index - 1, 0, {
  134. workLocation: '',
  135. post: '',
  136. startTime: '',
  137. endTime: '',
  138. resignReason: '',
  139. jyxy: '',
  140. competitionDetail: '',
  141. isEdit: true
  142. })
  143. }
  144. // 保存行
  145. const onSaveItem = (index: number) => {
  146. tableData.value[index].isEdit = false
  147. // console.log('tableData.value', tableData.value)
  148. const changeData = tableData.value.map((item: any) => {
  149. delete item.isEdit
  150. return item
  151. })
  152. $emit('onSave', changeData)
  153. }
  154. // 编辑行
  155. const onEditItem = (index: number) => {
  156. tableData.value[index].isEdit = true
  157. }
  158. // 删除
  159. const deleteRow = (index: number) => {
  160. tableData.value.splice(index, 1)
  161. }
  162. </script>
  163. <style scoped lang="scss">
  164. .detail-table {
  165. width: 100%;
  166. min-height: 100px;
  167. }
  168. </style>