Vue3+ts+element ui plus/antdesgin 实现可编辑单元格/可编辑功能

此功能是基于antdesgin表格组件可编辑单元格功能修改来实现,可查看原文档:带单元格编辑功能的表格

具体思路就是在element ui plus 或者 antdesgin 表格组件的单元格插槽中进行修改,放入“editable-cell”这个div就行;

此方法不仅适用于表格,相关需要自定义编辑的功能都可用此方法,核心就是自行调整“editable-cell”下的内容。

代码以及注释如下:


/*插槽内修改*/
<Table :columns="tableHeader" :data-source="tableData">
  <template #bodyCell="{ column: {dataIndex},index, text, record }">
      /*编辑主体*/
      <div class="editable-cell">
         /*如果editableData中存在这个要编辑的属性,则显示input编辑弹框*/
         <div v-if="editableData[record[dataIndex as string]+''+index]" class="editable-cell-input-wrapper">
            /*input绑定的值为editableData要编辑的属性(key)对应的值,以此来编辑*/
            <Input v-model:value="editableData[record[dataIndex as string]+''+index]"
                   @pressEnter="save(record[dataIndex as string]+''+index,dataIndex as string)"/>
            <icon class="editable-cell-icon-check"
                     @click="save(record[dataIndex as string]+''+index,dataIndex as string)">
                <Check/>
            </icon>
         </div>
         /*否则,显示edit图标*/
         <div v-else class="editable-cell-text-wrapper">
             /*通过checkCanBeEdit方法来判断这个单元格是否可编辑(显示编辑图标)*/
             <icon class="editable-cell-icon"
                      v-if="checkCanBeEdit(dataIndex as string)"
                      @click="edit(record,record[dataIndex as string]+''+index,dataIndex as string)">
                <Edit/>
             </icon>
         </div>
      </div>
  </template>
</Table>

ts:

tableData:表格数据  
tableHeader:表格头

/**
 * 将正在编辑的数据以key-value形式保存到editableData对象中,每个数据都赋予唯一的key(根据实际调整)
 * 编辑完成后(保存后都会更新最新的table数据) 将保存的key删除
 * 通过editableData中的正在编辑的属性有多少来显示input组件,否则显示编辑图标
 */
const editableData: UnwrapRef<Record<string, any>> = reactive({});

/**
 * 编辑框显示 (传入的参数可自行调整,只要能找到当前要修改的数据就行)
 * @param data 选中当前行的数据
 * @param col 当前列的表头名
 * @param key 修改的属性名
 */
const edit = (data: any, col: string, key: string,) => {
  /*将要修改的数据原本值赋给editableData中的唯一key*/
  editableData[key] = data[col];
};

/**
 * 保存修改的参数(传入的参数可自行调整,只要能找到当前要保存的数据就行)
 * @param col 当前列的表头名
 * @param key 修改的属性名
 */
const save = (key: string, col: string) => {
  /*以下方法并不重要,总之就是实现将editableData中修改的数据赋给tableData中对应的数据*/  
  tableData.value.filter((item, index) => {
    return key === item[col] + '' + index
  }).forEach((item) => {
    item[col] = editableData[key]
  });
  /*最后删除editableData中的数据*/
  delete editableData[key];
};
/**
 * 判断当前表头下对应的单元格是否可编辑
 * @param value 当前列的表头名
 */
function checkCanBeEdit(value: string): boolean {
  return !(['是否可编辑'].includes(value as string))
}

样式:

/*编辑主体样式(根据实际调整)*/
.editable-cell {
  position: relative;

  .editable-cell-input-wrapper,
  .editable-cell-text-wrapper {
    padding-right: 24px;
  }

  .editable-cell-text-wrapper {
    padding: 5px 24px 5px 5px;
  }

  .editable-cell-icon,
  .editable-cell-icon-check {
    position: absolute;
    right: 0;
    width: 20px;
    cursor: pointer;
  }

  .editable-cell-icon {
    margin-top: 4px;
    display: none;
  }

  .editable-cell-icon-check {
    line-height: 28px;
  }

  .editable-cell-icon:hover,
  .editable-cell-icon-check:hover {
    color: #108ee9;
  }
}
.editable-cell:hover .editable-cell-icon {
  display: inline-block;
}

  • 10
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先你需要在项目中安装 `element-plus`,可以通过以下命令进行安装: ``` npm install element-plus --save ``` 然后在你的 Vue 项目中引入 `element-plus` 的 `Table` 组件,以及需要使用的相关样式: ```js import { defineComponent } from 'vue'; import { ElTable, ElTableColumn } from 'element-plus'; import 'element-plus/lib/theme-chalk/index.css'; export default defineComponent({ components: { ElTable, ElTableColumn }, data() { return { tableData: [], // 后台获取的数据 }; }, mounted() { // 在这里调用后台接口获取数据,并将返回的数据赋值给 tableData }, render() { return ( <div> <el-table data={this.tableData}> <el-table-column prop="name" label="名称"></el-table-column> <el-table-column prop="age" label="年龄"></el-table-column> <el-table-column prop="address" label="地址"></el-table-column> </el-table> </div> ); }, }); ``` 在上面的代码中,我们使用了 `ElTable` 和 `ElTableColumn` 组件来渲染表格,其中 `data` 属性绑定了从后台获取的数据 `tableData`,每个 `el-table-column` 标签的 `prop` 属性绑定了对应数据对象的属性名,`label` 属性则是表格列的标题。 当然,你还需要在项目中引入 `element-plus` 的样式,这里我们直接引入了整个 `index.css` 文件来覆盖默认样式。如果你只需要使用部分组件,可以按需引入对应的样式文件。 以上就是使用 `element-plus` 的 `Table` 组件渲染后台数据的基本方法,你可以根据具体需求进行进一步的定制和优化。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值