lixiangyu
昨天 8b5bfdeb201b2653fb648f742b6d4ff5ff05ff6c
src/views/cms/CuttingPropertiesList.vue
@@ -1,5 +1,17 @@
<template>
  <a-card :bordered="false">
    <!-- 新增按钮 -->
    <div class="table-operator" style="margin-bottom: 10px;">
      <a-button
        @click="handleAdd"
        type="primary"
        icon="plus"
        :disabled="queryParam.cuttingId === '-1' || !queryParam.cuttingId"
      >
        新增
      </a-button>
    </div>
    <!-- table区域-begin -->
    <div>
      <a-table
@@ -35,16 +47,40 @@
          </a-button>
        </template>
        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>
        <!-- 可编辑单元格 -->
        <template
          v-for="col in ['propertyCode', 'propertyName', 'propertyUnit', 'propertyValue']"
          :slot="col"
          slot-scope="text, record"
        >
          <div :key="col">
            <a-input
              v-if="record.editable"
              style="margin: -5px 0"
              :value="text"
              @change="e => handleChange(e.target.value, record.id, col)"
            />
            <template v-else>
              {{ text }}
            </template>
          </div>
        </template>
        <!-- 操作列 -->
        <span slot="action" slot-scope="text, record">
          <div class="editable-row-operations">
            <span v-if="record.editable">
              <a-popconfirm title="确定保存?" @confirm="() => save(record.id)">
                <a>保存</a>
              </a-popconfirm>
              <a style="margin-left: 8px" @click="() => cancel(record.id)">取消</a>
            </span>
            <span v-else>
              <a :disabled="editingKey !== ''" @click="() => edit(record.id)">编辑</a>
          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
@@ -53,6 +89,8 @@
            </a-menu>
          </a-dropdown>
        </span>
          </div>
        </span>
      </a-table>
    </div>
@@ -60,10 +98,10 @@
</template>
<script>
  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { putAction, getAction, postAction } from '@/api/manage'
  export default {
    name: 'CuttingPropertiesList',
@@ -73,6 +111,7 @@
    data () {
      return {
        description: '刀具扩展属性管理页面',
      editingKey: '',
        // 表头
        columns: [
          {
@@ -88,22 +127,33 @@
          {
            title:'属性编码',
            align:"center",
            dataIndex: 'propertyCode'
          dataIndex: 'propertyCode',
          scopedSlots: { customRender: 'propertyCode' }
          },
          {
            title:'属性名称',
            align:"center",
            dataIndex: 'propertyName'
          dataIndex: 'propertyName',
          scopedSlots: { customRender: 'propertyName' }
          },
          {
            title:'计量单位',
            align:"center",
            dataIndex: 'propertyUnit'
          dataIndex: 'propertyUnit',
          scopedSlots: { customRender: 'propertyUnit' }
          },
          {
            title:'属性值',
            align:"center",
            dataIndex: 'propertyValue'
          dataIndex: 'propertyValue',
          scopedSlots: { customRender: 'propertyValue' }
        },
        {
          title: '操作',
          dataIndex: 'action',
          align:"center",
          scopedSlots: { customRender: 'action' },
          width: 200,
          }
        ],
        url: {
@@ -112,7 +162,8 @@
          deleteBatch: "/cms/cuttingProperties/deleteBatch",
          exportXlsUrl: "/cms/cuttingProperties/exportXls",
          importExcelUrl: "cms/cuttingProperties/importExcel",
        edit: "/cms/cuttingProperties/edit",
        add: "/cms/cuttingProperties/add" // 添加新增接口
        },
        dictOptions:{},
        superFieldList:[],
@@ -142,9 +193,154 @@
      this.queryParam.cuttingId = cuttingId;
      this.loadData(1);
    },
    // 新增行
    handleAdd() {
      // 如果已经在编辑状态,不允许新增
      if (this.editingKey !== '') {
        // 查找正在编辑的记录并取消编辑
        const editingRecord = this.dataSource.find(item => item.id === this.editingKey);
        if (editingRecord) {
          this.cancel(this.editingKey);
        }
      }
      // 创建新的空数据项
      const newRow = {
        id: 'new_' + new Date().getTime(), // 临时ID
        cuttingId: this.queryParam.cuttingId,
        propertyCode: '',
        propertyName: '',
        propertyUnit: '',
        propertyValue: '',
        editable: true,
        isNew: true // 标记为新添加的行
      };
      // 将新行插入到数据源开头
      this.dataSource = [newRow, ...this.dataSource];
      this.editingKey = newRow.id;
    },
    // 编辑行数据
    edit(key) {
      // 先保存当前数据的副本
      const newData = [...this.dataSource];
      const target = newData.find(item => key === item.id);
      this.editingKey = key;
      if (target) {
        target.editable = true;
        this.dataSource = newData;
      }
    },
    // 保存编辑的数据
    save(key) {
      const newData = [...this.dataSource];
      const target = newData.find(item => key === item.id);
      if (target) {
        delete target.editable;
        this.dataSource = newData;
        // 判断是新增还是编辑
        if (target.isNew) {
          // 新增操作
          const params = { ...target };
          delete params.id;
          delete params.isNew;
          // 删除创建时间字段,让后端自动生成
          delete params.createTime;
          delete params.updateTime;
          postAction(this.url.add, params).then(res => {
            if (res.success) {
              this.$message.success('新增成功');
              this.editingKey = '';
              this.loadData(); // 重新加载数据
            } else {
              this.$message.error(res.message);
            }
          }).catch(err => {
            this.$message.error('新增失败');
            console.log(err);
          });
        } else {
          // 编辑操作 - 只发送需要更新的字段
          const params = {
            id: target.id,
            propertyCode: target.propertyCode,
            propertyName: target.propertyName,
            propertyUnit: target.propertyUnit,
            propertyValue: target.propertyValue,
            // 不包含 createTime 字段
          };
          putAction(this.url.edit, params).then(res => {
            if (res.success) {
              this.$message.success('保存成功');
              this.editingKey = '';
              this.loadData(); // 重新加载数据
            } else {
              this.$message.error(res.message);
            }
          }).catch(err => {
            this.$message.error('保存失败');
            console.log(err);
          });
        }
      }
    },
    // 取消编辑
    cancel(key) {
      const newData = [...this.dataSource];
      const targetIndex = newData.findIndex(item => key === item.id);
      this.editingKey = '';
      if (targetIndex >= 0) {
        // 如果是新增的行,直接删除
        if (newData[targetIndex].isNew) {
          newData.splice(targetIndex, 1);
          this.dataSource = newData;
        } else {
          // 如果是编辑的行,从后端重新获取数据以恢复原始值
          const target = newData[targetIndex];
          getAction(this.url.list, { id: key }).then(res => {
            if(res.success && res.result.records.length > 0) {
              const originalData = res.result.records[0];
              Object.keys(target).forEach(key => {
                if(key !== 'editable') {
                  target[key] = originalData[key];
                }
              });
              delete target.editable;
              this.dataSource = newData;
            }
          });
        }
      }
    },
    // 处理单元格值变化
    handleChange(value, key, column) {
      const newData = [...this.dataSource];
      const target = newData.find(item => key === item.id);
      if (target) {
        target[column] = value;
        this.dataSource = newData;
      }
    }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
.editable-row-operations a {
  margin-right: 8px;
}
</style>