Home Web Front-end Layui Tutorial Some problems with layui uploading files and data tables

Some problems with layui uploading files and data tables

Dec 12, 2019 pm 04:45 PM
layui

Some problems with layui uploading files and data tables

layui是一款我比较喜欢的框架,它的界面风格和颜色搭配都是让人比较舒服的,所以我非常喜欢使用layui。

接下来就是在工作中使用layui遇到了一些比较细节的问题:

第一:layui上传文件的问题,

第二:layui 表格的问题。

首先第一个问题,就是layui上传文件的问题,首先我们来看layui是如何上传文件的:

function UpdateFile() {
        layui.use('upload', function () {
            var upload = layui.upload;
            //执行实例
            var uploadInst = upload.render({
                elem: '#upload' //绑定元素
                , url: '/ExcelTemplate'//上传接口
                , method: 'POST'
                , type: "file"
                , accept: 'file'
                , before: function (obj) {
                    layer.load(); //上传loading
                }
                , done: function (res) {
                    //上传完毕回调
                    if (res) {
                        layer.closeAll('loading');
                        var d = dialog({
                            title: '提示',
                            content: '上传模板成功',
                            width: 200,
                            ok: function () { self.location.reload(); },
                        });
                        d.show();
                    } else {
                        layer.closeAll('loading');
                        var d = dialog({
                            title: '提示',
                            content: '上传模板失败',
                            width: 200,
                            ok: function () { },
                        });
                        d.show();
                    }
                }
                , error: function () {
                    layer.closeAll('loading');
                }
            });
        });
    }
Copy after login

当然你需要在你的页面上定义一个按钮,然后触发点击事件,elem: '#upload' 就是用来与你的上传按钮做绑定了,接下来就是文件类型还有用post来传输。

然后我们需要在后台用一个参数去接收文件。

[HttpPost("")]
        public IActionResult UploadTemplate(IFormFile file)
        {
            long dateTime = DateTime.Now.ToFileTimeUtc();
            string[] template = file.FileName.Split(new char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries);
            string fileName = Path.Combine(hostingEnvironment.WebRootPath, "Upload", "ExcelTemplate", dateTime + "_" + template[template.Length - 1]);
            if (System.IO.File.Exists(fileName))
            {
                System.IO.File.Delete(fileName);
            }

            try
            {
                using (FileStream fs = new FileStream(fileName, FileMode.Create))
                {
                    file.CopyTo(fs);
                    fs.Flush();
                    return Ok(true);
                }
            }
            catch (Exception)
            {
                return BadRequest("上传模板失败!");
            }
        }
Copy after login

这里是用IFormFile 去接收文件,参数名最好是file,然后对文件进行操作,那么上传的文件要怎么才能下载呢,如下:

<script type="text/html" id="down">
    <a href="~/Upload/ExcelTemplate/{{d.name}}" download="{{d.name}}" class="layui-table-link">下载</a>
</script>
Copy after login

在表格中显示和下载。

第二就是表格的问题了:

layui.use([&#39;table&#39;, &#39;laypage&#39;], function () {
            var laypage = layui.laypage;
            var table = layui.table,
                form = layui.form;
            table.render({
                elem: &#39;#PaymentDayList&#39;
                , url: &#39;/PaymentDay&#39;
                , method: "get"
                , height: "auto"
                , width: "auto"
                , cellMinWidth: 80
                , limit: 10
                , curr: 1
                , request: {
                    pageName: &#39;pageIndex&#39;
                }
                , page: {
                    layout: [&#39;limit&#39;, &#39;count&#39;, &#39;prev&#39;, &#39;page&#39;, &#39;next&#39;, &#39;skip&#39;] //自定义分页布局
                    , groups: 5 //只显示 1 个连续页码
                    , first: false //不显示首页
                    , last: false //不显示尾页

                }
                , limits: [10, 20, 50, 100, 500, 1000]
                , cols: [[

                    { type: "checkbox", fixed: "left" },
                    { type: &#39;numbers&#39;, title: &#39;序号&#39; },
                    { field: &#39;name&#39;, title: &#39;账期名称&#39;, sort: true, width: 200 },
                    { field: &#39;settleMentInterval&#39;, title: &#39;结算周期&#39;, sort: true, width: 100 },
                    { field: &#39;startTime&#39;, title: &#39;账期起始时间&#39;, sort: true, width: 150 },
                    { field: &#39;endTime&#39;, title: &#39;账期终止时间&#39;, sort: true, width: 150 },
                    { field: &#39;warnDay&#39;, title: &#39;到期提醒日&#39;, sort: true, width: 150 },
                    { field: &#39;userName&#39;, title: &#39;商保专员&#39;, sort: false, width: 100 },
                    { field: &#39;addTime&#39;, title: &#39;创建时间&#39;, sort: true, width: 200 },
                    { field: &#39;isEnabled&#39;, title: &#39;启用&#39;, templet: &#39;#checkboxTpl&#39;, width: 100 },
                    { field: &#39;status&#39;, title: &#39;状态&#39;, sort: false, width: 100 },
                    { field: &#39;scope&#39;, title: &#39;适用范围&#39;, sort: false, width: 100 },

                ]]
            });
            $(&#39;#Select&#39;).on(&#39;click&#39;, function () {
                table.reload("PaymentDayList", {
                    page: {
                        curr: 1
                    }
                    , where: {
                        name: $("#name").val(),
                        startTime: $("#startTime").val(),
                        endTime: $("#endTime").val(),
                        status: $("#type option:selected").val()
                    }
                });
            });
            form.on(&#39;checkbox(lockDemo)&#39;, function (obj) {

                var isEnable;
                obj.elem.checked == true ? isEnable = "启用" : isEnable = "未启用";
                $.ajax({
                    url: &#39;/PaymentDay/Enabled/&#39; + obj.value + "/" + isEnable,
                    type: &#39;get&#39;,
                    success: function (result) {
                        if (result.code == 200) {
                        }
                        else {
                            var d = dialog({
                                title: &#39;提示&#39;,
                                content: &#39;操作失败!&#39;,
                                ok: function () { },
                            });
                            d.show();
                        }
                    }

                });
            });
        });
Copy after login

这时候有人可能留意到了有一个启用的checkbox,其中点击checkbox会发送get请求到控制器。从而完成与后台的交互。那么如果我们想要点击了checkbox按钮之后,我们选中这一条数据的时候不能删除这条数据怎么办呢?

我们就需要遍历一下这个页面的所有checkbox了,如下:

var table = layui.table;
            var checkStatus = table.checkStatus(&#39;PaymentDayList&#39;), data = checkStatus.data;

            if (data.length == 1) {
                var check = document.getElementsByName("lock");
                for (i = 0; i < check.length; i++) {
                    if (check[i].value == data[0].id) {
                        if (check[i].checked) {
                            var d = dialog({
                                title: &#39;提示&#39;,
                                content: "启用了的账期不能修改",
                                okValue: &#39;确定&#39;,
                                ok: function () {
                                }

                            }).width(200);
                            d.show();
                            return;
                        }

                    }

                }
Copy after login

这样就可以确定哪个是选中的了。

更多layui知识请关注layui使用教程栏目。

The above is the detailed content of Some problems with layui uploading files and data tables. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

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)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to set up jump on layui login page How to set up jump on layui login page Apr 04, 2024 am 03:12 AM

Layui login page jump setting steps: Add jump code: Add judgment in the login form submit button click event, and jump to the specified page through window.location.href after successful login. Modify the form configuration: add a hidden input field to the form element of lay-filter="login", with the name "redirect" and the value being the target page address.

How to get form data in layui How to get form data in layui Apr 04, 2024 am 03:39 AM

layui provides a variety of methods for obtaining form data, including directly obtaining all field data of the form, obtaining the value of a single form element, using the formAPI.getVal() method to obtain the specified field value, serializing the form data and using it as an AJAX request parameter, and listening Form submission event gets data.

How layui implements self-adaptation How layui implements self-adaptation Apr 26, 2024 am 03:00 AM

Adaptive layout can be achieved by using the responsive layout function of the layui framework. The steps include: referencing the layui framework. Define an adaptive layout container and set the layui-container class. Use responsive breakpoints (xs/sm/md/lg) to hide elements under specific breakpoints. Specify element width using the grid system (layui-col-). Create spacing via offset (layui-offset-). Use responsive utilities (layui-invisible/show/block/inline) to control the visibility of elements and how they appear.

How to transfer data in layui How to transfer data in layui Apr 26, 2024 am 03:39 AM

The method of using layui to transmit data is as follows: Use Ajax: Create the request object, set the request parameters (URL, method, data), and process the response. Use built-in methods: Simplify data transfer using built-in methods such as $.post, $.get, $.postJSON, or $.getJSON.

What is the difference between layui and vue? What is the difference between layui and vue? Apr 04, 2024 am 03:54 AM

The difference between layui and Vue is mainly reflected in functions and concerns. Layui focuses on rapid development of UI elements and provides prefabricated components to simplify page construction; Vue is a full-stack framework that focuses on data binding, component development and state management, and is more suitable for building complex applications. Layui is easy to learn and suitable for quickly building pages; Vue has a steep learning curve but helps build scalable and easy-to-maintain applications. Depending on the project needs and developer skill level, the appropriate framework can be selected.

How to run layui How to run layui Apr 04, 2024 am 03:42 AM

To run layui, perform the following steps: 1. Import layui script; 2. Initialize layui; 3. Use layui components; 4. Import layui styles (optional); 5. Ensure script compatibility and pay attention to other considerations. With these steps, you can build web applications using the power of layui.

What does layui mean? What does layui mean? Apr 04, 2024 am 04:33 AM

layui is a front-end UI framework that provides a wealth of UI components, tools and functions to help developers quickly build modern, responsive and interactive web applications. Its features include: flexible and lightweight, modular design, rich components, Powerful tools and easy customization. It is widely used in the development of various web applications, including management systems, e-commerce platforms, content management systems, social networks and mobile applications.

What language is layui framework? What language is layui framework? Apr 04, 2024 am 04:39 AM

The layui framework is a JavaScript-based front-end framework that provides a set of easy-to-use UI components and tools to help developers quickly build responsive web applications. Its features include: modular, lightweight, responsive, and has complete documentation and community support. layui is widely used in the development of management backend systems, e-commerce websites, and mobile applications. The advantages are quick start-up, improved efficiency, and easy maintenance. The disadvantages are poor customization and slow technology updates.

See all articles