dataTable.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <!--
  2. * @Author: your name
  3. * @Date: 2024-10-28 16:46:38
  4. * @LastEditTime: 2024-12-26 09:13:11
  5. * @LastEditors: bogon
  6. * @Description: In User Settings Edit
  7. * @FilePath: /performance-test/src/views/performance/components/custonAsCom/dataTable.vue
  8. -->
  9. <template>
  10. <div class="global-variable" v-loading="loading">
  11. <el-tabs
  12. v-if="tabData.length > 0"
  13. v-model="activeName"
  14. @tab-click="handleClick"
  15. closable
  16. @tab-remove="removeTab"
  17. class="tabsBox"
  18. >
  19. <template v-for="(tabItem, ind) in tabData">
  20. <el-tab-pane
  21. :label="tabItem.filename"
  22. :name="String(tabItem.fileId)"
  23. :key="tabItem.fileData + ind + 'tabPane'"
  24. >
  25. <el-row
  26. type="flex"
  27. class="row-bg"
  28. justify="end"
  29. :key="tabItem.filename + ind + 'row'"
  30. >
  31. <el-button type="primary" @click="handleEditTable(tabItem.fileId)">
  32. 编辑
  33. </el-button>
  34. </el-row>
  35. <template v-if="tabItem.fileData && tabItem.fileData.length > 0">
  36. <el-table
  37. :data="tabItem.fileData"
  38. border
  39. height="600"
  40. style="width: 100%"
  41. :key="ind + tabItem.fileData"
  42. >
  43. <el-table-column
  44. :width="
  45. Object.keys(tabItem.fileData[0]).length > 6 ? 200 : null
  46. "
  47. v-for="(tableTitle, index) in Object.keys(tabItem.fileData[0])"
  48. :key="index + 'title'"
  49. :prop="tableTitle"
  50. :label="tableTitle"
  51. >
  52. </el-table-column>
  53. </el-table>
  54. </template>
  55. <template v-else>
  56. <el-empty :image-size="200" description="数据正在计算"></el-empty>
  57. </template>
  58. </el-tab-pane>
  59. </template>
  60. </el-tabs>
  61. <el-empty v-else :image-size="200"></el-empty>
  62. </div>
  63. </template>
  64. <script>
  65. import {
  66. getDataFromIndexedDB,
  67. checkObjectStoreExists,
  68. } from "@/utils/indexedDb";
  69. import { mapMutations, mapState } from "vuex";
  70. export default {
  71. data() {
  72. return {
  73. activeName: "",
  74. tabData: [],
  75. tableData: [],
  76. loading: true,
  77. };
  78. },
  79. // computed: {
  80. // ...mapState("dragChart", {
  81. // triggerGetData: "triggerGetData",
  82. // }),
  83. // },
  84. // watch: {
  85. // triggerGetData: function (newVal) {
  86. // if (newVal) {
  87. // console.log(newVal, "newVal dataTable");
  88. // this.setTriggerGetData(false);
  89. // this.getIndexDbData();
  90. // // 重置 triggerGetData 状态为 false
  91. // }
  92. // },
  93. // },
  94. mounted() {
  95. //判断indexedDb中是否存在这个数据表
  96. checkObjectStoreExists("FileDataDB", "files")
  97. .then((exists) => {
  98. if (exists) {
  99. console.log("对象存储 'files' 存在!");
  100. this.getIndexDbData();
  101. } else {
  102. this.loading = false;
  103. console.log("对象存储 'files' 不存在!");
  104. }
  105. })
  106. .catch((error) => {
  107. console.error("检查对象存储时出错:", error);
  108. });
  109. },
  110. methods: {
  111. ...mapMutations("dragChart", [
  112. "setTriggerGetData",
  113. "setUpdateTriggerGetData",
  114. ]),
  115. async getIndexDbData() {
  116. this.loading = true;
  117. const jsonData = await getDataFromIndexedDB();
  118. this.tabData = jsonData;
  119. this.activeName = jsonData && jsonData[0] && String(jsonData[0].fileId);
  120. this.loading = false;
  121. },
  122. // 删除 tab 方法
  123. removeTab(targetName) {
  124. // 1. 在 tabData 中删除对应的 tab
  125. const index = this.tabData.findIndex((tab) => tab.fileId === targetName);
  126. console.log(this.tabData, targetName, "targetName");
  127. if (index !== -1) {
  128. this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
  129. confirmButtonText: "确定",
  130. cancelButtonText: "取消",
  131. type: "warning",
  132. })
  133. .then(() => {
  134. // 删除 tabData 中的对应项
  135. const data = this.tabData;
  136. const removedTab = data.splice(index, 1)[0]; // 获取被删除的 tab 数据
  137. // 2. 更新 IndexedDB
  138. this.updateDataInIndexedDB(removedTab);
  139. // 3. 触发需要更新的状态
  140. this.setTriggerGetData(true);
  141. this.setUpdateTriggerGetData(true);
  142. this.$message({
  143. type: "success",
  144. message: "删除成功!",
  145. });
  146. })
  147. .catch(() => {
  148. this.$message({
  149. type: "info",
  150. message: "已取消删除",
  151. });
  152. });
  153. }
  154. },
  155. async updateDataInIndexedDB(removedTab) {
  156. try {
  157. const db = await this.openIndexedDB(); // 打开 IndexedDB
  158. // 创建事务,指定操作对象存储空间
  159. const transaction = db.transaction(["files"], "readwrite");
  160. const store = transaction.objectStore("files");
  161. // 使用 getAll 获取所有数据
  162. const getRequest = store.getAll();
  163. getRequest.onsuccess = () => {
  164. const data = getRequest.result;
  165. console.log(data, "所有数据");
  166. // 假设文件数据在 data[0].data 中
  167. let updatedData = data[0]?.data || [];
  168. // 从数据中移除被删除的 tab 数据
  169. updatedData = updatedData.filter(
  170. (item) => item.fileId !== removedTab.fileId
  171. );
  172. // 更新存储的数据(这里假设数据存储在 id 为 "fileDataArray" 的键下)
  173. const putRequest = store.put({
  174. id: "fileDataArray", // 使用 "fileDataArray" 作为 id
  175. data: updatedData,
  176. });
  177. putRequest.onsuccess = () => {
  178. console.log("Updated data successfully in IndexedDB");
  179. };
  180. putRequest.onerror = (event) => {
  181. console.error(
  182. "Error updating data in IndexedDB:",
  183. event.target.error
  184. );
  185. };
  186. };
  187. getRequest.onerror = (event) => {
  188. console.error(
  189. "Error retrieving data from IndexedDB:",
  190. event.target.error
  191. );
  192. };
  193. } catch (error) {
  194. console.error("Failed to update data in IndexedDB:", error);
  195. }
  196. },
  197. // 打开 IndexedDB 数据库
  198. openIndexedDB() {
  199. return new Promise((resolve, reject) => {
  200. const request = indexedDB.open("FileDataDB");
  201. request.onsuccess = (event) => {
  202. resolve(event.target.result);
  203. };
  204. request.onerror = (event) => {
  205. reject(event.target.error);
  206. };
  207. });
  208. },
  209. handleClick(tab, event) {},
  210. handleEditTable(id) {
  211. this.$router.push({
  212. path: "/home/performance/luckySheet",
  213. query: { id },
  214. });
  215. },
  216. },
  217. };
  218. </script>
  219. <style scoped lang="scss">
  220. .global-variable {
  221. .tabsBox {
  222. margin: 0 15px;
  223. ::v-deep .el-tabs__item {
  224. padding: 0 20px !important;
  225. }
  226. .row-bg {
  227. margin-bottom: 10px;
  228. }
  229. }
  230. }
  231. </style>