可编辑表格

该内容是实现表格内容的新增、编辑和删除操作

效果图如下:

一、添加表格各个属性

<a-table style="width: 1200px;margin-left: 20px;border-collapse: collapse;" :columns="Columns"  bordered :pagination="false"
      :scroll="{ y: 120 }" >
        <template #bodyCell="{ column, text, record }">
          
        </template>
      </a-table>
const Columns = [
  { title: '字段英文名称', dataIndex: "fieldEname", width: '7%',fixed: 'left', align: 'center'},
  { title: '字段中文名称', dataIndex: "fieldCname",width: '7%', align: 'center'},
  { title: '字段说明', dataIndex: "fieldDesc",width: '30%', align: 'center'},
  { title: '标准映射', dataIndex: "standardId",width: '15%', align: 'center'},
  { title: '操作', dataIndex: "methods",width: '7%', align: 'center'}
];

注意:可自行修改

二、添加新增绑定事件

 <div>
      <a-button @click="Addfield" style="color: #fff;margin-left: 40px;margin-top: 10px;" type="primary">添加字段</a-button>
    </div>

注意:可自行修改

三、添加数据源绑定以及可编辑表格实现

3.1根据需求添加渲染逻辑

<a-table style="width: 1200px;margin-left: 20px;border-collapse: collapse;" :columns="Columns" :dataSource="dataSource" bordered :pagination="false"
      :scroll="{ y: 120 }" >
        <template #bodyCell="{ column, text, record }">
          <template v-if="['fieldCname', 'fieldEname', 'fieldDesc'].includes(column.dataIndex)">
            <div>
              <a-input
                v-if="editableData[record.id]"
                v-model:value="editableData[record.id][column.dataIndex]"
                style="margin: -5px 0"
              />
              <template v-else>
                {{ text }}  
              </template>
            </div>
          </template>

          <template v-else-if="column.dataIndex === 'standardId'">
            <a-select v-if="editableData[record.id]" v-model:value="editableData[record.id][column.dataIndex]" style="width: 200px;">
              <a-select-option value="1">BZ0001 企业名称 name</a-select-option>
              <!-- 添加更多选项 -->
            </a-select>
            <template v-else>
              {{ text }}
            </template>
          </template>

          <template v-else-if="column.dataIndex === 'methods'">
            <div class="editable-row-operations">
              <span v-if="editableData[record.id]">
                <a-typography-link @click="save(record.id)" style="margin-right: 10px;color: #00BFFF;">保存</a-typography-link>
                <a-popconfirm title="确认取消编辑吗?" @confirm="cancel(record.id)">
                  <a style="color: #00BFFF;">取消</a>
                </a-popconfirm>
              </span>
              <span v-else>
                <a @click="edit(record.id)" style="margin-right: 10px;color: #00BFFF;">编辑</a>
                <a-popconfirm
                  v-if="dataSource.length"
                  title="确定删除吗?"
                  @confirm="remove(record.id)"
                >
                  <a style="color: #00BFFF;">删除</a>
                </a-popconfirm>
              </span>
            </div>
          </template>
        </template>
      </a-table>

3.2添加可编辑表格逻辑

const dataSource = ref([]);
const editableData = reactive({});
const edit = id => {
  editableData[id] = cloneDeep(dataSource.value.filter(item => id === item.id)[0]);
};
const save = id => {
  Object.assign(dataSource.value.filter(item => id === item.id)[0], editableData[id]);
  delete editableData[id];
};
const remove  = id =>{
  dataSource.value = dataSource.value.filter(item => item.id !== id);
}
const cancel = id => {
  delete editableData[id];
};

3.3添加新增表格内容

const Addfield = () => {
  // 检查 dataSource 是否已定义s
  if (!dataSource.value) {
    dataSource.value = [];
  }
  // 生成新的行数据
  const newRow = {
    id: Date.now(), // 使用时间戳作为新行的 key,确保唯一性
    fieldCname: '阿萨大大', // 新行的其他属性值设为默认值
    fieldEname: 'fghjk',
    fieldDesc: 'sdfghj',
    standardId:'BZ0001 企业名称 name'
  };
  // 将新行数据添加到表格数据源中
  dataSource.value.push(newRow);
};

注意:所有的表格参数以及样式可自行修改

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值