How to display images in layui tables: First, place a table tag, id and lay-filter in the HTML code; then use the custom template function in the js code to implement logical processing; finally declare a string type The variable is used to receive the name of the picture.
The operating environment of this tutorial: windows10 system, layui2.5.6. This article is applicable to all brands of computers.
Recommended: "layUI Tutorial"
1. The effect achieved
2. Code
HTML code
The HTML code is still similar to other tables. You only need to place a table tag in the appropriate place, and write both id and lay-filter. Just go up.
JS code
In the JS code, except that the picture column is different from the other columns, the other columns are basically similar. Of course, if you write some fixed columns, you will find that in your The fixed column written also needs to be different from other columns. Let’s talk about the driver picture column first. The function of custom template (templet) is used in this column. You can use this feature to perform logical processing and convert raw data into other formats. Of course, I didn't use data conversion here. Here I just use this function to add some styles.
layui.use(['table', 'layer'], function () { layuiTable = layui.table; layer = layui.layer; tabDriver = layuiTable.render({ elem: "#tabDriver", cellMinWidth: 100, height: 'full-200', cols: [[ { type: 'checkbox', align: "center", fixed: "left", style: "height:110px;"}, { type: 'numbers', title: "序号", align: "center", fixed: "left", style: "height:110px;" }, { field: 'DriverID', title: 'DriverID', hide: true }, { field: 'PassengerCarID', title: 'PassengerCarID', hide: true }, { field: 'DriverPicture', title: '驾驶员照片', align: "center", templet: "#imgtmp" }, { field: 'DriverCode', title: '驾驶员编号', align: "center", width: 120 }, { field: 'DirverName', title: '姓名', align: "center" }, { field: 'DriverSex', title: '性别', align: "center" }, { field: 'DriverMovePhone', title: '联系电话', align: "center", width: 130 }, { field: 'DriverIDNum', title: '身份证号', align: "center", width: 175 }, { field: 'OccupationalNumber', title: '从业资格证号', align: "center", width: 120 }, { field: 'PassengerCarCode', title: '驾驶车辆编号', align: "center", width: 120 }, { field: 'DriverNumber', title: '驾驶证号', align: "center", width: 100 }, { field: 'DrivingType', title: '准驾车型', align: "center", width: 100 }, { field: 'StrDrivingDay', title: '驾驶证审验期', align: "center", width: 120 }, { field: 'StrOccupationalDay', title: '从业资格证审验期', align: "center", width: 150 }, { field: 'strSGZUseLifes', title: '上岗证有效期', align: "center", width: 150 }, { field: 'DriverRemark', title: '备注', align: "center" }, { title: '操作', templet: setOperate, width: 100, align: "center", fixed: "right", style: "height:110px;" }, ]], page: { limit: 10,//指定每页显示条数 limits: [5, 10, 15, 20, 25, 30, 35, 40, 45, 50],//每页条数的选择项 }, data: [], toolbar: "#toolbarDemo", }); //监听事件 layuiTable.on('row(tabDriver)', function (obj) { //标中选中样式 obj.tr.addClass("layui-table-click").siblings().removeClass("layui-table-click"); //选中行,勾选复选框 obj.tr.find("p.layui-unselect.layui-form-checkbox")[1].click(); }); });
Custom template (templet)
Here, the way to write a custom template is very simple. The outermost layer is wrapped with a script tag. The type of the script tag is text/html and the id is imgtep. (The id here must be consistent with the id in the templet in the column of the driver's photo in the layui table). Use an img tag in the script tag to display the driver's photo, and give the img tag some fixed width and height. Among them, {{d.DriverPicture}} in src represents the path of the corresponding picture queried from the database. (My database here saves the path of the corresponding picture, and the corresponding picture is saved in a special folder in the project. Instead of using binary saved pictures. If you use binary to save pictures, the data must be processed after querying. Conversion.)
If fixed columns are set in the layui data table
Add style to the corresponding fixed columns, and then set the height of these fixed columns.
Controller Code - Save Picture
I am too lazy to write some queries on the controller side. First, use the name received by HttpPostedFileBase in the controller method. This A form to receive the image information passed. Note: The received name must be the same as the name of the hidden type input tag under the img tag on the page.
First save the other data except the picture, and then process the picture
First declare a string type variable to receive the name of the last picture, and then whether fileDriverImage is empty , if not equal to empty. First get the suffix name of the image, which will be used later to determine whether the passed image is the image type. Five random strings are spliced in front of the picture name (fileName) to prevent exceptions when pictures with the same name appear during modification. Then determine whether the path to Bai Cun's picture exists. If the path does not exist, create the corresponding path in the project. The path is divided into two, a temporary path is used to save the pictures after uploading, but before the data is saved to the database. A final path used to save the image after the data is saved successfully. Then splice the temporary path where the picture is saved and the path of the picture to be saved to the database. Then assign the path of the spliced picture to be saved to the database to the corresponding field in the table object to be saved. Then judge the suffix name obtained previously, convert all the suffix names into lowercase, and then determine whether it is an image type. If it is a picture type, save the picture to a temporary path.
string fileName = ""; //判断图片是否为空 if (fileDriverImage != null) { string fileExtension = System.IO.Path.GetExtension(fileDriverImage.FileName); fileName = Common.ValidCodeUtils.GetRandomCode(5) + fileDriverImage.FileName; //判断是否存在该路径,若不存在,创建 最终路径 if (!Directory.Exists(Server.MapPath("~/Document/BusinessManagement/Driverimg/"))) { Directory.CreateDirectory(Server.MapPath("~/Document/BusinessManagement/Driverimg/")); } //临时路径 if (!Directory.Exists(Server.MapPath("~/Document/BusinessManagement/Temp/"))) { Directory.CreateDirectory(Server.MapPath("~/Document/BusinessManagement/Temp/")); } //拼接保存的图片路径 string fileTempPath = Server.MapPath("/Document/BusinessManagement/Temp/") + fileDriverImage.FileName; string fileSavePath = "/Document/BusinessManagement/Driverimg/" + fileDriverImage.FileName; sysDriver.DriverPicture = fileSavePath; if (fileExtension != null) { fileExtension = fileExtension.ToLower(); //将后缀转化为小写 //判断文件扩展名是否是指定的图片类型 if ("(.gif)|(.jpg)|(.bmp)|(.jpeg)|(.png)".Contains(fileExtension)) { fileDriverImage.SaveAs(fileTempPath); //保存文件 } } }
数据库保存成功之后将图片从临时路径移动到最终路径
在数据保存成功之后,判断获取到的图片的文件是否为空,若不为空,获取图片在临时路径中的路径和在最终路径中的路径。然后使用IO中的Move将图片从临时路径移动到最终路径。
The above is the detailed content of How to display pictures in layui table. For more information, please follow other related articles on the PHP Chinese website!

The article discusses using Layui's flow module for infinite scrolling, covering setup, best practices, performance optimization, and customization for enhanced user experience.

The article details how to use Layui's element module to create and customize UI elements like tabs, accordions, and progress bars, highlighting HTML structures, initialization, and common pitfalls to avoid.Character count: 159

The article discusses customizing Layui's carousel module, focusing on CSS and JavaScript modifications for appearance and behavior, including transition effects, autoplay settings, and adding custom navigation controls.

The article guides on using Layui's carousel module for image sliders, detailing steps for setup, customization options, implementing autoplay and navigation, and performance optimization strategies.

The article discusses configuring Layui's upload module to restrict file types and sizes using accept, exts, and size properties, and customizing error messages for violations.

The article explains how to use Layui's layer module to create modal windows and dialog boxes, detailing setup, types, customization, and common pitfalls to avoid.

Layui, known for simplicity and performance, is compared with Bootstrap and Semantic UI on design, components, and integration ease. Layui excels in modularity and Chinese support.(159 characters)

Layui extends beyond basic web apps to SPAs, real-time dashboards, PWAs, and complex data visualization, enhancing enterprise-level user experiences with its modular design and rich UI components.(159 characters)


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools