materialsDistributionAdd.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <template>
  2. <div class="app-container p-2">
  3. <el-row :gutter="20">
  4. <el-col :span="6">
  5. <h2>新增</h2>
  6. </el-col>
  7. <el-col :lg="24" :xs="24">
  8. <el-table :data="tableData" border height="400">
  9. <el-table-column label="序号" prop="seqNo" width="80">
  10. <template #default="{ $index }">
  11. <span>{{ $index + 1 }}</span>
  12. </template>
  13. </el-table-column>
  14. <el-table-column label="物资类型" prop="materialType">
  15. <template #default="{ row, $index }">
  16. <span
  17. class="editable-span"
  18. contenteditable="true"
  19. @blur="saveEdit($index, 'materialType', $event.target.innerText)"
  20. v-text="row.materialType"
  21. ></span>
  22. </template>
  23. </el-table-column>
  24. <el-table-column label="物资名称" prop="materialName">
  25. <template #default="{ row, $index }">
  26. <span
  27. class="editable-span"
  28. contenteditable="true"
  29. @blur="saveEdit($index, 'materialName', $event.target.innerText)"
  30. v-text="row.materialName"
  31. ></span>
  32. </template>
  33. </el-table-column>
  34. <el-table-column label="物资数量(件)" prop="quantity">
  35. <template #default="{ row, $index }">
  36. <span
  37. class="editable-span"
  38. contenteditable="true"
  39. @blur="saveEdit($index, 'quantity', $event.target.innerText)"
  40. v-text="row.quantity"
  41. ></span>
  42. </template>
  43. </el-table-column>
  44. <el-table-column label="物资用途" prop="purpose">
  45. <template #default="{ row, $index }">
  46. <span
  47. class="editable-span"
  48. contenteditable="true"
  49. @blur="saveEdit($index, 'purpose', $event.target.innerText)"
  50. v-text="row.purpose"
  51. ></span>
  52. </template>
  53. </el-table-column>
  54. </el-table>
  55. </el-col>
  56. <div class="common-dialog-footer" style="width: 100%; justify-content: center; display: flex">
  57. <el-row :span="24" :gutter="10" class="mb8">
  58. <el-col :span="1.5">
  59. <el-button type="primary" @click="handleAddRow">新增一项</el-button>
  60. </el-col>
  61. <el-col :span="1.5">
  62. <el-button type="primary" @click="handleSave">提交</el-button>
  63. </el-col>
  64. <el-col :span="1.5">
  65. <el-button type="danger" @click="handleReturn">取消</el-button>
  66. </el-col>
  67. </el-row>
  68. </div>
  69. </el-row>
  70. </div>
  71. </template>
  72. <script setup lang="ts">
  73. const props = defineProps({
  74. eventId: String
  75. });
  76. const emits = defineEmits(['close']);
  77. const tableData = ref<any[]>([]);
  78. const loadFromLocalStorage = () => {
  79. const storedData = localStorage.getItem('tableData');
  80. if (storedData) {
  81. tableData.value = JSON.parse(storedData);
  82. } else {
  83. tableData.value = [
  84. { materialType: '三防物资', materialName: '雨衣', quantity: '100', purpose: '高州泰利台风前置' },
  85. { materialType: '三防物资', materialName: '水鞋', quantity: '10', purpose: '高州泰利台风前置' },
  86. { materialType: '三防物资', materialName: '雨伞', quantity: '50', purpose: '高州泰利台风前置' }
  87. ];
  88. }
  89. };
  90. const addDefaultRow = () => {
  91. const newRow = {
  92. materialType: '',
  93. materialName: '',
  94. quantity: '',
  95. purpose: ''
  96. };
  97. tableData.value.push(newRow);
  98. };
  99. const saveEdit = (index: number, key: string, value: string) => {
  100. tableData.value[index][key] = value;
  101. localStorage.setItem('tableData', JSON.stringify(tableData.value));
  102. };
  103. const handleAddRow = () => addDefaultRow();
  104. const handleReturn = () => {
  105. emits('close');
  106. };
  107. const handleSave = () => {
  108. // 这里可以添加保存到服务器的逻辑
  109. console.log('数据已保存:', tableData.value);
  110. };
  111. onMounted(() => {
  112. loadFromLocalStorage();
  113. });
  114. </script>
  115. <style scoped>
  116. .app-container {
  117. font-family: Avenir, Helvetica, Arial, sans-serif;
  118. -webkit-font-smoothing: antialiased;
  119. -moz-osx-font-smoothing: grayscale;
  120. text-align: center;
  121. color: #2c3e50;
  122. }
  123. .report-period {
  124. margin-top: 10px;
  125. font-size: 14px;
  126. color: #606266;
  127. }
  128. .editable-span {
  129. cursor: pointer;
  130. display: inline-block;
  131. white-space: nowrap;
  132. overflow: hidden;
  133. text-overflow: ellipsis;
  134. }
  135. .editable-span[contenteditable='true'] {
  136. white-space: normal;
  137. outline: none; /* 移除编辑时的焦点边框 */
  138. }
  139. .editable-span[contenteditable='true']:empty::before {
  140. content: attr(data-placeholder); /* 可选:为空时显示占位符 */
  141. color: #999;
  142. }
  143. .common-dialog-footer {
  144. margin-top: 20px;
  145. }
  146. </style>