Skip to content

Commit 92e722c

Browse files
committed
优化权限角色组列表
1 parent 82386cc commit 92e722c

File tree

4 files changed

+58
-38
lines changed

4 files changed

+58
-38
lines changed

application/admin/controller/auth/Group.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,10 @@ public function roletree()
258258
}
259259
if (($pid || $parentGroupModel) && (!$id || $currentGroupModel)) {
260260
$id = $id ? $id : null;
261-
$ruleList = collection(model('AuthRule')->order('weigh', 'desc')->order('id', 'asc')->select())->toArray();
261+
$ruleList = Db::name("auth_rule")
262+
->field('id,pid,name,title,icon,ismenu,status,weigh')
263+
->order('weigh DESC,id ASC')
264+
->select();
262265
//读取父类角色所有节点列表
263266
$parentRuleList = [];
264267
if (in_array('*', explode(',', $parentGroupModel->rules))) {

application/admin/view/auth/group/add.html

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<form id="add-form" class="form-horizontal form-ajax" role="form" data-toggle="validator" method="POST" action="">
22
{:token()}
3-
<input type="hidden" name="row[rules]" value="" />
3+
<input type="hidden" name="row[rules]" value=""/>
44
<div class="form-group">
55
<label class="control-label col-xs-12 col-sm-2">{:__('Parent')}:</label>
66
<div class="col-xs-12 col-sm-8">
@@ -10,14 +10,16 @@
1010
<div class="form-group">
1111
<label class="control-label col-xs-12 col-sm-2">{:__('Name')}:</label>
1212
<div class="col-xs-12 col-sm-8">
13-
<input type="text" class="form-control" id="name" name="row[name]" value="" data-rule="required" />
13+
<input type="text" class="form-control" id="name" name="row[name]" value="" data-rule="required"/>
1414
</div>
1515
</div>
1616
<div class="form-group">
1717
<label class="control-label col-xs-12 col-sm-2">{:__('Permission')}:</label>
1818
<div class="col-xs-12 col-sm-8">
19-
<span class="text-muted"><input type="checkbox" name="" id="checkall" /> <label for="checkall"><span>{:__('Check all')}</span></label></span>
20-
<span class="text-muted"><input type="checkbox" name="" id="expandall" /> <label for="expandall"><span>{:__('Expand all')}</span></label></span>
19+
<div style="margin-top:8px;">
20+
<span class="text-muted"><input type="checkbox" name="" id="checkall"/> <label for="checkall"><span>{:__('Check all')}</span></label></span>
21+
<span class="text-muted"><input type="checkbox" name="" id="expandall"/> <label for="expandall"><span>{:__('Expand all')}</span></label></span>
22+
</div>
2123

2224
<div id="treeview"></div>
2325
</div>

application/admin/view/auth/group/edit.html

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<form id="edit-form" class="form-horizontal form-ajax" role="form" method="POST" action="">
22
{:token()}
3-
<input type="hidden" name="row[rules]" value="" />
3+
<input type="hidden" name="row[rules]" value=""/>
44
<div class="form-group">
55
<label class="control-label col-xs-12 col-sm-2">{:__('Parent')}:</label>
66
<div class="col-xs-12 col-sm-8">
@@ -10,14 +10,16 @@
1010
<div class="form-group">
1111
<label class="control-label col-xs-12 col-sm-2">{:__('Name')}:</label>
1212
<div class="col-xs-12 col-sm-8">
13-
<input type="text" class="form-control" id="name" name="row[name]" value="{$row.name|htmlentities}" data-rule="required" />
13+
<input type="text" class="form-control" id="name" name="row[name]" value="{$row.name|htmlentities}" data-rule="required"/>
1414
</div>
1515
</div>
1616
<div class="form-group">
1717
<label class="control-label col-xs-12 col-sm-2">{:__('Permission')}:</label>
1818
<div class="col-xs-12 col-sm-8">
19-
<span class="text-muted"><input type="checkbox" name="" id="checkall" /> <label for="checkall"><span>{:__('Check all')}</span></label></span>
20-
<span class="text-muted"><input type="checkbox" name="" id="expandall" /> <label for="expandall"><span>{:__('Expand all')}</span></label></span>
19+
<div style="margin-top:8px;">
20+
<span class="text-muted"><input type="checkbox" name="" id="checkall"/> <label for="checkall"><span>{:__('Check all')}</span></label></span>
21+
<span class="text-muted"><input type="checkbox" name="" id="expandall"/> <label for="expandall"><span>{:__('Expand all')}</span></label></span>
22+
</div>
2123

2224
<div id="treeview"></div>
2325
</div>

public/assets/js/backend/auth/group.js

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'jstree'], function (
4747
{field: 'state', checkbox: true,},
4848
{field: 'id', title: 'ID'},
4949
{field: 'pid', title: __('Parent')},
50-
{field: 'name', title: __('Name'), align: 'left', formatter:function (value, row, index) {
50+
{
51+
field: 'name', title: __('Name'), align: 'left', formatter: function (value, row, index) {
5152
return value.toString().replace(/(&|&amp;)nbsp;/g, '&nbsp;');
5253
}
5354
},
@@ -79,14 +80,17 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'jstree'], function (
7980
},
8081
api: {
8182
bindevent: function () {
83+
var treeview = $("#treeview");
84+
85+
// 表单提交前设定规则集合
8286
Form.api.bindevent($("form[role=form]"), null, null, function () {
83-
if ($("#treeview").length > 0) {
84-
var r = $("#treeview").jstree("get_all_checked");
87+
if (treeview.length > 0) {
88+
var r = treeview.jstree("get_all_checked");
8589
$("input[name='row[rules]']").val(r.join(','));
8690
}
8791
return true;
8892
});
89-
//渲染权限节点树
93+
9094
//变更级别后需要重建节点树
9195
$(document).on("change", "select[name='row[pid]']", function () {
9296
var pid = $(this).data("pid");
@@ -96,37 +100,21 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'jstree'], function (
96100
Backend.api.toastr.error(__('Can not change the parent to self'));
97101
return false;
98102
}
99-
$.ajax({
100-
url: "auth/group/roletree",
101-
type: 'post',
102-
dataType: 'json',
103-
data: {id: id, pid: $(this).val()},
104-
success: function (ret) {
105-
if (ret.hasOwnProperty("code")) {
106-
var data = ret.hasOwnProperty("data") && ret.data != "" ? ret.data : "";
107-
if (ret.code === 1) {
108-
//销毁已有的节点树
109-
$("#treeview").jstree("destroy");
110-
Controller.api.rendertree(data);
111-
} else {
112-
Backend.api.toastr.error(ret.msg);
113-
}
114-
}
115-
}, error: function (e) {
116-
Backend.api.toastr.error(e.message);
117-
}
118-
});
103+
treeview.jstree(true).refresh(false);
119104
});
105+
120106
//全选和展开
121107
$(document).on("click", "#checkall", function () {
122-
$("#treeview").jstree($(this).prop("checked") ? "check_all" : "uncheck_all");
108+
treeview.jstree($(this).prop("checked") ? "check_all" : "uncheck_all");
123109
});
124110
$(document).on("click", "#expandall", function () {
125-
$("#treeview").jstree($(this).prop("checked") ? "open_all" : "close_all");
111+
treeview.jstree($(this).prop("checked") ? "open_all" : "close_all");
126112
});
127-
$("select[name='row[pid]']").trigger("change");
113+
114+
//首次渲染
115+
Controller.api.rendertree();
128116
},
129-
rendertree: function (content) {
117+
rendertree: function () {
130118
$("#treeview")
131119
.on('redraw.jstree', function (e) {
132120
$(".layer-footer").attr("domrefresh", Math.random());
@@ -150,7 +138,32 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'jstree'], function (
150138
"plugins": ["checkbox", "types"],
151139
"core": {
152140
'check_callback': true,
153-
"data": content
141+
'strings': {
142+
'Loading ...': __('Loading')
143+
},
144+
"data": function (obj, callback) {
145+
var pidObj = $("select[name='row[pid]']");
146+
$.ajax({
147+
url: "auth/group/roletree",
148+
type: 'post',
149+
dataType: 'json',
150+
data: {id: pidObj.data('id'), pid: pidObj.val()},
151+
success: function (ret) {
152+
if (ret.hasOwnProperty("code")) {
153+
var data = ret.hasOwnProperty("data") && ret.data != "" ? ret.data : "";
154+
if (ret.code === 1) {
155+
callback(data);
156+
} else {
157+
Backend.api.toastr.error(ret.msg);
158+
callback([]);
159+
}
160+
}
161+
}, error: function (e) {
162+
Backend.api.toastr.error(e.message);
163+
callback([]);
164+
}
165+
});
166+
}
154167
}
155168
});
156169
}

0 commit comments

Comments
 (0)