当前位置: 首页 > news >正文

vue element-ui的table列表中展示缩略图片效果实例

这篇文章主要给大家介绍了关于vue element-ui的table列表中展示多张图片(可放大)效果的相关资料,文中通过代码示例介绍的非常详细,需要的朋友可以参考下

一、效果图

二、代码部分

1、原理 

 使用 `<el-table-column>` 和 `<el-image>` 组件来在表格中插入缩略图

2、template部分

我们使用 `<el-table>` 组件来创建一个表格。在 `<el-table-column>` 中,我们使用了 `<template>` 标签来定义插槽(slot),并通过 `slot-scope="scope"` 获取当前行的数据。

在插槽内部,我们使用了 `<el-image>` 组件来显示缩略图。使用 `v-for` 指令来循环遍历 `getImageUrls(scope.row)` 方法返回的缩略图 URL 数组,然后将每个 URL 绑定到 `<el-image>` 的 `:src` 属性上。

同时,我们使用 `:preview-src-list` 属性来设置点击缩略图时弹窗中显示的图片列表。 你需要根据实际需求,将 `tableData` 中的数据和 `getImageUrls` 方法进行相应的修改。

另外:想要调整缩略图大小在width更改

<template><el-table :data="tableData"><el-table-column prop="panoramic_image" label="全景图" align="center" width="150"><template slot-scope="scope"><el-imagev-for="(item, index) in getImageUrls(scope.row)":key="index":src="getImagePath(item)":preview-src-list="[getImagePath(item)]"></el-image></template></el-table-column></el-table>
</template>

3、script

 请根据你的项目实际情况,使用适合你的方法导入图片并设置正确的路径。 注意:如果你修改了图片的路径,也要相应地更新代码中的路径,确保能正确引用到图片。

<script>
export default {data() {return {tableData: [{panoramic_image: 'img1.jpg',},{panoramic_image: 'img2.jpg',},// ...],imagePathPrefix: 'assets/images/', // 图片文件夹路径前缀};},methods: {getImageUrls(row) {// 返回缩略图文件名数组},getImagePath(filename) {return this.imagePathPrefix + filename; // 拼接完整的图片路径},},
};
</script>

另一种写法 

<script>
export default {data() {return {tableData: [{panoramic_image: '@src/assets/img/image/image2.jpg',},{panoramic_image: '@src/assets/img/image/image1.jpg',},// ...],};},methods: {getImageUrls(row) {// 返回缩略图文件名数组},getImagePath(filename) {return this.imagePathPrefix + filename; // 拼接完整的图片路径},
};
</script>

总结

src不要写错了

一些疑问

Q:请逐句分析每句话:

 <el-imagev-for="(item, index) in getImageUrls(scope.row)":key="index":src="getImagePath(item)":preview-src-list="[getImagePath(item)]"></el-image>

A: 

 1. `v-for="(item, index) in getImageUrls(scope.row)"`:这是 Vue 的循环指令,它会根据 `getImageUrls(scope.row)` 返回的图片链接数组进行循环渲染,每次循环将数组中的元素绑定到 `item` 变量,循环的索引绑定到 `index` 变量。

2. `:key="index"`:这是 Vue 的关键字属性绑定,它用来标识每个循环渲染的元素的唯一性。在这里,将使用 `index` 作为每个 `<el-image>` 的唯一标识。

3. `:src="getImagePath(item)"`:这是将 `getImagePath(item)` 方法返回的图片链接绑定到 `<el-image>` 组件的 `src` 属性上,从而将图片的地址传递给组件用于显示。

4. `:preview-src-list="[getImagePath(item)]"`:这是将 `getImagePath(item)` 方法返回的图片链接封装到一个数组中,然后将该数组作为 `<el-image>` 组件的 `preview-src-list` 属性的值。`preview-src-list` 属性通常用于定义图片预览时的图片地址列表。

通过以上代码,可以实现根据 `scope.row` 对象中的数据,在每次循环中渲染出对应的图片。`getImageUrls` 方法用于获取图片链接,`getImagePath` 方法用于生成图片的完整地址。每次循环将会创建一个 `<el-image>` 组件,并设置对应的图片链接和预览图片链接。

 

 Q:能否精简这句代码呢?舍去不重要的部分,告诉我哪些是必要留下的。

 <el-imagev-for="(item, index) in getImageUrls(scope.row)":key="index":src="getImagePath(item)":preview-src-list="[getImagePath(item)]"></el-image>

A:

<el-imagev-for="(item, index) in getImageUrls(scope.row)" :key="index" :src="getImagePath(item)"></el-image>

 在这个精简的代码中,我们保留了以下部分:

1. `v-for="(item, index) in getImageUrls(scope.row)"`:循环遍历 `getImageUrls(scope.row)` 返回的图片链接数组,`item` 是当前循环的元素,`index` 是当前循环的索引。

2. `:key="index"`:为每个 `<el-image>` 组件添加唯一的 `key` 值,以辅助 Vue 进行渲染。

3. `:src="getImagePath(item)"`:将 `getImagePath(item)` 方法返回的图片链接绑定到 `<el-image>` 组件的 `src` 属性,用于显示图片。

通过以上精简后的代码,我们可以实现根据 `scope.row` 对象中的数据,在每次循环中渲染出对应的图片。

相关文章:

  • ES 原理和使用场景
  • 快速入门java网络编程基础------Nio
  • Java环境准备:JDK与IDEA
  • 网络地图服务(WMS)详解
  • IPv6组播--SSM Mapping
  • 搭建LNMP网站平台并部署Web应用
  • 第一次面试总结 - 迈瑞医疗 - 软件测试
  • 【打卡】牛客网:BM76 正则表达式匹配
  • 京东年度数据报告-2023全年度游戏本十大热门品牌销量(销额)榜单
  • 如何自动化部署和发布系统?
  • NLP论文阅读记录 - 2021 | WOS 使用深度强化学习及其他技术进行自动文本摘要
  • 什么是信噪比
  • k8s实战从入门到上天系列第一篇:K8s微服务实战内容开篇介绍
  • ApolloCarla联合仿真基本操作
  • k8s--集群调度(kube-scheduler)
  • 【347天】每日项目总结系列085(2018.01.18)
  • 【391天】每日项目总结系列128(2018.03.03)
  • 【译】理解JavaScript:new 关键字
  • 2017届校招提前批面试回顾
  • in typeof instanceof ===这些运算符有什么作用
  • Javascripit类型转换比较那点事儿,双等号(==)
  • JAVA并发编程--1.基础概念
  • Java多态
  • Java面向对象及其三大特征
  • Java应用性能调优
  • PHP 的 SAPI 是个什么东西
  • Spring Cloud Feign的两种使用姿势
  • VirtualBox 安装过程中出现 Running VMs found 错误的解决过程
  • 构建二叉树进行数值数组的去重及优化
  • 关于 Cirru Editor 存储格式
  • 回顾2016
  • 微信小程序实战练习(仿五洲到家微信版)
  • 问题之ssh中Host key verification failed的解决
  • 正则与JS中的正则
  • 【干货分享】dos命令大全
  • 不要一棍子打翻所有黑盒模型,其实可以让它们发挥作用 ...
  • 国内开源镜像站点
  • 浅谈sql中的in与not in,exists与not exists的区别
  • ​Spring Boot 分片上传文件
  • ​插件化DPI在商用WIFI中的价值
  • # 再次尝试 连接失败_无线WiFi无法连接到网络怎么办【解决方法】
  • (1/2)敏捷实践指南 Agile Practice Guide ([美] Project Management institute 著)
  • (52)只出现一次的数字III
  • (k8s中)docker netty OOM问题记录
  • (pojstep1.3.1)1017(构造法模拟)
  • (附源码)ssm高校志愿者服务系统 毕业设计 011648
  • (附源码)流浪动物保护平台的设计与实现 毕业设计 161154
  • (论文阅读11/100)Fast R-CNN
  • (提供数据集下载)基于大语言模型LangChain与ChatGLM3-6B本地知识库调优:数据集优化、参数调整、Prompt提示词优化实战
  • (已解决)报错:Could not load the Qt platform plugin “xcb“
  • (原創) 人會胖會瘦,都是自我要求的結果 (日記)
  • (原創) 如何使用ISO C++讀寫BMP圖檔? (C/C++) (Image Processing)
  • (转)chrome浏览器收藏夹(书签)的导出与导入
  • (转)h264中avc和flv数据的解析
  • (转载)虚函数剖析