首页 > 其他分享 >element_ui实现表格内套表单,点击可以编辑

element_ui实现表格内套表单,点击可以编辑

时间:2023-05-25 14:24:07浏览次数:34  
标签:表格 column element item ui scope 内套 id row

<template>
<div class="app-container">
<el-table :data="list" stripe style="width: 100%" @cell-dblclick="openEditColumn">
<el-table-column prop="cameraX" label="坐标位置:X">
<template slot-scope="scope">
<span v-show="!scope.row.xEdit">{{ scope.row.cameraX }}</span>
<el-input @blur="editColumnData(scope.row, 'cameraX')"
@keyup.enter.native="editColumnData(scope.row, 'cameraX')" v-show="scope.row.xEdit"
size="mini" v-model="scope.row.cameraX"></el-input>
</template>
</el-table-column>
<el-table-column prop="cameraY" label="坐标位置:Y">
<template slot-scope="scope">
<span v-show="!scope.row.yEdit">{{ scope.row.cameraY }}</span>
<el-input @blur="editColumnData(scope.row, 'cameraY')"
@keyup.enter.native="editColumnData(scope.row, 'cameraY')" v-show="scope.row.yEdit"
size="mini" v-model="scope.row.cameraY"></el-input>
</template>
</el-table-column>
<el-table-column prop="cameraZ" label="坐标位置:Z">
<template slot-scope="scope">
<span v-show="!scope.row.zEdit">{{ scope.row.cameraZ }}</span>
<el-input @blur="editColumnData(scope.row, 'cameraZ')"
@keyup.enter.native="editColumnData(scope.row, 'cameraZ')" v-show="scope.row.zEdit"
size="mini" v-model="scope.row.cameraZ"></el-input>
</template>
</el-table-column>
</el-table>
</div>
</template>
<script>
export default {
name: "handleViewCamera",
components: {},
props: {
},
data() {
return {
list: {},
};
},
created() {
},
mounted() {
this.getList();
},
methods: {
// 获取数据列表
getList() {
queryList(this.regionId).then((res) => {
res.data.cameras.forEach(item => {
item.xEdit = false;
item.yEdit = false;
item.zEdit = false;
this.list.push(item)
});
});
},
// 打开信息编辑
openEditColumn(row, column, cell, event) {
if (column.property === "cameraX") {
this.equipmentList.cameras.forEach(item => {
if (item.id === row.id) {
// 激活当前点击的单元格进入可以编辑(span与el-input标签显示隐藏切换)
item.xEdit = true
}
});
} else if (column.property === "cameraY") {
this.equipmentList.cameras.forEach(item => {
if (item.id === row.id) {
item.yEdit = true
}
});
} else if (column.property === "cameraZ") {
this.equipmentList.cameras.forEach(item => {
if (item.id === row.id) {
item.zEdit = true
}
});
}
},
// 表格数据编辑保存并关闭编辑
editColumnData(row, column) {
// 关闭表格编辑
this.list.forEach(item => {
if (item.id === row.id) {
if (column === "cameraX") {
item.xEdit = false;
} else if (column === "cameraY") {
item.yEdit = false;
} else if (column === "cameraZ") {
item.zEdit = false;
}
}
});
}
},
};
但是表单大在小和表格不同,比较不自然,可以设样式来解决:以下为参考
在需要套表单输入框的表格列中,使用 scoped slot 的方式来渲染表格单元格。例如:
<template slot-scope="scope">
  <el-form>
    <el-form-item>
      <el-input :border="false" v-model="scope.row.data"></el-input>
    </el-form-item>
  </el-form>
</template>
<template slot-scope="scope">
  <el-form>
    <el-form-item>
      <el-input :border="false" v-model="scope.row.data"></el-input>
    </el-form-item>
  </el-form>
</template>
​
在 el-input 组件中添加 ​:border="false"​ 属性,来设置输入框无边框。

根据表格列的宽度,调整 el-form 和 el-input 的宽度,使其与表格列一致。例如:

<template slot-scope="scope">
  <el-form :style="{width: scope.col.width + 'px'}">
    <el-form-item :style="{marginBottom: '0'}">
      <el-input :border="false" v-model="scope.row.data" :style="{width: (scope.col.width - 20) + 'px'}"></el-input>
    </el-form-item>
  </el-form>
</template>
<template slot-scope="scope">
  <el-form :style="{width: scope.col.width + 'px'}">
    <el-form-item :style="{marginBottom: '0'}">
      <el-input :border="false" v-model="scope.row.data" :style="{width: (scope.col.width - 20) + 'px'}"></el-input>
    </el-form-item>
  </el-form>
</template>
​
在上述代码中,使用了 ​scope.col.width​ 来获取当前表格列的宽度,并根据表格列的宽度来设置 el-form 和 el-input 的宽度。其中,减去了 20 像素的宽度,是为了留出一定的空隙,使得输入框和表格列之间有一定的间距。

最后,将以上代码加入到 el-table-column 中,即可实现表格里面套表单输入框的效果。例如:
<el-table-column prop="data" label="数据">
  <template slot-scope="scope">
    <el-form :style="{width: scope.col.width + 'px'}">
      <el-form-item :style="{marginBottom: '0'}">
        <el-input :border="false" v-model="scope.row.data" :style="{width: (scope.col.width - 20) + 'px'}"></el-input>
      </el-form-item>
    </el-form>
  </template>
</el-table-column>
<el-table-column prop="data" label="数据">
  <template slot-scope="scope">
    <el-form :style="{width: scope.col.width + 'px'}">
      <el-form-item :style="{marginBottom: '0'}">
        <el-input :border="false" v-model="scope.row.data" :style="{width: (scope.col.width - 20) + 'px'}"></el-input>
      </el-form-item>
    </el-form>
  </template>
</el-table-column>
​
希望这可以帮助您完成 Element_ui 表格里面套表单输入框的功能。

 













 

标签:表格,column,element,item,ui,scope,内套,id,row
From: https://www.cnblogs.com/fgxwan/p/17431067.html

相关文章

  • Centos 7 Squid正向代理
    环境centos7(1台),windows(1台)Centos7,两张网卡,一张网卡是内网不能上网,IP地址为192.168.10.1/24另一张网卡设置为桥接模式要求可以上网。Windows,网卡为内网IP地址为192.168.10.2/24,DNS为192.168.10.1两边都关闭防火墙和子系统。正向代理的缺点:客户端需要单独配置,才能使用正向代理功......
  • vue3+vite build打包删除控制台打印
    很简单,直接在vite.config.ts里面添加配置即可:exportdefault({mode})=>defineConfig({build:{minify:"terser",terserOptions:{compress:{//生产环境时移除consoledrop_console:true,drop_debugger:true,......
  • 关于《Building a GraphQL service》的尝试
      以下是实现代码packagecom.example.graphqlserver;importjava.util.Arrays;importjava.util.List;publicrecordAuthor(Stringid,StringfirstName,StringlastName){privatestaticList<Author>authors=Arrays.asList(newAuthor(......
  • layui table.exportFile 导出数据常见应用场景
    layuitable.exportFile导出数据常见应用场景置顶jhadjahjhb于2020-12-2216:46:36发布5896收藏8分类专栏:layui文章标签:layui版权layui专栏收录该内容1篇文章0订阅订阅专栏layuitable.exportFile导出数据数据准备工作场景1,导出全部数据场景2,导出......
  • String 和 StringBuilder的区别与性能
    string和StringBuilder都可以用在串联字符串上,但是其性能在不同的场景下会有很大的差别。1、使用string+场景:多个字串通过+来进行连接,如下所示,此时都在一条语句里面,因此内部会通过StringBuilder对象来完成对应的连接,此时推荐使用string+来连接字串strings1,s2,s3,......
  • Android tcp、okhttp请求&&python tcp&&a bytes-like object is required, not ‘str
    更多内容欢迎来到我的新博客blog.devilwst.top更多内容欢迎来到我的新博客blog.devilwst.top首先声明一下,本文部分代码来自于博客AndroidpublicstaticvoidGetByHttpURL(finalStringurl){newThread(newRunnable(){@Overridepublicvo......
  • 多线程合集(三)---异步的那些事之自定义AsyncTaskMethodBuilder
    引言之前在上一篇文章中多线程合集(二)---异步的那些事,async和await原理抛析,我们从源码去分析了async和await如何运行,以及将编译后的IL代码写成了c#代码,以及实现自定义的Awaiter,自定义异步状态机同时将本系列的第一篇文章的自定义TaskScheduler和自定义的Awaiter......
  • Microsoft Build 云技能挑战 2023:免费认证考试
    参加MicrosoftBuild并完成MicrosoftBuild云技能挑战。按照条款及条件,通过完成MicrosoftLearn上的八个独特集合之一,提高、扩展和发现新技能并获得免费认证考试。挑战将持续到6月20日,因此请立即注册并开始,以避免错失机会。以帮助你持续提升技能,并向你的雇主和同事证明......
  • 图形学流体力学Fluid Simulation for Computer Graphics
    从水的飞溅,到火焰和烟雾的旋转,流体已经成为计算机图形学的一个重要组成部分。这本书旨在涵盖模拟这些动画效果的基本知识。让我们来看看控制它们运动的基本方程。动画中大多数有趣的流体流动都是由著名的incompressibleNavier-Stokes方程控制的。>>fluidenginedevelopment>>......
  • 找不到“element-plus/global”的类型定义文件。 程序包含该文件是因为: 在 compilerO
    问题描述在tsconfig.json文件里types字段添加"element-plus/global"后出现报错。问题原因TS升级到5.x带来的规范性问题。可以通过npmviewtypescriptversion命令查看下你的TS版本。深层分析参考这篇:https://github.com/element-plus/element-plus/issues/12119问题解决1......