2019-03-20 12:15:02 +08:00
|
|
|
(function ($) {
|
2018-06-07 00:45:47 +08:00
|
|
|
DataEntity = function (options) {
|
2018-09-02 00:50:13 +08:00
|
|
|
this.options = options;
|
2018-06-07 00:45:47 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
DataEntity.prototype = {
|
|
|
|
load: function (value) {
|
2018-09-02 00:50:13 +08:00
|
|
|
for (name in this.options) {
|
|
|
|
var ctl = $(this.options[name]);
|
|
|
|
if (ctl.attr('data-toggle') === "dropdown") {
|
2018-08-05 15:31:21 +08:00
|
|
|
ctl.val(value[name]).dropdown('val');
|
2018-06-07 00:45:47 +08:00
|
|
|
}
|
2018-09-02 00:50:13 +08:00
|
|
|
else if (ctl.attr('data-toggle') === 'toggle') {
|
2018-06-07 00:45:47 +08:00
|
|
|
ctl.bootstrapToggle(value[name] ? 'on' : 'off');
|
|
|
|
}
|
2018-08-05 01:19:34 +08:00
|
|
|
else ctl.val(value[name]);
|
2018-06-07 00:45:47 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
reset: function () {
|
2018-09-02 00:50:13 +08:00
|
|
|
for (name in this.options) {
|
|
|
|
var ctl = $(this.options[name]);
|
2018-06-07 00:45:47 +08:00
|
|
|
var dv = ctl.attr("data-default-val");
|
|
|
|
if (dv === undefined) dv = "";
|
2018-09-02 00:50:13 +08:00
|
|
|
if (ctl.attr('data-toggle') === "dropdown") {
|
2018-08-05 15:31:21 +08:00
|
|
|
ctl.val(dv).dropdown('val');
|
2018-06-07 00:45:47 +08:00
|
|
|
}
|
2018-09-02 00:50:13 +08:00
|
|
|
else if (ctl.attr('data-toggle') === 'toggle') {
|
|
|
|
ctl.bootstrapToggle(dv === "true" ? 'on' : 'off');
|
2018-06-07 00:45:47 +08:00
|
|
|
}
|
2018-08-05 01:19:34 +08:00
|
|
|
else ctl.val(dv);
|
2018-06-07 00:45:47 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
get: function () {
|
|
|
|
var target = {};
|
2018-09-02 00:50:13 +08:00
|
|
|
for (name in this.options) {
|
|
|
|
var ctl = $(this.options[name]);
|
2018-06-07 00:45:47 +08:00
|
|
|
var dv = ctl.attr('data-default-val');
|
2018-09-02 00:50:13 +08:00
|
|
|
if (ctl.attr('data-toggle') === 'toggle') {
|
2018-06-07 00:45:47 +08:00
|
|
|
target[name] = ctl.prop('checked');
|
|
|
|
continue;
|
|
|
|
}
|
2018-09-02 00:50:13 +08:00
|
|
|
else if (dv !== undefined && ctl.val() === "") target[name] = dv;
|
2018-06-07 00:45:47 +08:00
|
|
|
else target[name] = ctl.val();
|
2018-09-02 00:50:13 +08:00
|
|
|
if (target[name] === "true" || target[name] === "True") target[name] = true;
|
|
|
|
if (target[name] === "false" || target[name] === "False") target[name] = false;
|
2018-06-07 00:45:47 +08:00
|
|
|
}
|
|
|
|
return target;
|
|
|
|
}
|
2018-09-02 00:50:13 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
DataTable = function (options) {
|
|
|
|
var that = this;
|
|
|
|
this.options = $.extend(true, {}, DataTable.settings, options);
|
|
|
|
this.dataEntity = new DataEntity(options.map);
|
|
|
|
|
|
|
|
// handler click event
|
|
|
|
for (var name in this.options.click) {
|
|
|
|
$(name).on('click', { handler: this.options.click[name] }, function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.data.handler.call(that, this);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// handler extra click event
|
|
|
|
for (var cId in this.options.events) {
|
|
|
|
$(cId).on('click', { handler: this.options.events[cId] }, function (e) {
|
|
|
|
var options = that.options;
|
|
|
|
var row = {};
|
|
|
|
if (options.bootstrapTable && options.bootstrapTable.constructor === String) {
|
|
|
|
var arrselections = $(options.bootstrapTable).bootstrapTable('getSelections');
|
|
|
|
if (arrselections.length === 0) {
|
|
|
|
lgbSwal({ title: '请选择要编辑的数据', type: "warning" });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (arrselections.length > 1) {
|
|
|
|
lgbSwal({ title: '请选择一个要编辑的数据', type: "warning" });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
row = arrselections[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
e.data.handler.call(this, row);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
DataTable.settings = {
|
|
|
|
url: undefined,
|
|
|
|
bootstrapTable: 'table',
|
|
|
|
modal: '#dialogNew',
|
|
|
|
click: {
|
|
|
|
'#btn_query': function (element) {
|
|
|
|
if (this.options.bootstrapTable.constructor === String) $(this.options.bootstrapTable).bootstrapTable('refresh');
|
|
|
|
handlerCallback.call(this, null, element, { oper: 'query' });
|
|
|
|
},
|
|
|
|
'#btn_add': function (element) {
|
|
|
|
this.dataEntity.reset();
|
|
|
|
if (this.options.modal.constructor === String) $(this.options.modal).modal("show");
|
|
|
|
if (this.options.bootstrapTable.constructor === String) $(this.options.bootstrapTable).bootstrapTable('uncheckAll');
|
|
|
|
handlerCallback.call(this, null, element, { oper: 'create' });
|
|
|
|
},
|
|
|
|
'#btn_edit': function (element) {
|
|
|
|
var options = this.options;
|
|
|
|
var data = {};
|
|
|
|
if (options.bootstrapTable.constructor === String) {
|
|
|
|
var arrselections = $(options.bootstrapTable).bootstrapTable('getSelections');
|
|
|
|
if (arrselections.length === 0) {
|
|
|
|
lgbSwal({ title: '请选择要编辑的数据', type: "warning" });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (arrselections.length > 1) {
|
|
|
|
lgbSwal({ title: '请选择一个要编辑的数据', type: "warning" });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
data = arrselections[0];
|
|
|
|
this.dataEntity.load(data);
|
|
|
|
if (options.modal.constructor === String) $(options.modal).modal("show");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
handlerCallback.call(this, null, element, { oper: 'edit', data: data });
|
|
|
|
},
|
|
|
|
'#btn_delete': function (element) {
|
|
|
|
var that = this;
|
|
|
|
var options = this.options;
|
|
|
|
if (options.bootstrapTable.constructor === String) {
|
|
|
|
var arrselections = $(options.bootstrapTable).bootstrapTable('getSelections');
|
|
|
|
if (arrselections.length === 0) {
|
|
|
|
lgbSwal({ title: '请选择要删除的数据', type: "warning" });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
swal({
|
2018-11-22 14:50:04 +08:00
|
|
|
title: "删除数据",
|
|
|
|
text: "您确定要删除选中的所有数据吗",
|
2018-09-02 00:50:13 +08:00
|
|
|
type: "warning",
|
|
|
|
showCancelButton: true,
|
2018-11-22 10:43:10 +08:00
|
|
|
cancelButtonClass: 'btn-secondary',
|
|
|
|
confirmButtonText: "我要删除",
|
|
|
|
confirmButtonClass: "btn-danger ml-2",
|
2018-09-02 00:50:13 +08:00
|
|
|
cancelButtonText: "取消"
|
|
|
|
}, function () {
|
2019-03-10 16:05:41 +08:00
|
|
|
$.logData.push({ url: options.url, data: arrselections });
|
2018-09-02 00:50:13 +08:00
|
|
|
setTimeout(function () {
|
|
|
|
var iDs = arrselections.map(function (element, index) { return element.Id; });
|
|
|
|
$.bc({
|
2018-09-09 13:19:05 +08:00
|
|
|
url: options.url, data: iDs, method: 'delete', title: '删除数据',
|
2018-09-02 00:50:13 +08:00
|
|
|
callback: function (result) {
|
|
|
|
if (result) $(options.bootstrapTable).bootstrapTable('refresh');
|
|
|
|
handlerCallback.call(that, null, element, { oper: 'del', success: result });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}, 100);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'#btnSubmit': function (element) {
|
|
|
|
var that = this;
|
|
|
|
var options = $.extend(true, {}, this.options, { data: this.dataEntity.get() });
|
|
|
|
$.bc({
|
2018-09-09 13:19:05 +08:00
|
|
|
url: options.url, data: options.data, title: "保存数据", modal: options.modal, method: "post",
|
2018-09-02 00:50:13 +08:00
|
|
|
callback: function (result) {
|
|
|
|
if (result) {
|
|
|
|
$(options.bootstrapTable).bootstrapTable('refresh');
|
2018-10-29 10:09:55 +08:00
|
|
|
handlerCallback.call(that, null, element, { oper: 'save', success: result, data: options.data });
|
2018-09-02 00:50:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
DataTable.prototype = {
|
|
|
|
constructor: DataTable,
|
|
|
|
idEvents: function () {
|
|
|
|
var op = {
|
|
|
|
dataEntity: this.dataEntity,
|
|
|
|
table: this.options.bootstrapTable,
|
|
|
|
modal: this.options.modal,
|
2019-03-20 12:15:02 +08:00
|
|
|
src: this,
|
|
|
|
url: this.options.url
|
2018-09-02 00:50:13 +08:00
|
|
|
};
|
2019-03-20 17:56:04 +08:00
|
|
|
var formatData = function (data) {
|
|
|
|
delete data._nodes;
|
|
|
|
delete data._parent;
|
|
|
|
delete data._level;
|
|
|
|
delete data._last;
|
|
|
|
};
|
2018-09-02 00:50:13 +08:00
|
|
|
return {
|
|
|
|
'click .edit': function (e, value, row, index) {
|
|
|
|
op.dataEntity.load(row);
|
|
|
|
$(op.table).bootstrapTable('uncheckAll');
|
|
|
|
$(op.table).bootstrapTable('check', index);
|
|
|
|
handlerCallback.call(op.src, null, e, { oper: 'edit', data: row });
|
|
|
|
$(op.modal).modal("show");
|
2019-03-20 12:15:02 +08:00
|
|
|
},
|
|
|
|
'click .del': function (e, value, row, index) {
|
2019-03-20 17:56:04 +08:00
|
|
|
var text = "您确定要删除 <span class='text-danger font-weight-bold'>" + row.Name + "</span> 吗?";
|
|
|
|
var data = $.extend({}, row);
|
|
|
|
formatData(data);
|
|
|
|
data = [data];
|
|
|
|
if ($.isArray(row._nodes) && row._nodes.length > 0) {
|
|
|
|
$.each(row._nodes, function (index, element) {
|
|
|
|
var ele = $.extend({}, element);
|
|
|
|
formatData(ele);
|
|
|
|
data.push(ele);
|
|
|
|
});
|
|
|
|
text = "本删除项含有级联子项目</br>您确定要删除 <span class='text-danger font-weight-bold'>" + row.Name + "</span> 以及子项目吗?";
|
|
|
|
}
|
2019-03-20 12:15:02 +08:00
|
|
|
swal({
|
|
|
|
html: true,
|
|
|
|
title: "删除数据",
|
2019-03-20 17:56:04 +08:00
|
|
|
text: text,
|
2019-03-20 12:15:02 +08:00
|
|
|
type: "warning",
|
|
|
|
showCancelButton: true,
|
|
|
|
cancelButtonClass: 'btn-secondary',
|
|
|
|
confirmButtonText: "我要删除",
|
|
|
|
confirmButtonClass: "btn-danger ml-2",
|
|
|
|
cancelButtonText: "取消"
|
|
|
|
}, function () {
|
2019-03-20 17:56:04 +08:00
|
|
|
$.logData.push({ url: op.url, data: data });
|
2019-03-20 12:15:02 +08:00
|
|
|
setTimeout(function () {
|
2019-03-20 17:56:04 +08:00
|
|
|
var iDs = data.map(function (element, index) {
|
|
|
|
return element.Id;
|
|
|
|
});
|
2019-03-20 12:15:02 +08:00
|
|
|
$.bc({
|
|
|
|
url: op.url, data: iDs, method: 'delete', title: '删除数据',
|
|
|
|
callback: function (result) {
|
|
|
|
if (result) $(op.table).bootstrapTable('refresh');
|
|
|
|
handlerCallback.call(op.src, null, e, { oper: 'del', success: result });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}, 100);
|
|
|
|
});
|
2018-09-02 00:50:13 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function handlerCallback(callback, element, data) {
|
|
|
|
if ($.isFunction(callback)) callback.call(e, data);
|
|
|
|
if ($.isFunction(this.options.callback)) this.options.callback.call(element, data);
|
2018-06-07 00:45:47 +08:00
|
|
|
}
|
2016-10-20 17:55:29 +08:00
|
|
|
}(jQuery));
|