前端插件-LayUI-----表格的使用(增删查改、搜索、刷新)

效果图

表格参数

数据接口返回参数示例接口:

http://www.layui.com/demo/table/user/?page=1&limit=30

前台页面:

@{
    Layout = null;
}





    
    Index

    @*这里引入Layui Css资源(注意资源路径)*@
    


    
        @*搜索层*@                      按模板名称搜索:                                           
            搜索         
        @*Table表格层*@         @*创建一个table实例   在页面放置一个元素 ,然后通过 table.render() 方法指定该容器,         这里使用的是表格自动渲染     参数:Url:数据接口路径;Page:是否开启分页;id:表格唯一标示*@                                                            ID@*数据字段名称*@                     用户名                     年龄                     性别                     操作                                            
    @*工具栏  在Table中使用 toolbar声明一个 Id  放置在任意位置皆可*@              编辑         删除          @*引入jquery*@     @*/引入Layui的js*@     

后台代码:

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace WebApplication5.Controllers
{
    public class HomeController : Controller
    {
        public static List list = new List()//模拟数据源集合
        {
             new UserInfo
                {
                    UserId=1,
                    UserName="测试1",
                    UserAge=18,
                    UserSex="男"

                },
                new UserInfo
                {
                    UserId=2,
                    UserName="测试2",
                    UserAge=18,
                    UserSex="男"

                },
                new UserInfo
                {
                    UserId=3,
                    UserName="测试3",
                    UserAge=18,
                    UserSex="男"

                },
                new UserInfo
                {
                    UserId=4,
                    UserName="测试4",
                    UserAge=18,
                    UserSex="男"

                },
                new UserInfo
                {
                    UserId=5,
                    UserName="测试5",
                    UserAge=18,
                    UserSex="男"

                },
                new UserInfo
                {
                    UserId=6,
                    UserName="测试6",
                    UserAge=18,
                    UserSex="男"

                },
                new UserInfo
                {
                    UserId=7,
                    UserName="测试7",
                    UserAge=18,
                    UserSex="男"

                },
                new UserInfo
                {
                    UserId=8,
                    UserName="测试8",
                    UserAge=18,
                    UserSex="男"

                },
                new UserInfo
                {
                    UserId=9,
                    UserName="测试9",
                    UserAge=18,
                    UserSex="男"

                },
                new UserInfo
                {
                    UserId=10,
                    UserName="测试10",
                    UserAge=18,
                    UserSex="男"

                },
                new UserInfo
                {
                    UserId=11,
                    UserName="测试11",
                    UserAge=18,
                    UserSex="男"

                },
                new UserInfo
                {
                    UserId=12,
                    UserName="测试12",
                    UserAge=18,
                    UserSex="男"

                },
                new UserInfo
                {
                    UserId=13,
                    UserName="测试13",
                    UserAge=18,
                    UserSex="男"

                },
                new UserInfo
                {
                    UserId=14,
                    UserName="测试14",
                    UserAge=18,
                    UserSex="男"

                },
                new UserInfo
                {
                    UserId=15,
                    UserName="测试15",
                    UserAge=18,
                    UserSex="男"

                },
                new UserInfo
                {
                    UserId=16,
                    UserName="测试16",
                    UserAge=18,
                    UserSex="男"

                },
                new UserInfo
                {
                    UserId=17,
                    UserName="测试17",
                    UserAge=18,
                    UserSex="男"

                },
                new UserInfo
                {
                    UserId=18,
                    UserName="测试18",
                    UserAge=18,
                    UserSex="男"

                },
                new UserInfo
                {
                    UserId=19,
                    UserName="测试19",
                    UserAge=18,
                    UserSex="男"

                },
                new UserInfo
                {
                    UserId=20,
                    UserName="测试21",
                    UserAge=18,
                    UserSex="男"

                }
        };




        public ActionResult Index()
        {
            return View();
        }





        /// 
        /// 将泛型对象序列化为json字符串
        /// 
        /// 类型T
        /// 泛型对象
        /// json字符串
        public static string JsonSerialize(T obj)
        {
            Contract.Requires(obj != null);

            return JsonConvert.SerializeObject(obj);
        }




        /// 
        /// 模糊查询
        /// 
        /// 
        [HttpPost]
        public JsonResult Temp_search(int limit, int page)
        {
            string key = Request["name"].ToString();//接收值
            List listData = list.Where(a=>a.UserName.Contains(key)).ToList(); //查询
            int counts = listData.Count;
            listData = listData.Skip(limit * (page - 1)).Take(limit).ToList();
            return Json(new { code = 0, msg = "", count = counts, data = listData }, JsonRequestBehavior.DenyGet);
        }


        /// 
        /// 初始化数据分页
        /// 
        /// 
        /// 
        /// 
        public string Get_Data(int page,int limit)
        {

            List listData = list;
            int count = listData.Count;//记录总条数
            listData = listData.Skip(limit * (page - 1)).Take(limit).ToList();//MVC分页
            return "{\"code\":0,\"msg\":\"\",\"count\":" + count + ",\"data\":" +JsonSerialize(listData) + "}";//返回数据格式
        }




        /// 
        /// 删除信息
        /// 
        /// 
        [HttpPost]
        public JsonResult DeleteInfoById()
        {
            int id = 0;
            string code = "1";
            string msg = "删除成功!";
            try
            {
                id =int.Parse(Request["id"].ToString());
                UserInfo u = new UserInfo(); ;
                foreach (var item in list)
                {
                    if (item.UserId==id)
                    {
                        u = item;
                        break;
                    }
                }
                list.Remove(u);
            }
            catch (Exception ex)
            {

                msg=ex.Message;
                code = "2";
            }
            return Json(new { code=code,msg=msg},JsonRequestBehavior.DenyGet);
        }
    }
}

码字不易   希望能帮到大家   喜欢的话给个关注吧。

未完待续..........

原文地址:

http://www.zddblog.top/Home/Detail?art_id=Nw==

你可能感兴趣的:(前端插件)