vue+layui制作列表頁


優點:

1.選用layui國產。

2.layui有一套完整的前端框架,基本哪來就可以用。

3.選用vue去掉了很多頁面元素js拼接的繁瑣,及不易修改。

4.vue里面還有一些過濾器等,用起來很方便。

列表頁:

1.用vue數據綁定,加載表格。

2.用layui做分頁處理。

3.用的bootstrap做列表樣式。也可以用layui的一套列表樣式

4.用vue插件axios,做ajax請求。

先上代碼

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <link href="../bootstrap-3.3.7/css/bootstrap.css" rel="stylesheet" />
    <link href="animate.css" rel="stylesheet" />
    <link href="../layui/layui/css/layui.css" rel="stylesheet" />
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <script src="../layui/layui/layui.js"></script>
</head>
<body>
    <div id="app" class="container">
        <table class="table table-bordered ">
            <thead>
                <tr>
                    <td>Id</td>
                    <td>姓名</td>
                    <td>年齡</td>
                    <td>性別</td>
                </tr>
            </thead>
            <tbody>
                <tr class="animated jello" v-for="item in list">
                    <td>{{item.Id}}</td>
                    <td>{{item.Name}}</td>
                    <td>{{item.Age}}</td>
                    <td>{{item.Sex | sex}}</td>
                </tr>
            </tbody>
        </table>
        <div id="laypage"></div>
    </div>
    <script>
        //var total = 0;
        var vm = new Vue({
            el: '#app',
            data: {
                list: [],
                total: -1,
                pageIndex: 1,
                pageSize:2,
            },
            methods: {
                loadList: function () {
                    axios.get('/data.ashx?pageIndex=' + this.pageIndex + '&pageSize=' + this.pageSize).then(result => {
                        console.log(result);
                        this.list = result.data.Data;
                        this.total = result.data.Total;
                        if (this.pageIndex==1) {
                            loadPage();
                        }
                        
                    });
                }
            },
            //鈎子函數:data和methods加載后執行
            created: function () {
                this.loadList();
                //loadPage();
            },
            filters: {
                sex: function (data) {
                    return data ? '男' : '女';
                }
            }
        })
        function loadPage() {
            layui.use(['laypage', 'layer'], function () {
                var laypage = layui.laypage,
                    layer = layui.layer;
                laypage.render({
                    elem: 'laypage',
                    count: vm.total, //數據量
                    limit: vm.pageSize,//每頁限制
                    jump: function (obj, first) { //點擊跳轉函數
                        //obj包含了當前分頁的所有參數,比如:
                        console.log(obj);
                        console.log(first);
                        //首次不執行
                        if (!first) {
                            vm.pageIndex = obj.curr;
                            vm.loadList();

                            //loadData(obj.curr, obj.limit);
                        }
                    }
                });
            });
        }
        
    </script>
</body>
</html>

  

后端請求數據代碼:這里寫的比較簡單,做個演示。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace 前端
{
    /// <summary>
    /// data 的摘要說明
    /// </summary>
    public class data : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string pageIndex = context.Request.QueryString["pageIndex"];
            string pageSize = context.Request.QueryString["pageSize"];
            List<Person> list = new List<Person>();
            list.Add(new Person() { Id=1,Name="張三",Age=23,Sex=1});
            list.Add(new Person() { Id = 2, Name = "斯蒂芬", Age = 23, Sex = 0 });
            list.Add(new Person() { Id = 3, Name = "非公黨委", Age = 29, Sex = 1 });

            var resultList = list.Skip((int.Parse(pageIndex) - 1) * int.Parse(pageSize)).Take(int.Parse(pageSize)).ToList();
            context.Response.ContentType = "text/plain";
            context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(new {
                Total = list.Count,
                Data = resultList
            }));
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        public class Person
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public int Age { get; set; }
            public int Sex { get; set; }
        }
    }
}

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM