search
HomeWeb Front-endLayui TutorialHow to display pictures in layui table

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.

How to display pictures in layui table

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

How to display pictures in layui table
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.
How to display pictures in layui table
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.
How to display pictures in layui table

    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.)
How to display pictures in layui table
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.
How to display pictures in layui table

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.
How to display pictures in layui tableHow to display pictures in layui table
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将图片从临时路径移动到最终路径。
How to display pictures in layui table

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!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How do I use Layui's flow module for infinite scrolling?How do I use Layui's flow module for infinite scrolling?Mar 18, 2025 pm 01:01 PM

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

How do I use Layui's element module to create tabs, accordions, and progress bars?How do I use Layui's element module to create tabs, accordions, and progress bars?Mar 18, 2025 pm 01:00 PM

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

How do I customize the appearance and behavior of Layui's carousel module?How do I customize the appearance and behavior of Layui's carousel module?Mar 18, 2025 pm 12:59 PM

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.

How do I use Layui's carousel module to create image sliders?How do I use Layui's carousel module to create image sliders?Mar 18, 2025 pm 12:58 PM

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.

How do I configure Layui's upload module to restrict file types and sizes?How do I configure Layui's upload module to restrict file types and sizes?Mar 18, 2025 pm 12:57 PM

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.

How do I use Layui's layer module to create modal windows and dialog boxes?How do I use Layui's layer module to create modal windows and dialog boxes?Mar 18, 2025 pm 12:46 PM

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.

How does Layui compare to other CSS frameworks like Bootstrap and Semantic UI?How does Layui compare to other CSS frameworks like Bootstrap and Semantic UI?Mar 14, 2025 pm 07:29 PM

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)

What are some advanced use cases for Layui beyond typical web applications?What are some advanced use cases for Layui beyond typical web applications?Mar 14, 2025 pm 07:28 PM

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)

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

DVWA

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

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows

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

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools