index.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <template>
  2. <div>
  3. <com-table :columns="columns" :data-source="data" bordered>
  4. <template v-for="col in ['name', 'age', 'address']" #[col]="{ text, record }" :key="col">
  5. <div :key="col">
  6. <a-input
  7. v-if="record.editable"
  8. style="margin: -5px 0"
  9. :value="text"
  10. @change="e => handleChange(e.target.value, record.key, col)"
  11. />
  12. <template v-else>
  13. {{ text }}
  14. </template>
  15. </div>
  16. </template>
  17. <template #operation="{ record }">
  18. <div class="editable-row-operations">
  19. <span v-if="record.editable">
  20. <a @click="save(record.key)">Save</a>
  21. <a-popconfirm title="Sure to cancel?" @confirm="cancel(record.key)">
  22. <a>Cancel</a>
  23. </a-popconfirm>
  24. </span>
  25. <span v-else>
  26. <a v-bind="editingKey !== '' ? { disabled: 'disabled' } : {}" @click="edit(record.key)">
  27. Edit
  28. </a>
  29. </span>
  30. </div>
  31. </template>
  32. </com-table>
  33. </div>
  34. </template>
  35. <script lang="ts">
  36. import { defineComponent, ref } from 'vue'
  37. import ComTable from '_c/Table'
  38. const columns = [
  39. {
  40. title: 'name',
  41. dataIndex: 'name',
  42. width: '25%',
  43. slots: { customRender: 'name' }
  44. },
  45. {
  46. title: 'age',
  47. dataIndex: 'age',
  48. width: '15%',
  49. slots: { customRender: 'age' }
  50. },
  51. {
  52. title: 'address',
  53. dataIndex: 'address',
  54. width: '40%',
  55. slots: { customRender: 'address' }
  56. },
  57. {
  58. title: 'operation',
  59. dataIndex: 'operation',
  60. slots: { customRender: 'operation' }
  61. }
  62. ]
  63. const dataList: any[] = []
  64. for (let i = 0; i < 100; i++) {
  65. dataList.push({
  66. key: i.toString(),
  67. name: `Edrward ${i}`,
  68. age: 32,
  69. address: `London Park no. ${i}`
  70. })
  71. }
  72. export default defineComponent({
  73. // name: 'EditRow',
  74. components: {
  75. ComTable
  76. },
  77. setup() {
  78. const cacheData = ref<any[]>(dataList.map(item => ({ ...item })))
  79. const editingKey = ref<string>('')
  80. const data = ref<any[]>(dataList)
  81. function handleChange(value: string, key: string, column: string) {
  82. const newData = [...data.value]
  83. const target: any[] = newData.filter((item: any) => key === item.key)[0]
  84. if (target) {
  85. target[column] = value
  86. data.value = newData
  87. }
  88. }
  89. function edit(key: string) {
  90. const newData = [...data.value]
  91. const target: any = newData.filter((item: any) => key === item.key)[0]
  92. editingKey.value = key
  93. if (target) {
  94. target.editable = true
  95. data.value = newData
  96. }
  97. }
  98. function save(key: string) {
  99. const newData = [...data.value]
  100. const newCacheData = [...cacheData.value]
  101. const target: any = newData.filter((item: any) => key === item.key)[0]
  102. const targetCache: any[] = newCacheData.filter((item: any) => key === item.key)[0]
  103. if (target && targetCache) {
  104. delete target.editable
  105. data.value = newData
  106. Object.assign(targetCache, target)
  107. cacheData.value = newCacheData
  108. }
  109. editingKey.value = ''
  110. }
  111. function cancel(key: string) {
  112. const newData = [...data.value]
  113. const target: any = newData.filter((item: any) => key === item.key)[0]
  114. editingKey.value = ''
  115. if (target) {
  116. Object.assign(target, cacheData.value.filter((item: any) => key === item.key)[0])
  117. delete target.editable
  118. data.value = newData
  119. }
  120. }
  121. return {
  122. columns,
  123. data,
  124. editingKey,
  125. handleChange, edit, save, cancel
  126. }
  127. }
  128. })
  129. </script>
  130. <style lang="less" scoped>
  131. .editable-row-operations a {
  132. margin-right: 8px;
  133. }
  134. </style>