• layui表格数据动态实现


    <!DOCTYPE html>
    <html lang="en">

    <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>字典管理</title>
    <link rel="stylesheet" href="../../statics/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../../statics/style/admin.css" media="all">
    </head>

    <body>

    <!-- 主体部分 -->
    <div class="layui-card">
    <div class="layui-card-header">字典管理</div>

    <div class="layui-card-body">

    <div class="layui-form">
    <div class="layui-form-item">
    <div class="layui-inline">
    <label class="layui-form-label">字典名称</label>
    <div class="layui-input-inline">
    <input type="text" id="dictName" name="dictName" placeholder="字典名称" autocomplete="off" class="layui-input">
    </div>
    </div>
    <div class="layui-inline">
    <label class="layui-form-label">字典类型</label>
    <div class="layui-input-inline">
    <input type="text" id="dictType" name="dictType" placeholder="字典类型" autocomplete="off" class="layui-input">
    </div>
    </div>

    <div class="layui-inline">
    <button id="btn_search" class="layui-btn layui-icon layui-icon-search" data-type="search">查询
    </button>
    </div>

    </div>

    </div>

    <table id="manage_table" lay-filter="manage_table"></table>
    </div>
    </div>

    <script type="text/html" id="tableToolBar">
    <div class="layui-btn-container">
    <button class="layui-btn layui-icon layui-icon-add-1" lay-event="add">新增</button>
    </div>
    </script>

    <!-- 表格基本操作按钮 -->
    <script type="text/html" id="table_operation">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </script>
    <!--其他操作-->
    <script type="text/html" id="other_operation">

    </script>

    <!-- 添加-编辑 弹出框模板 -->
    <!-- 新增,编辑表单 -->
    <script type="text/html" id="add_edit_layer">

    <form class="layui-form" lay-filter="layer_form" id="layer_form" action="" style=" 95%; margin: 10px auto;">

    <div class="layui-form-item">
    <label class="layui-form-label"><span style="color: red;">*</span>&nbsp;字典名称:</label>
    <div class="layui-input-block">
    <input type="text" name="name" lay-verify="required" lay-verType="tips"
    placeholder="请输入字典名称" autocomplete="off" class="layui-input">
    </div>
    </div>

    <div class="layui-form-item">
    <label class="layui-form-label"><span style="color: red;">*</span>&nbsp;字典类型:</label>
    <div class="layui-input-block">
    <input type="text" name="type" lay-verify="required" lay-verType="tips"
    placeholder="请输入字典类型" autocomplete="off" class="layui-input">
    </div>
    </div>

    <div class="layui-form-item">
    <label class="layui-form-label"><span style="color: red;">*</span>&nbsp;字典码:</label>
    <div class="layui-input-block">
    <input type="text" name="code" lay-verify="required" lay-verType="tips"
    placeholder="请输入字典码" autocomplete="off" class="layui-input">
    </div>
    </div>

    <div class="layui-form-item">
    <label class="layui-form-label"><span style="color: red;">*</span>&nbsp;字典值:</label>
    <div class="layui-input-block">
    <input type="text" name="value" lay-verify="required" lay-verType="tips"
    placeholder="请输入字典值" autocomplete="off" class="layui-input">
    </div>
    </div>

    <div class="layui-form-item">
    <label class="layui-form-label">排序号:</label>
    <div class="layui-input-block">
    <input type="number" name="orderNum" lay-verify="number"
    placeholder="请输入排序号" autocomplete="off" class="layui-input">
    </div>
    </div>

    <div class="layui-form-item">
    <label class="layui-form-label">备注:</label>
    <div class="layui-input-block">
    <input type="text" name="remark"
    placeholder="请输入备注" autocomplete="off" class="layui-input">
    </div>
    </div>



    <div class="layui-form-item" id="form_btn">
    <div class="layui-input-block">
    <button class="layui-btn" lay-submit lay-filter="post_form">保存</button>
    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div>
    </div>
    </form>
    </script>

    <!-- javascript 脚本 -->
    <script src="../../statics/layui/layui.js"></script>
    <script>
    layui.config({
    base: '../../statics/' //静态资源所在路径
    }).extend({
    index: 'lib/index' //主入口模块
    }).use(['index', 'table'], function () {
    var table = layui.table;

    var admin = layui.admin,
    $ = layui.jquery,
    table = layui.table,
    form = layui.form,
    layer = layui.layer;

    var currentRow = {},
    currentOpt = "",
    saveUrl = "/sys/dict/save",
    updateUrl = "/sys/dict/update",
    deleteUrl = "/sys/dict/delete",
    listUrl = "/sys/dict/list",
    deleteId = "id",
    layerIndex = 0;

    $(function () {
    // 查询按钮
    $("#btn_search").on("click", function () {
    table.reload('manage_table', {
    where: {
    dictName: $("#dictName").val(),
    dictType: $("#dictType").val()
    }
    , page: {
    curr: 1 //重新从第 1 页开始
    }
    });
    });

    initTable();
    admin.resize(function () {
    table.resize("manage_table");
    });
    });

    function initTable() {
    // 初始化列表(表格)
    table.render({
    elem: '#manage_table'
    , url: listUrl //数据接口
    , method: "POST"
    , page: true
    , toolbar: '#tableToolBar'
    , defaultToolbar: []
    , cellMinWidth: 100
    , cols: [
    [ //表头
    {field: 'name', title: '字典名称', align: 'center'}
    , {field: 'type', title: '字典类型', align: 'center'}
    , {field: 'code', title: '字典码', align: 'center'}
    , {field: 'value', title: '字典值', align: 'center'}
    , {field: 'orderNum', title: '排序号', align: 'center'}
    , {field: 'remark', title: '备注', align: 'center'}
    , {
    165,
    align: 'center',
    title: '操作',
    toolbar: '#table_operation',
    fixed: 'right'
    }
    ]
    ],
    parseData: function (res) { //res 即为原始返回的数据
    return {
    "code": res.code == 200 ? 0 : res.code, //解析接口状态
    "msg": res.code == 200 ? "" : res.msg, //解析提示文本
    "count": res.page ? res.page.totalCount : 0, //解析数据长度
    "data": res.page ? res.page.list: {} //解析数据列表
    };
    }
    });

    // 绑定表格bar 按钮事件
    table.on('tool(manage_table)', function (obj) {

    if (obj.event === 'edit') {
    currentRow = obj.data;
    currentOpt = updateUrl;
    layerIndex = layer.open({
    title: "修改",
    type: 1,
    area: admin.screen() < 2 ? ['80%', '300px'] : ['500px', '450px'],
    shadeClose: true,
    content: document.querySelector("#add_edit_layer").innerHTML
    });
    form.val('layer_form', currentRow);
    form.render();

    } else if (obj.event === 'del') {
    layer.confirm('确认删除该行吗?', function (index) {
    admin.req({
    url: deleteUrl + "?ids=" + obj.data[deleteId],
    type: 'GET',
    done: function() {
    layer.msg('删除成功');
    table.reload('manage_table');
    }
    });
    layer.close(index)
    })
    }
    });

    //监听新增事件
    table.on('toolbar(manage_table)', function(obj){
    if(obj.event == 'add') {
    currentRow = {};
    currentOpt = saveUrl;
    layerIndex = layer.open({
    title: '新增',
    type: 1,
    area: admin.screen() < 2 ? ['80%', '300px'] : ['500px', '450px'],
    shadeClose: true,
    content: document.querySelector("#add_edit_layer").innerHTML
    });
    document.getElementById("layer_form").reset();
    form.render();
    }

    });

    }

    // 保存按钮事件
    form.on('submit(post_form)', function (data) {
    var postData = Object.assign(currentRow, data.field);
    for (var i in postData) { // 清除值为null的属性
    if (postData[i] === null) {
    delete postData[i];
    }
    }

    admin.req({
    url: currentOpt,
    type: 'POST',
    data: JSON.stringify(postData),
    contentType: 'application/json;charset=utf-8',
    dataType: 'json',
    done: function() {
    table.reload('manage_table');
    layer.msg('操作成功');
    layer.close(layerIndex);
    }
    });
    return false;
    });
    });
    </script>

    </body>

    </html>

    主要注意一下引进去的js跟css

    还有自己写得接口调用以及数据渲染的时候的值。

  • 相关阅读:
    基于微软平台IIS/ASP.NET开发的大型网站
    谈谈用ASP.NET开发的大型网站有哪些架构方式(成本)
    微软Expression Blend基础教程系列
    Silverlight类库介绍FJCore
    Blend基础数据绑定
    Windows Embedded 版 Silverlight “Cashmere”
    Blend基础动画
    Blend基础控件与用户控件
    Silverlight 4 新特性归纳整理
    SQL经典短小代码收集
  • 原文地址:https://www.cnblogs.com/easyjie/p/14666834.html
Copyright © 2020-2023  润新知