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

<!-- 列拖拽 -->
<template>
  <div>
    <!-- 刷新之后的拖拽效果失效?可以将选中的和拖拽后的结果返回给后端然后进行展示-->
    <vxe-grid ref="xGrid2" v-bind="gridOptions2" @custom="customEvent" :custom-config="{storage: true}" id="toolbar">
    </vxe-grid>
  </div>
</template>
<script>
import Sortable from "sortablejs";
import VXETable from "vxe-table";
export default {
  data() {
    return {
      gridOptions2: {
        border: true,
        showFooter: true,
        class: "sortable-column-demo",
        columnConfig: {
          useKey: true,
          minWidth: 200,
        },
        scrollX: {
          enabled: false,
        },
        footerMethod: this.footerMethod,
        toolbarConfig: {
          custom: {
            trigger: "click",
            immediate: "immediate",
          }, //自定义列配置,默认继承
        },
        columns: [
          { field: "name", title: "Name", fixed: "left", width: 300 },
          { field: "nickname", title: "Nickname" },
          { field: "role", title: "Role" },
          { field: "sex", title: "Sex" },
          { field: "age", title: "Age" },
          { field: "date3", title: "Date" },
          { field: "address", title: "Address", width: 200, showOverflow: true },
        ],
        data: [
          {
            id: 10001,
            name: "Test1",
            nickname: "T1",
            role: "Develop",
            sex: "Man",
            age: 28,
            address: "Shenzhen",
          },
          {
            id: 10002,
            name: "Test2",
            nickname: "T2",
            role: "Test",
            sex: "Women",
            age: 22,
            address: "Guangzhou",
          },
          {
            id: 10003,
            name: "Test3",
            nickname: "T3",
            role: "PM",
            sex: "Man",
            age: 32,
            address: "Shanghai",
          },
          {
            id: 10004,
            name: "Test4",
            nickname: "T4",
            role: "Designer",
            sex: "Women",
            age: 23,
            address: "Shenzhen",
          },
          {
            id: 10005,
            name: "Test5",
            nickname: "T5",
            role: "Develop",
            sex: "Women",
            age: 30,
            address: "Shanghai",
          },
        ],
        columns_data: [],
      },
    };
  },
  // 将默认展示的数据取出在created中去展示
  created() {
    this.columnDrop2();
  },
  beforeDestroy() {
    if (this.sortable2) {
      this.sortable2.destroy();
    }
  },
  methods: {
    //可以将选中的返回给后端(customDataList)
    customEvent() {
      let customDataList = [];
      (this.$refs.xGrid2.getColumns()).forEach(item => {
        if (item.type != "seq") {
          customDataList.push(item.property);
        }
      });
      console.log(customDataList);
    },
    meanNum(list, field) {
      let count = 0;
      list.forEach((item) => {
        count += Number(item[field]);
      });
      return count / list.length;
    },
    sumNum(list, field) {
      let count = 0;
      list.forEach((item) => {
        count += Number(item[field]);
      });
      return count;
    },
    footerMethod({ columns, data }) {
      return [
        columns.map((column, columnIndex) => {
          if (columnIndex === 0) {
            return "平均";
          }
          if (["age", "sex"].includes(column.property)) {
            return this.meanNum(data, column.property);
          }
          return null;
        }),
        columns.map((column, columnIndex) => {
          if (columnIndex === 0) {
            return "和值";
          }
          if (["age", "sex"].includes(column.property)) {
            return this.sumNum(data, column.property);
          }
          return null;
        }),
      ];
    },

    columnDrop2() {
      this.$nextTick(() => {
        const $table = this.$refs.xGrid2;
        console.log("$table",$table.$el);
        this.sortable2 = Sortable.create(
          $table.$el.querySelector(
            ".body--wrapper>.vxe-table--header .vxe-header--row"
          ),
          {
            handle: ".vxe-header--column",
            onEnd: ({ item, newIndex, oldIndex }) => {
              const { fullColumn, tableColumn } = $table.getTableColumn();//获取当前表格的列,全量表头列和当前渲染中的表头列
              const targetThElem = item;
              const wrapperElem = targetThElem.parentNode;
              console.log("wrapperElem", wrapperElem);
              const newColumn = fullColumn[newIndex];//newComn是目标的位置
              if (newColumn.fixed != undefined) {
                console.log("错误的移动")
                const oldThElem = wrapperElem.children[oldIndex];
                if (newIndex > oldIndex) {
                  wrapperElem.insertBefore(targetThElem, oldThElem);
                } else {
                  wrapperElem.insertBefore(
                    targetThElem,
                    oldThElem ? oldThElem.nextElementSibling : oldThElem
                  );
                }
                VXETable.modal.message({
                  content: "固定列不允许拖动,即将还原操作!",
                  status: "error",
                });
                return;
              }
              // 获取列索引 columnIndex > fullColumn
              const oldColumnIndex = $table.getColumnIndex(
                tableColumn[oldIndex]
              );
              const newColumnIndex = $table.getColumnIndex(
                tableColumn[newIndex]
              );
              // 移动到目标列
              const currRow = fullColumn.splice(oldColumnIndex, 1)[0];
              fullColumn.splice(newColumnIndex, 0, currRow);
              $table.loadColumn(fullColumn);//列重载
              //存到需要展示的地方
              this.gridOptions2.columns_data = [];
              for (let j = 0; j < fullColumn.length; j++) {
                //每次拖拽后保存的值和顺序
                if (fullColumn[j].visible) {
                  console.log(fullColumn[j].field);
                  let obj = { field: "", title: "", fixed: "", showOverflow: "" };
                  obj.field = fullColumn[j].field;
                  obj.title = fullColumn[j].title;
                  obj.fixed = fullColumn[j].fixed;
                  obj.showOverflow = fullColumn[j].showOverflow;
                  this.gridOptions2.columns_data.push(obj);
                }
              }
            },
          }
        );
      });
    },
  },
};
</script>
<style lang="sass" scoped>
.sortable-column-demo .vxe-header--row .vxe-header--column.sortable-ghost,
.sortable-column-demo .vxe-header--row .vxe-header--column.sortable-chosen
  background-color: #dfecfb

  .sortable-column-demo .vxe-header--row .vxe-header--column.col--fixed
    cursor: no-drop
</style>

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
在 `vxetable` 中,实现表格宽可以自定义拖拽的方式,可以通过配置 `column-resizing` 属性来实现。 具体来说,你需要在 `columns` 属性中为每一设置 `resizable: true`,以指示该可以进行宽调整。然后,在 `tableProps` 属性中,你可以将 `column-resizing` 属性设置为一个对象,该对象包含一个 `handleWidth` 属性(可选),以指定调整的手柄宽度,和一个 `minWidth` 属性(可选),以指定的最小宽度。 例如,下面是一个实现了自定义拖拽表格宽的 `vxetable` 示例代码: ```vue <template> <vxe-table :data="tableData" :columns="tableColumns" :table-props="tableProps" /> </template> <script> export default { data() { return { tableData: [ { name: 'John', age: 22, gender: 'Male' }, { name: 'Jane', age: 30, gender: 'Female' }, { name: 'Bob', age: 45, gender: 'Male' } ], tableColumns: [ { field: 'name', title: 'Name', resizable: true }, { field: 'age', title: 'Age', resizable: true }, { field: 'gender', title: 'Gender', resizable: true } ], tableProps: { columnResizing: { handleWidth: 5, minWidth: 50 } } } } } </script> ``` 在上面的代码中,`tableData` 和 `tableColumns` 分别是表格的数据定义,其中每一都设置了 `resizable: true`,以允许宽调整。在 `tableProps` 中,我们将 `column-resizing` 设置为一个对象,其中 `handleWidth` 属性设置为 `5`,以指定调整的手柄宽度为 `5px`,并将 `minWidth` 属性设置为 `50`,以指定的最小宽度为 `50px`。 这样,在 `vxetable` 中,你就可以通过简单的配置实现表格宽可以自定义拖拽了。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值