VXE Grid 行编辑模式

效果图

<template>
  <div>
    <vxe-modal
      v-model="dlgShow"
      :position="{ top: 30 }"
      width="1200"
      min-width="550"
      min-height="260"
      height="500"
      resize
      size="small"
      show-zoom
      destroy-on-close
      @show="show"
    >
      <template #title>
        <div class="vxe-modal-header-set">
          <div class="header-btn-list">
            <h3 class="header-title">发票明细</h3>
            <div class="btn-wrap-set">
              <vxe-button
                class="btn"
                status="primary"
                content="添加"
                @click="add"
              ></vxe-button>
              <vxe-button
                class="btn"
                status="info"
                @click="close"
                content="取消"
              ></vxe-button>
            </div>
          </div>
        </div>
      </template>
      <vxe-grid
        border
        resizable
        ref="table"
        size="small"
        :loading="loading"
        :columns="tableColumn"
        :data="tableData"
        height="auto"
        :edit-rules="validRules"
        :edit-config="{ trigger: 'manual', mode: 'row' }"
        @edit-closed="editClose"
      >
        <template #fwmc_edit="{ row }">
          <vxe-input v-model="row.fwmc"></vxe-input>
        </template>
        <template #ggxh_edit="{ row }">
          <vxe-input v-model="row.ggxh"></vxe-input>
        </template>
        <template #fpdw_edit="{ row }">
          <vxe-input v-model="row.fpdw"></vxe-input>
        </template>
        <template #fpdj_edit="{ row }">
          <vxe-input type="number" v-model="row.fpdj"></vxe-input>
        </template>
        <template #fpshl_edit="{ row }">
          <vxe-input type="integer" v-model="row.fpshl"></vxe-input>
        </template>
        <template #fpsl_edit="{ row }">
          <vxe-select v-model="row.fpsl" placeholder="请选择" size="small">
            <vxe-option
              v-for="item in shuilOptions"
              :key="item.dictValue"
              :value="item.dictValue"
              :label="item.dictLabel"
            ></vxe-option>
          </vxe-select>
        </template>
        <template #fpsl_default="{ row }">
          <span>{{ row.fpsl }}%</span>
        </template>

        <template #fpse_edit="{ row }">
          <span>{{ row.fpse }}</span>
        </template>
        <template #sjhj_edit="{ row }">
          <span>{{ row.sjhj }}</span>
        </template>
        <template #fpje_edit="{ row }">
          <span>{{ row.fpje }}</span>
        </template>

        <template #operate="{ row }">
          <template v-if="$refs.table.isActiveByRow(row)">
            <vxe-button
              size="mini"
              icon="fa fa-save"
              status="primary"
              title="保存"
              circle
              @click="saveRowEvent(row)"
            ></vxe-button>
            <vxe-button
              size="mini"
              icon="fa vxe-icon--close"
              circle
              title="取消"
              @click="canCelRowEvent(row)"
            ></vxe-button>
          </template>
          <template v-else>
            <vxe-button
              size="mini"
              circle
              icon="fa fa-edit"
              title="编辑"
              @click="editRowEvent(row)"
            ></vxe-button>
            <vxe-button
              size="mini"
              icon="fa fa-trash"
              circle
              title="删除"
              @click="delRowEvent(row)"
            ></vxe-button>
          </template>
        </template>
      </vxe-grid>
    </vxe-modal>
  </div>
</template>
<script>
import {
  listCwfpglmx,
  addCwfpglmx,
  updateCwfpglmx,
  delCwfpglmx,
} from "@/api/erp/cw/cwfpglmx";
import { guid } from "@/utils/tool";

export default {
  name: "wzjxfpmxwin",

  data() {
    return {
      dlgShow: false,
      loading: false,
      editRow: null,
      //发票id
      fpid: "",
      //发票类型
      fplx: "",
      validRules: {
        fwmc: [{ required: true, message: "必须填写" }],
        ggxh: [{ required: true, message: "必须填写" }],
        fpdw: [{ required: true, message: "必须填写" }],
        fpdj: [
          { required: true, message: "必须填写" },
          {
            validator: ({ cellValue }) => {
              return new Promise((resolve, reject) => {
                if (/^\d+(.\d{1,8})?$/.test(cellValue) == false) {
                  reject(new Error("小数位的长度需在1-8之间"));
                } else {
                  resolve();
                }
              });
            },
          },
        ],
        fpshl: [{ required: true, message: "必须填写" }],
        fpsl: [{ required: true, message: "必须填写" }],
      },
      tableColumn: [
        { type: "seq", title: "序号", width: 60, align: "center" },
        {
          field: "fwmc",
          title: "名称",
          align: "left",
          width: 140,
          editRender: {},
          slots: { edit: "fwmc_edit" },
        },
        {
          field: "ggxh",
          title: "规格型号",
          width: 120,
          align: "left",
          showOverflow: "tooltip",
          editRender: {},
          slots: { edit: "ggxh_edit" },
        },
        {
          field: "fpdw",
          title: "单位",
          align: "left",
          width: 100,
          editRender: {},
          slots: { edit: "fpdw_edit" },
        },

        {
          field: "fpshl",
          title: "数量",
          align: "left",
          width: 100,
          editRender: {},
          slots: { edit: "fpshl_edit" },
        },
        {
          field: "fpdj",
          title: "不含税单价",
          align: "left",
          width: 120,
          editRender: {},
          slots: { edit: "fpdj_edit" },
        },
        {
          field: "fpje",
          title: "金额",
          align: "left",
          width: 100,
          editRender: {},
          slots: { edit: "fpje_edit" },
        },
        {
          field: "fpsl",
          title: "税率",
          align: "left",
          width: 100,
          editRender: {},
          slots: { edit: "fpsl_edit", default: "fpsl_default" },
        },
        {
          field: "fpse",
          title: "税额",
          align: "left",
          width: 100,
          editRender: {},
          slots: { edit: "fpse_edit" },
        },
        {
          field: "sjhj",
          title: "税价合计",
          align: "left",
          width: 100,
          editRender: {},
          slots: { edit: "sjhj_edit" },
        },
        { title: "操作", slots: { default: "operate" } },
      ],
      tableData: [],
      shuilOptions: [],
    };
  },
  created() {
    this.getFPSL();
  },
  watch: {
    editRow: {
      handler: function (val) {
        if (val == null) return;
        try {
          this.editRow.fpje = val.fpshl * val.fpdj;
        } catch {
          this.editRow.fpje = 0;
        }
        try {
          let se = (this.editRow.fpje * val.fpsl) / 100;
          this.editRow.fpse = Math.round((se + Number.EPSILON) * 100) / 100;
        } catch {
          this.editRow.fpse = 0;
        }
        try {
          this.editRow.sjhj = this.editRow.fpje + this.editRow.fpse;
        } catch {
          this.editRow.sjhj = 0;
        }
      },
      deep: true,
    },
  },
  methods: {
    async validAllEvent(back) {
      const $table = this.$refs.table;
      const errMap = await $table.validate(true).catch((errMap) => errMap);
      if (errMap) {
        back(false);
      } else {
        back(true);
      }
    },

    getFPSL() {
      this.getDicts("tax_ratio").then((response) => {
        let options = response.data;
        this.shuilOptions = options;
      });
    },
    openDlg(fpid, type) {
      this.fpid = fpid;
      this.fplx = type + "";
      this.editRow = null;
      this.getList();
      this.dlgShow = true;
    },
    close() {
      this.dlgShow = false;
    },
    editClose({ row }) {
      if (this.editRow != null) this.$refs.table.setActiveRow(this.editRow);
    },
    add() {
      this.validAllEvent((ret) => {
        if (ret == false) return;
        if (this.editRow != null) return;
        let row = {
          pkid: guid(),
          fpid: this.fpid,
          fplx: this.fplx,
          fwmc: "",
          ggxh: "",
          fpdw: "",
          fpje: "",
          fpdj: "",
          fpshl: "",
          fpsl: "",
          fpse: "",
          sjhj: "",
          _state: "add",
        };
        this.tableData.push(row);
        this.editRow = row;
        this.$refs.table.setActiveRow(row);
      });
    },
    show() {
      if (this.editRow != null) {
        this.$refs.table.setActiveRow(this.editRow);
      }
    },
    canCelRowEvent(row) {
      this.editRow = null;
      this.$refs.table.clearActived();
      this.getList();
    },
    delRowEvent(row) {
      this.$XModal.confirm("您确定要删除吗?").then((type) => {
        if (type == "confirm") {
          delCwfpglmx(row.pkid)
            .then((response) => {
              this.loading = false;
              this.editRow = null;
              if (response.code == 200) {
                this.$refs.table.remove(row);

                this.$XModal.message({
                  content: response.msg,
                  status: "success",
                });
              } else {
                this.$XModal.message({
                  content: response.msg,
                  status: "error",
                });
              }
            })
            .finally(() => {
              this.loading = false;
            });
        }
      });
    },
    saveRowEvent() {
      this.validAllEvent((ret) => {
        if (ret == false) return;
        this.loading = true;
        if (this.editRow._state == "add") {
          addCwfpglmx(this.editRow)
            .then((response) => {
              this.loading = false;
              if (response.code == 200) {
                this.editRow = null;
                this.$refs.table.clearActived();
                this.$XModal.message({
                  content: response.msg,
                  status: "success",
                });
              } else {
                this.$XModal.message({
                  content: response.msg,
                  status: "error",
                });
              }
            })
            .finally(() => {
              this.loading = false;
            });
        } else if (this.editRow._state == "edit") {
          updateCwfpglmx(this.editRow)
            .then((response) => {
              this.loading = false;
              if (response.code == 200) {
                this.editRow = null;
                this.$refs.table.clearActived();
                this.$XModal.message({
                  content: response.msg,
                  status: "success",
                });
              } else {
                this.$XModal.message({
                  content: response.msg,
                  status: "error",
                });
              }
            })
            .finally(() => {
              this.loading = false;
            });
        }
      });
    },
    editRowEvent(row) {
      this.editRow = row;
      this.editRow._state = "edit";
      this.$refs.table.setActiveRow(row);
    },
    getList() {
      this.loading = true;
      listCwfpglmx({
        fpid: this.fpid,
        fplx: this.fplx,
      }).then((response) => {
        this.tableData = response.rows;
        this.loading = false;
      });
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/assets/styles/global.scss";
::v-deep .vxe-modal--wrapper .vxe-modal--content {
  padding: 2px 2px 0px 2px;
}
</style>

 

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
对于vxe-grid,您可以按照以下步骤进表格编辑: 1. 安装vxe-table:您需要确保已经安装了vxe-table库。您可以使用npm或yarn来安装: ```bash npm install vxe-table @vxe-table/plugin-antd ``` 或 ```bash yarn add vxe-table @vxe-table/plugin-antd ``` 2. 导入和注册组件:在您的Vue组件中,您需要导入并注册vxe-table组件和所需的插件: ```javascript import 'vxe-table/lib/style.css'; import { VXETable, Table, Column, AntdGrid } from 'vxe-table'; import 'vxe-table-plugin-antd/dist/style.css'; VXETable.use(AntdGrid); Vue.use(Table); Vue.use(Column); ``` 3. 创建表格:在模板中创建vxe-grid组件并设置必要的属性和事件处理程序: ```html <template> <div> <vxe-grid :data="tableData" :columns="tableColumns" @edit-closed="onEditClosed" @edit-actived="onEditActived" ></vxe-grid> </div> </template> ``` 4. 设置数据和列:在您的Vue组件中,设置表格的数据和列属性。例如: ```javascript data() { return { tableData: [ { id: 1, name: 'John Doe', age: 25 }, { id: 2, name: 'Jane Smith', age: 30 }, // more data... ], tableColumns: [ { field: 'id', title: 'ID' }, { field: 'name', title: 'Name', editRender: { name: 'input' } }, { field: 'age', title: 'Age', editRender: { name: 'input' } }, // more columns... ], }; }, ``` 5. 处理编辑事件:添加相关的事件处理程序方法。例如,您可以在方法中更新表格数据: ```javascript methods: { onEditActived(params) { // 处理编辑开始事件 // 可以在这里进一些处理,例如保存之前的数据等 }, onEditClosed(params) { // 处理编辑结束事件 // 可以在这里更新表格数据 this.tableData = params.afterData; }, }, ``` 这是一个基本的vxe-grid表格编辑的示例。您可以根据您的需要进进一步的自定义和配置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值