移动系统liao
2024-05-09 5d6cb15ac86d9174393cb9d1538d69b567e2c26c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<title>区域设置</title>
<!--当前位置开始-->
<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <script type="text/html" template lay-done="layui.data.updateMainBreadcrumb();">
        </script>
    </div>
</div>
 
<style>
    /* 左树 */
    #areaTreeBar { padding: 10px 15px; border: 1px solid #e6e6e6; background-color: #f2f2f2; }
    #areaTree { border: 1px solid #e6e6e6; padding: 10px 5px; overflow: auto; height: -webkit-calc(100vh - 158px); height: -moz-calc(100vh - 158px); height: calc(100vh - 158px); }
    .layui-tree-entry .layui-tree-txt { padding: 0 5px; border: 1px transparent solid; text-decoration: none !important; }
    .layui-tree-entry.ew-tree-click .layui-tree-txt { background-color: #fff3e0; border: 1px #FFE6B0 solid; }
    /* 右表搜索表单 */
    #areaTbSearchForm .layui-form-label { box-sizing: border-box !important; width: 90px !important; }
    #areaTbSearchForm .layui-input-block { margin-left: 90px !important; }
    /* 主管标识 */
    td .leader-flag { background: #52c41a; color: #fff; font-size: 12px; line-height: 18px; position: absolute; padding: 0 15px; right: -15px; top: 2px; -webkit-transform: rotate(45deg); transform: rotate(45deg); -webkit-transform-origin: center; transform-origin: center; }
</style>
<!-- 正文开始 -->
<script type="text/html" template lay-type="Post" lay-url="{{ layui.setter.apiUrl }}Api/CoreCmsArea/GetIndex" lay-done="layui.data.done(d);">
 
    <div class="layui-fluid" style="padding-bottom: 0;">
        <div class="layui-row layui-col-space15">
            <div class="layui-col-md2">
                <div class="layui-card">
                    <div class="layui-card-body" style="padding: 10px;">
                        <!-- 左树 -->
                        <div id="areaTree" class="ztree"></div>
                    </div>
                </div>
            </div>
            <div class="layui-col-md10">
                <div class="layui-card">
                    <div class="layui-card-body" style="padding: 10px;">
                        <!-- 数据表格 -->
                        <table id="areaTable" lay-filter="areaTable"></table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</script>
 
<!-- 表格操作列 -->
<script type="text/html" id="areaTbBar">
    <!--{{#  if(d.depth <3 ){ }}
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="add">添加下级</a>
    {{#  } }}-->
    <a class="layui-btn layui-btn-primary 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="LAY-app-CoreCmsArea-toolbar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="addData"><i class="layui-icon layui-icon-add-1"></i>添加下级</button>
    </div>
</script>
 
 
<script>
    var indexData;
    var debug = layui.setter.debug;
    var checkId;
    layui.data.done = function (d) {
        //开启调试情况下获取接口赋值数据
        if (debug) { console.log(d); }
 
        indexData = d.data;
        layui.use(['index', 'table', 'laydate', 'util', 'tree', 'dropdown', 'tableX', 'coreHelper', 'zTree'],
            function () {
                var $ = layui.$
                    , admin = layui.admin
                    , table = layui.table
                    , form = layui.form
                    , laydate = layui.laydate
                    , setter = layui.setter
                    , coreHelper = layui.coreHelper
                    , util = layui.util
                    , tableX = layui.tableX
                    , view = layui.view;
                var tree = layui.tree;
                var selObj, treeData;  // 左树选中数据
 
                /* 渲染树形 */
                function renderTree() {
                    coreHelper.Post('Api/CoreCmsArea/GetAllList', null, function (res) {
                        if (res.code === 0) {
                            $.fn.zTree.init($('#areaTree'), {
                                check: { enable: false },
                                callback: {
                                    onClick: function (event, treeId, treeNode) {
                                        checkId = treeNode.id;
                                        insTb.reload({
                                            where: { parentId: treeNode.id },
                                            url: layui.setter.apiUrl + 'Api/CoreCmsArea/GetPageList',
                                            method: 'post'
                                        });
                                    },
                                },
                                data: {
                                    key: { name: "name" },
                                    simpleData: {
                                        enable: true,
                                        idKey: "id",
                                        pIdKey: "parentId",
                                        rootPId: 0
                                    }
                                }
                            }, res.data);
                        }
                        else {
                            return layer.msg(res.msg, { icon: 2 });
                        }
                        //$('#areaTree').find('.layui-tree-entry:first>.layui-tree-main>.layui-tree-txt').trigger('click');
                    });
                }
                renderTree();
 
 
                /* 渲染表格 */
                var insTb = tableX.render({
                    elem: '#areaTable',
                    data: [],
                    toolbar: '#LAY-app-CoreCmsArea-toolbar',
                    height: 'full-135',
                    page: true,
                    limit: 20,
                    cellMinWidth: 100,
                    cols: [[
                        { type: 'numbers', fixed: 'left' },
                        { field: 'id', title: '地区ID', width: 80, sort: false },
                        { field: 'parentId', title: '父级ID', sort: false },
                        { field: 'depth', title: '地区深度', sort: false },
                        { field: 'name', title: '地区名称', sort: false },
                        { field: 'sort', title: '地区排序', sort: false },
                        {
                            title: '操作', toolbar: '#areaTbBar', unresize: true,
                            align: 'center', fixed: 'right', width: 100
                        }
                    ]],
                    done: function () {
                        var $flag = $('#areaTable+div .leader-flag');
                        $flag.parent().parent().append($flag);
                    }
                });
 
                //监听工具条
                table.on('tool(areaTable)', function (obj) {
                    if (obj.event === 'del') {
                        doDelete(obj);
                    } else if (obj.event === 'edit') {
                        doEdit(obj)
                    }
                });
 
                //头工具栏事件
                table.on('toolbar(areaTable)', function (obj) {
                    var checkStatus = table.checkStatus(obj.config.id);
                    switch (obj.event) {
                    case 'addData':
                        doCreate(checkId);
                        break;
                    };
                });
 
                //执行创建操作
                function doCreate(checkId) {
                    coreHelper.Post("Api/CoreCmsArea/GetCreate", { id: checkId }, function (e) {
                        if (e.code === 0) {
                            admin.popup({
                                shadeClose: false,
                                title: '创建数据',
                                area: ['350px', '350px'],
                                id: 'LAY-popup-CoreCmsArea-create',
                                success: function (layero, index) {
                                    view(this.id).render('shop/area/create', { data: e.data }).done(function () {
                                        //监听提交
                                        form.on('submit(LAY-app-CoreCmsArea-createForm-submit)',
                                            function (data) {
                                                var field = data.field; //获取提交的字段
 
                                                if (debug) { console.log(field); } //开启调试返回数据
                                                //提交 Ajax 成功后,关闭当前弹层并重载表格
                                                coreHelper.Post("Api/CoreCmsArea/DoCreate", field, function (e) {
                                                    console.log(e)
                                                    if (e.code === 0) {
                                                        renderTree();
                                                        //layui.table.reloadData('areaTable'); //重载表格
                                                        layer.close(index); //再执行关闭
                                                        layer.msg(e.msg);
                                                    } else {
                                                        layer.msg(e.msg);
                                                    }
                                                });
                                            });
                                    });
                                }
                                , btn: ['确定', '取消']
                                , yes: function (index, layero) {
                                    layero.contents().find("#LAY-app-CoreCmsArea-createForm-submit").click();
                                }
                            });
                        } else {
                            layer.msg(e.msg);
                        }
                    });
                }
                //执行编辑操作
                function doEdit(obj) {
                    coreHelper.Post("Api/CoreCmsArea/GetEdit", { id: obj.data.id }, function (e) {
                        if (e.code === 0) {
                            admin.popup({
                                shadeClose: false,
                                title: '编辑数据',
                                area: ['350px', '350px'],
                                id: 'LAY-popup-CoreCmsArea-edit',
                                success: function (layero, index) {
                                    view(this.id).render('shop/area/edit', { data: e.data }).done(function () {
                                        //监听提交
                                        form.on('submit(LAY-app-CoreCmsArea-editForm-submit)',
                                            function (data) {
                                                var field = data.field; //获取提交的字段
 
                                                if (debug) { console.log(field); } //开启调试返回数据
                                                //提交 Ajax 成功后,关闭当前弹层并重载表格
                                                coreHelper.Post("Api/CoreCmsArea/DoEdit", field, function (e) {
                                                    console.log(e)
                                                    if (e.code === 0) {
                                                        renderTree();
                                                        //layui.table.reloadData('areaTable'); //重载表格
                                                        layer.close(index); //再执行关闭
                                                        layer.msg(e.msg);
                                                    } else {
                                                        layer.msg(e.msg);
                                                    }
                                                });
                                            });
                                    })
                                }
                                , btn: ['确定', '取消']
                                , yes: function (index, layero) {
                                    layero.contents().find("#LAY-app-CoreCmsArea-editForm-submit").click();
                                }
                            });
                        } else {
                            layer.msg(e.msg);
                        }
                    });
                }
                //执行单个删除
                function doDelete(obj) {
                    layer.confirm('确定删除吗?删除后将无法恢复。', function (index) {
                        coreHelper.Post("Api/CoreCmsArea/DoDelete", { id: obj.data.id }, function (e) {
                            if (debug) { console.log(e); } //开启调试返回数据
                            renderTree();
                            //table.reloadData('areaTable');
                            layer.msg(e.msg);
                        });
                    });
                }
 
            });
    };
</script>