当前位置:   article > 正文

vxetable自定义列+拖拽+数据持久化

vxetable自定义列
  1. <!-- 列拖拽 -->
  2. <template>
  3. <div>
  4. <!-- 刷新之后的拖拽效果失效?可以将选中的和拖拽后的结果返回给后端然后进行展示-->
  5. <vxe-grid ref="xGrid2" v-bind="gridOptions2" @custom="customEvent" :custom-config="{storage: true}" id="toolbar">
  6. </vxe-grid>
  7. </div>
  8. </template>
  9. <script>
  10. import Sortable from "sortablejs";
  11. import VXETable from "vxe-table";
  12. export default {
  13. data() {
  14. return {
  15. gridOptions2: {
  16. border: true,
  17. showFooter: true,
  18. class: "sortable-column-demo",
  19. columnConfig: {
  20. useKey: true,
  21. minWidth: 200,
  22. },
  23. scrollX: {
  24. enabled: false,
  25. },
  26. footerMethod: this.footerMethod,
  27. toolbarConfig: {
  28. custom: {
  29. trigger: "click",
  30. immediate: "immediate",
  31. }, //自定义列配置,默认继承
  32. },
  33. columns: [
  34. { field: "name", title: "Name", fixed: "left", width: 300 },
  35. { field: "nickname", title: "Nickname" },
  36. { field: "role", title: "Role" },
  37. { field: "sex", title: "Sex" },
  38. { field: "age", title: "Age" },
  39. { field: "date3", title: "Date" },
  40. { field: "address", title: "Address", width: 200, showOverflow: true },
  41. ],
  42. data: [
  43. {
  44. id: 10001,
  45. name: "Test1",
  46. nickname: "T1",
  47. role: "Develop",
  48. sex: "Man",
  49. age: 28,
  50. address: "Shenzhen",
  51. },
  52. {
  53. id: 10002,
  54. name: "Test2",
  55. nickname: "T2",
  56. role: "Test",
  57. sex: "Women",
  58. age: 22,
  59. address: "Guangzhou",
  60. },
  61. {
  62. id: 10003,
  63. name: "Test3",
  64. nickname: "T3",
  65. role: "PM",
  66. sex: "Man",
  67. age: 32,
  68. address: "Shanghai",
  69. },
  70. {
  71. id: 10004,
  72. name: "Test4",
  73. nickname: "T4",
  74. role: "Designer",
  75. sex: "Women",
  76. age: 23,
  77. address: "Shenzhen",
  78. },
  79. {
  80. id: 10005,
  81. name: "Test5",
  82. nickname: "T5",
  83. role: "Develop",
  84. sex: "Women",
  85. age: 30,
  86. address: "Shanghai",
  87. },
  88. ],
  89. columns_data: [],
  90. },
  91. };
  92. },
  93. // 将默认展示的数据取出在created中去展示
  94. created() {
  95. this.columnDrop2();
  96. },
  97. beforeDestroy() {
  98. if (this.sortable2) {
  99. this.sortable2.destroy();
  100. }
  101. },
  102. methods: {
  103. //可以将选中的返回给后端(customDataList)
  104. customEvent() {
  105. let customDataList = [];
  106. (this.$refs.xGrid2.getColumns()).forEach(item => {
  107. if (item.type != "seq") {
  108. customDataList.push(item.property);
  109. }
  110. });
  111. console.log(customDataList);
  112. },
  113. meanNum(list, field) {
  114. let count = 0;
  115. list.forEach((item) => {
  116. count += Number(item[field]);
  117. });
  118. return count / list.length;
  119. },
  120. sumNum(list, field) {
  121. let count = 0;
  122. list.forEach((item) => {
  123. count += Number(item[field]);
  124. });
  125. return count;
  126. },
  127. footerMethod({ columns, data }) {
  128. return [
  129. columns.map((column, columnIndex) => {
  130. if (columnIndex === 0) {
  131. return "平均";
  132. }
  133. if (["age", "sex"].includes(column.property)) {
  134. return this.meanNum(data, column.property);
  135. }
  136. return null;
  137. }),
  138. columns.map((column, columnIndex) => {
  139. if (columnIndex === 0) {
  140. return "和值";
  141. }
  142. if (["age", "sex"].includes(column.property)) {
  143. return this.sumNum(data, column.property);
  144. }
  145. return null;
  146. }),
  147. ];
  148. },
  149. columnDrop2() {
  150. this.$nextTick(() => {
  151. const $table = this.$refs.xGrid2;
  152. console.log("$table",$table.$el);
  153. this.sortable2 = Sortable.create(
  154. $table.$el.querySelector(
  155. ".body--wrapper>.vxe-table--header .vxe-header--row"
  156. ),
  157. {
  158. handle: ".vxe-header--column",
  159. onEnd: ({ item, newIndex, oldIndex }) => {
  160. const { fullColumn, tableColumn } = $table.getTableColumn();//获取当前表格的列,全量表头列和当前渲染中的表头列
  161. const targetThElem = item;
  162. const wrapperElem = targetThElem.parentNode;
  163. console.log("wrapperElem", wrapperElem);
  164. const newColumn = fullColumn[newIndex];//newComn是目标的位置
  165. if (newColumn.fixed != undefined) {
  166. console.log("错误的移动")
  167. const oldThElem = wrapperElem.children[oldIndex];
  168. if (newIndex > oldIndex) {
  169. wrapperElem.insertBefore(targetThElem, oldThElem);
  170. } else {
  171. wrapperElem.insertBefore(
  172. targetThElem,
  173. oldThElem ? oldThElem.nextElementSibling : oldThElem
  174. );
  175. }
  176. VXETable.modal.message({
  177. content: "固定列不允许拖动,即将还原操作!",
  178. status: "error",
  179. });
  180. return;
  181. }
  182. // 获取列索引 columnIndex > fullColumn
  183. const oldColumnIndex = $table.getColumnIndex(
  184. tableColumn[oldIndex]
  185. );
  186. const newColumnIndex = $table.getColumnIndex(
  187. tableColumn[newIndex]
  188. );
  189. // 移动到目标列
  190. const currRow = fullColumn.splice(oldColumnIndex, 1)[0];
  191. fullColumn.splice(newColumnIndex, 0, currRow);
  192. $table.loadColumn(fullColumn);//列重载
  193. //存到需要展示的地方
  194. this.gridOptions2.columns_data = [];
  195. for (let j = 0; j < fullColumn.length; j++) {
  196. //每次拖拽后保存的值和顺序
  197. if (fullColumn[j].visible) {
  198. console.log(fullColumn[j].field);
  199. let obj = { field: "", title: "", fixed: "", showOverflow: "" };
  200. obj.field = fullColumn[j].field;
  201. obj.title = fullColumn[j].title;
  202. obj.fixed = fullColumn[j].fixed;
  203. obj.showOverflow = fullColumn[j].showOverflow;
  204. this.gridOptions2.columns_data.push(obj);
  205. }
  206. }
  207. },
  208. }
  209. );
  210. });
  211. },
  212. },
  213. };
  214. </script>
  215. <style lang="sass" scoped>
  216. .sortable-column-demo .vxe-header--row .vxe-header--column.sortable-ghost,
  217. .sortable-column-demo .vxe-header--row .vxe-header--column.sortable-chosen
  218. background-color: #dfecfb
  219. .sortable-column-demo .vxe-header--row .vxe-header--column.col--fixed
  220. cursor: no-drop
  221. </style>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/692060
推荐阅读
相关标签
  

闽ICP备14008679号