jqery easyui 利用treeview實現選單的增刪改查
阿新 • • 發佈:2019-02-07
$(function () {
//function onEndEdit(index, row) {
// debugger;
// var ed = $(this).datagrid('getEditor', {
// index: index,
// field: 'IsEnable'
// });
// debugger;
// row.IsEnable = $(ed.target).checkbox('getChecked');
// row.IsOpen = true;
// //row.productname = $(ed.target).combobox('getText');
//};
$('#tg').treegrid({
url: '/Menu/GetNavgitionData',
idField: 'BaseId',
treeField: 'Name',
iconCls: 'icon-ok',
state: 'closed',
rownumbers: true , //行號
animate: true, //點選節點的動畫效果
collapsible: true,//定義是否可摺疊按鈕
fitColumns: true, // 置為true將自動使列適應表格寬度以防止出現水平滾動。
showFooter: true,//定義是否顯示行底(如果是做統計表格,這裡可以顯示總計等)。
striped: false,//設定為true將交替顯示行背景。
onDblClickRow: function (row) { edit() },//雙擊節點是觸發
//onClickRow: function (row) { save() },//點選節點時觸發
columns: [[
{ field: 'Name', title: '模組名稱', editor: 'text', width: 180 },
{ field: 'Domain', title: '導航領域', editor: 'text', width: 150 },
{ field: 'Path', title: '跳轉路徑', editor: 'text', width: 200 },
{ field: 'Sequence', title: '排序序號', editor: 'text', width: 100 },
{
field: 'Parent', title: '上級選單', width: 100,
formatter: function (value, row, index) {
if (value != null)
return value.Name;
},
{
field: 'IsEnable', title: '是否啟用', width: 60,
formatter: function (value, row, index) {
if (value == true || value == 'true') {
var d = '<input type="checkbox" checked="checked" class="zhen" disabled="disabled" /> ';
row.IsEnable = 'true'; //此處如果不裝換為字串型別,下面的編輯器將不會有預設值
}
else {
var d = '<input type="checkbox" disabled="disabled" /> ';
row.IsEnable = 'false';
}
return d;
},
editor: { //編輯器
type: 'checkbox',
options: {
on: true, off: false, //此出實際返回字串型別
}
}
},
{
field: 'IsOpen', title: '是否公開', width: 60,
formatter: function (value, row, index) {
//alert(rec.isPresent);
if (value == true) {
var d = '<input type="checkbox" checked="checked" name="IsOpen"/> ';
row.IsOpen = 'true';
}
else {
var d = '<input type="checkbox" name="IsOpen"/> ';
row.IsOpen = 'false';
}
return d;
},
editor: { //編輯器
type: 'checkbox',
options: {
on: true, off: false, //此出實際返回字串型別
}
}
}
]],
toolbar: [
{
text: '新增根選單', iconCls: 'icon-add', handler: function () {
appendroot();
}
}, '-',
{
text: '新增子選單', iconCls: 'icon-add', handler: function () {
append();
}
}, '-',
{
text: '刪除', iconCls: 'icon-remove', handler: function () {
remove();
}
}, '-',
{
text: '編輯', iconCls: 'icon-edit', handler: function () {
edit();
}
}, '-',
{
text: '儲存', iconCls: 'icon-save', handler: function () {
save();
}
}, '-',
{
text: '取消編輯', iconCls: 'icon-undo', handler: function () {
$('#tg').treegrid('cancelEdit', editingId);
editingId = undefined;
}
}, '-',
{
text: '全部摺疊', iconCls: 'icon-fold', handler: function () {
$('#tg').treegrid('collapseAll');
}
}, '-',
{
text: '全部展開', iconCls: 'icon-unfold', handler: function () {
$('#tg').treegrid('expandAll');
}
},
],
}
);
})
var editingId;
function IsEditingId() {
if (editingId) {
$('#tg').treegrid('endEdit', editingId);
editingId = undefined;
}
}
function edit() {
debugger;
if (undefined != editingId) {
utils.message('error', '請先儲存當前節點!');
return;
}
var row = $('#tg').treegrid('getSelected');
if (undefined == row) {
utils.message('error', '請選中一個節點!');
}
if (row) {
editingId = row.BaseId;
$('#tg').treegrid('beginEdit', editingId);
}
}
function save() {
if (editingId != undefined) {
editId = editingId;
$('#tg').treegrid('endEdit', editingId);
utils.message('confirm', '確認儲存當前操作嗎?', function (b) {
if (b) {
var row = $('#tg').treegrid('find', editingId);
if (row.IsEnable == 'false') {
row.IsEnable == false
}
if (row.IsEnable == 'true') {
row.IsEnable = true
}
if (row.IsOpen == 'true') {
row.IsOpen = true;
}
if (row.IsOpen == 'false') {
row.IsOpen = false;
}
var node = {
BaseId: row.BaseId, Domain: row.Domain, Name: row.Name, Path: row.Path, ParentId: row._parentId, Sequence: row.Sequence, IsEnable: row.IsEnable, IsOpen: row.IsOpen
};
var data;
$.ajax({
type: "post",
async: false,
data: node,
url: '/Menu/SaveNewNode',
success: function (res) {
data = res;
}
});
if (data == 'success') {
utils.message('success', '儲存成功!');
$('#tg').treegrid('refresh', editingId);
$('#tg').treegrid('acceptChanges');
}
else {
utils.message('error', data);
$('#tg').treegrid('rejectChanges');
$('#tg').treegrid('refresh', editingId);
//$('#tg').treegrid('remove', editingId);
// $('#tg').treegrid('cancelEdit', editingId);
//$('#tg').treegrid('rejectChanges');
editingId = undefined;
return;
}
editingId = undefined;
}
}
)
}
}
function appendroot() {
debugger;
$('#tg').treegrid('append', {
parent: null, // 節點有一個'id'值,定義是通過'idField'屬性
data: [{
BaseId: guid(),
Name: '',
Path: '',
Parent: null,
IsEnable: false,
IsOpen: false,
}]
});
//$('#tg').treegrid('expand', node.BaseId);
$('#tg').treegrid('select', editingId);
$('#tg').treegrid('beginEdit', editingId);
}
function append() {
if (undefined != editingId) {
utils.message('error', '請先儲存當前節點!');
return;
}
var node = $('#tg').treegrid('getSelected');
if (!node) {
utils.message('error', '請選擇節點!');
return;
}
$('#tg').treegrid('append', {
parent: node.BaseId, // 節點有一個'id'值,定義是通過'idField'屬性
data: [{
BaseId: guid(),
Name: '',
Path: '',
Parent: node,
IsEnable: false,
IsOpen: false,
}]
});
$('#tg').treegrid('expand', node.BaseId);
$('#tg').treegrid('select', editingId);
$('#tg').treegrid('beginEdit', editingId);
}
function remove() {
var node = $('#tg').treegrid('getSelected');
if (undefined != editingId) {
if (editingId != node.BaseId) {
utils.message('error', '請先儲存當前節點!');
return;
}
}
if (null == node) {
utils.message('error', '請選擇要刪除的節點!');
return;
}
var id = node.BaseId;
utils.message('confirm', '確認刪除嗎?', function (b) {
if (b) {
$('#tg').treegrid('endEdit', id);
$.post('/Menu/DeleteNode', { id: id }, function (data) {
if (data == 'error') {
utils.message('error', '刪除失敗!');
$('#tg').treegrid('rejectChanges');
}
if (data == 'success') {
$('#tg').treegrid('remove', node.BaseId);
utils.message('success', '刪除成功!');
editingId = undefined;
// $('#tg').treegrid('reload', id);
$('#tg').treegrid('acceptChanges');
}
});
}
});
}
//表示全域性唯一識別符號 (GUID)。 因為操作選單需要id 所以要自己生成id 來儲存到資料庫
function guid() {
function S4() {
return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
}
editingId = (S4() + S4() + S4() + S4() + S4() + S4() + S4() + S4());
return editingId;
}