search
HomeWeb Front-endLayui TutorialSome problems with layui uploading files and data tables

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');
                }
            });
        });
    }

当然你需要在你的页面上定义一个按钮,然后触发点击事件,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("上传模板失败!");
            }
        }

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

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

在表格中显示和下载。

第二就是表格的问题了:

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();
                        }
                    }

                });
            });
        });

这时候有人可能留意到了有一个启用的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;
                        }

                    }

                }

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

更多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
This article is reproduced at:博客园. If there is any infringement, please contact admin@php.cn delete

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.