/**
@ Name:简单封下table
*/
layui.define(['form', 'table', 'yzn', 'laydate', 'laytpl', 'element','notice'], function(exports) {
var MOD_NAME = 'yznTable',
$ = layui.$,
table = layui.table,
yzn = layui.yzn,
laydate = layui.laydate,
element = layui.element,
laytpl = layui.laytpl,
form = layui.form,
notice = layui.notice;
var init = {
table_elem: '#currentTable',
table_render_id: 'currentTable',
};
var ColumnsForSearch = [];
yznTable = {
bindevent: function () {
// 监听请求
$(document).on('click', '[data-request]', function() {
var that = $(this);
var title = $(this).data('title'),
url = $(this).data('request') || $(this).attr("href"),
tableId = $(this).data('table'),
checkbox = $(this).data('checkbox');
var postData = {};
if (checkbox === 'true') {
tableId = tableId || init.table_render_id;
var checkStatus = table.checkStatus(tableId),
data = checkStatus.data;
if (data.length <= 0) {
yzn.msg.error('请勾选需要操作的数据');
return false;
}
var ids = [];
$.each(data, function(i, v) {
ids.push(v.id);
});
postData.id = ids;
}
title = title || '确定进行该操作?';
tableId = tableId || init.table_render_id;
func = function() {
yzn.request.post({
url: url,
data: postData,
}, function(data,res) {
yzn.msg.success(res.msg, function() {
tableId && table.reload(tableId);
});
}, function(data,res) {
yzn.msg.error(res.msg, function () {
tableId && table.reload(tableId);
});
})
}
if (typeof(that.attr('confirm')) == 'undefined') {
func();
} else {
yzn.msg.confirm(title, func);
}
return false;
});
//单行表格删除(不刷新)
$(document).on('click', '.layui-tr-del', function() {
var that = $(this),
tableId = $(this).data('table'),
href = !that.attr('data-href') ? that.attr('href') : that.attr('data-href');
tableId = tableId || init.table_render_id;
layer.confirm('删除之后无法恢复,您确定要删除吗?', { icon: 3, title: '提示信息' }, function(index) {
if (!href) {
notice.info({ message: '请设置data-href参数' });
return false;
}
$.post(href, function(res) {
if (res.code == 1) {
notice.success({ message: res.msg });
tableId && table.reload(tableId);
} else {
notice.error({ message: res.msg });
}
});
layer.close(index);
});
return false;
});
// 列表页批量操作按钮组
$(document).on('click', '[data-batch-all]', function() {
var that = $(this),
tableId = that.attr('data-batch-all'),
url = that.attr('data-href');
tableId = tableId || init.table_render_id;
url = url !== undefined ? url : window.location.href;
var checkStatus = table.checkStatus(tableId),
data = checkStatus.data;
if (data.length <= 0) {
yzn.msg.error('请选择要操作的数据');
return false;
}
var ids = [];
$.each(data, function(i, v) {
ids.push(v.id);
});
var index = yzn.msg.confirm('您确定要执行此操作吗?', function() {
yzn.request.post({
url: url,
data: {
id: ids
},
}, function(data,res) {
notice.success({ message: res.msg });
tableId && table.reload(tableId);
}, function(data,res) {
notice.error({ message: res.msg });
});
layer.close(index);
});
return false;
});
// 监听动态表格刷新
$(document).on('click', '[data-table-refresh]', function() {
var tableId = $(this).attr('data-table-refresh');
if (tableId === undefined || tableId === '' || tableId == null) {
tableId = init.table_render_id;
}
table.reload(tableId);
});
//通用状态设置开关
form.on('switch(switchStatus)', function(data) {
var that = $(this),
status = 0;
if (!that.attr('data-href')) {
notice.info({ message: '请设置data-href参数' });
return false;
}
if (this.checked) {
status = 1;
}
$.get(that.attr('data-href'), { value: status }, function(res) {
if (res.code === 1) {
notice.success({ message: res.msg });
} else {
notice.error({ message: res.msg });
that.trigger('click');
form.render('checkbox');
}
});
});
},
render: function(options) {
options.init = options.init || init;
options.modifyReload = yzn.parame(options.modifyReload, false);
options.id = options.id || options.init.table_render_id;
options.elem = options.elem || options.init.table_elem;
options.cols = options.cols || [];
options.layFilter = options.id + '_LayFilter';
options.url = options.url || options.init.index_url;
options.search = yzn.parame(options.search, true);
options.showSearch = yzn.parame(options.showSearch, true);
options.searchFormVisible = yzn.parame(options.searchFormVisible, false);
options.searchFormTpl = yzn.parame(options.searchFormTpl || options.init.searchFormTpl, false);
options.defaultToolbar = options.defaultToolbar || ['filter', 'print', 'exports'];
if (options.search && options.showSearch) {
options.defaultToolbar.push({
title: '搜索',
layEvent: 'TABLE_SEARCH',
icon: 'layui-icon-search',
extend: 'data-table-id="' + options.id + '"'
})
}
options.even = yzn.parame(options.even, true);
// 判断是否为移动端
if (yzn.checkMobile()) {
options.defaultToolbar = !options.search ? ['filter'] : ['filter', {
title: '搜索',
layEvent: 'TABLE_SEARCH',
icon: 'layui-icon-search',
extend: 'data-table-id="' + options.id + '"'
}];
}
options.searchInput = options.search ? yzn.parame(options.searchInput, true) : false;
var tableDone = options.done || function() {};
options.done = function(res, curr, count) {
tableDone(res, curr, count);
};
// 判断元素对象是否有嵌套的
options.cols = yznTable.formatCols(options.cols, options.init);
// 初始化表格lay-filter
$(options.elem).attr('lay-filter', options.layFilter);
//自定义搜索
if (options.search === true && options.searchFormTpl !== false) {
data = options.tpldata || {}
laytpl($('#' + options.searchFormTpl).html()).render(data, function (html) {
$(options.elem).before(html);
yznTable.listenTableSearch(options.id);
})
// 初始化form表单
form.render();
}
// 初始化表格搜索
if (options.search === true && options.searchFormTpl === false) {
yznTable.renderSearch(options.cols, options.elem, options.id, options.searchFormVisible);
}
// 初始化表格左上方工具栏
options.toolbar = options.toolbar || ['refresh', 'add', 'delete', 'export'];
options.toolbar = yznTable.renderToolbar(options);
var newTable = table.render(options);
// 监听表格搜索开关显示
yznTable.listenToolbar(options.layFilter, options.id);
// 监听表格开关切换
yznTable.renderSwitch(options.cols, options.init, options.id, options.modifyReload);
// 监听表格文本框编辑
yznTable.listenEdit(options.init, options.layFilter, options.id, options.modifyReload);
return newTable;
},
renderToolbar: function(options) {
var d = options.toolbar,
tableId = options.id,
searchInput = options.searchInput,
elem = options.elem,
init = options.init;
d = d || [];
var toolbarHtml = '';
$.each(d, function(i, v) {
if (v === 'refresh') {
toolbarHtml += '\n';
} else if (v === 'add') {
if (yzn.checkAuth('add', elem)) {
toolbarHtml += '\n';
}
} else if (v === 'delete') {
if (yzn.checkAuth('delete', elem)) {
toolbarHtml += '\n';
}
} else if (v === 'recyclebin') {
if (yzn.checkAuth('recyclebin', elem)) {
toolbarHtml += '\n';
}
} else if (v === 'restore') {
if (yzn.checkAuth('restore', elem)) {
toolbarHtml += '\n';
}
} else if (v === 'destroy') {
if (yzn.checkAuth('destroy', elem)) {
toolbarHtml += '\n';
}
} else if (typeof v === "object") {
$.each(v, function(ii, vv) {
vv.class = vv.class || '';
vv.icon = vv.icon || '';
vv.auth = vv.auth || '';
vv.url = vv.url || '';
vv.method = vv.method || 'open';
vv.title = vv.title || vv.text;
vv.text = vv.text || vv.title;
vv.extend = vv.extend || '';
vv.checkbox = vv.checkbox || false;
vv.html = vv.html || '';
if (yzn.checkAuth(vv.auth, elem)) {
toolbarHtml += yznTable.buildToolbarHtml(vv, tableId);
}
});
}
});
if (searchInput) {
toolbarHtml += '\n'
}
return '
' + toolbarHtml + '
';
},
buildToolbarHtml: function(toolbar, tableId) {
var html = '';
toolbar.class = toolbar.class || '';
toolbar.icon = toolbar.icon || '';
toolbar.auth = toolbar.auth || '';
toolbar.url = toolbar.url || '';
toolbar.extend = toolbar.extend || '';
toolbar.method = toolbar.method || 'open';
toolbar.field = toolbar.field || 'id';
toolbar.title = toolbar.title || toolbar.text;
toolbar.text = toolbar.text || toolbar.title;
toolbar.checkbox = toolbar.checkbox || false;
toolbar.html = toolbar.html || '';
if (toolbar.html !== '') {
return toolbar.html;
}
var formatToolbar = toolbar;
formatToolbar.icon = formatToolbar.icon !== '' ? ' ' : '';
formatToolbar.class = formatToolbar.class !== '' ? 'class="' + formatToolbar.class + '" ' : '';
if (toolbar.method === 'open') {
formatToolbar.method = formatToolbar.method !== '' ? 'data-open="' + formatToolbar.url + '" data-title="' + formatToolbar.title + '" ' : '';
} else {
formatToolbar.method = formatToolbar.method !== '' ? 'data-request="' + formatToolbar.url + '" data-title="' + formatToolbar.title + '" ' : '';
}
formatToolbar.checkbox = toolbar.checkbox ? ' data-checkbox="true" ' : '';
formatToolbar.tableId = tableId !== undefined ? ' data-table="' + tableId + '" ' : '';
html = '\n';
return html;
},
renderSearch: function(cols, elem, tableId, searchFormVisible) {
// TODO 只初始化第一个table搜索字段,如果存在多个(绝少数需求),得自己去扩展
cols = cols[0] || {};
var newCols = [];
var formHtml = '';
$.each(cols, function(i, d) {
d.field = d.field || false;
d.fieldAlias = yzn.parame(d.fieldAlias, d.field);
d.title = d.title || d.field || '';
d.selectList = d.selectList || {};
d.search = yzn.parame(d.search, true);
d.searchTip = d.searchTip || '请输入' + d.title || '';
d.searchValue = d.searchValue || '';
d.searchOp = d.searchOp || '=';
d.timeType = d.timeType || 'datetime';
d.extend = typeof d.extend === 'undefined' ? '' : d.extend;
d.addClass = typeof d.addClass === 'undefined' ? (typeof d.addclass === 'undefined' ? 'layui-input' : 'layui-input ' + d.addclass) : 'layui-input ' + d.addClass;
if (d.field !== false && d.search !== false) {
ColumnsForSearch.push(d);
switch (d.search) {
case true:
formHtml += '\t';
break;
case 'select':
//d.searchOp = '=';
var selectHtml = '';
$.each(d.selectList, function(sI, sV) {
var selected = '';
if (sI === d.searchValue) {
selected = 'selected=""';
}
selectHtml += '/n';
});
formHtml += '\t';
break;
case 'range':
d.searchOp = 'range';
formHtml += '\t';
break;
case 'time':
d.searchOp = '=';
formHtml += '\t';
break;
case 'between':
d.searchOp = 'BETWEEN';
formHtml += '\t';
break;
}
//newCols.push(d);
}
});
if (formHtml !== '') {
$(elem).before('');
yznTable.listenTableSearch(tableId);
// 初始化form表单
form.render();
}
},
formatCols: function(cols, init) {
for (i in cols) {
var col = cols[i];
for (index in col) {
var val = col[index];
// 判断是否包含初始化数据
if (val.init === undefined) {
cols[i][index]['init'] = init;
}
// 格式化列操作栏
if (val.templet === yznTable.formatter.tool && val.operat === undefined) {
cols[i][index]['operat'] = ['edit', 'delete'];
}
// 判断是否包含开关组件
if (val.templet === yznTable.formatter.switch && val.filter === undefined) {
cols[i][index]['filter'] = val.field;
}
// 判断是否含有搜索下拉列表
if (val.selectList !== undefined && val.search === undefined) {
cols[i][index]['search'] = 'select';
}
// 判断是否初始化对齐方式
if (val.align === undefined) {
cols[i][index]['align'] = 'left';
}
// 部分字段开启排序
var sortDefaultFields = ['id', 'sort'];
if (val.sort === undefined && sortDefaultFields.indexOf(val.field) >= 0) {
cols[i][index]['sort'] = true;
}
// 初始化图片高度
if (val.templet === yznTable.formatter.image && val.imageHeight === undefined) {
cols[i][index]['imageHeight'] = 30;
}
// 判断是否多层对象
if (val.field !== undefined && val.field.split(".").length > 1) {
if (val.templet === undefined) {
cols[i][index]['templet'] = yznTable.formatter.value;
}
}
// 判断是否列表数据转换
/*if (val.selectList !== undefined && val.templet === undefined) {
cols[i][index]['templet'] = yznTable.formatter.list;
}*/
}
}
return cols;
},
listenTableSearch: function(tableId) {
var that = this;
that.$commonsearch = $(".table-search-fieldset");
layui.define(['yznForm'], function(exports) {
var yznForm = layui.yznForm;
yznForm.bindevent(that.$commonsearch);
})
form.on('submit(' + tableId + '_filter)', function(data) {
var searchQuery = yznTable.getSearchQuery(that, true);
table.reload(tableId, {
page: {
curr: 1
},
where: {
filter: JSON.stringify(searchQuery.filter),
op: JSON.stringify(searchQuery.op)
}
});
return false;
})
//表格点击搜索
$(document).on("click", ".searchit", function () {
var value = $(this).data("value");
var field = $(this).data("field");
var obj = $("form [name='" + field + "']", that.$commonsearch);
if (obj.length > 0) {
if (obj.is("select")) {
$("option[value='" + value + "']", obj).prop("selected", true);
form.render('select');
} else if (obj.length > 1) {
$("form [name='" + field + "'][value='" + value + "']", that.$commonsearch).prop("checked", true);
} else {
obj.val(value + "");
}
obj.trigger("change");
$("form button[type='submit']",that.$commonsearch).trigger("click");
}
});
//快速搜索
$(document).on('blur', '#layui-input-search', function(event) {
var text = $(this).val();
table.reload(tableId, { where: { search: text } });
$('#layui-input-search').prop("value", $(this).val());
return false
})
},
getSearchQuery: function(that, removeempty) {
var op = {};
var filter = {};
var value = '';
$("form.form-commonsearch .operate", that.$commonsearch).each(function(i) {
var name = $(this).data("name");
var sym = $(this).is("select") ? $("option:selected", this).val() : $(this).val().toUpperCase();
var obj = $("[name='" + name + "']", that.$commonsearch);
if (obj.length == 0)
return true;
var vObjCol = ColumnsForSearch[i];
var process = vObjCol && typeof vObjCol.process == 'function' ? vObjCol.process : null;
if (obj.length > 1) {
if (/BETWEEN$/.test(sym)) {
var value_begin = $.trim($("[name='" + name + "']:first", that.$commonsearch).val()),
value_end = $.trim($("[name='" + name + "']:last", that.$commonsearch).val());
if (value_begin.length || value_end.length) {
if (process) {
value_begin = process(value_begin, 'begin');
value_end = process(value_end, 'end');
}
value = value_begin + ',' + value_end;
} else {
value = '';
}
//如果是时间筛选,将operate置为RANGE
if ($("[name='" + name + "']:first", that.$commonsearch).hasClass("datetimepicker")) {
sym = 'RANGE';
}
} else {
value = $("[name='" + name + "']:checked", that.$commonsearch).val();
value = process ? process(value) : value;
}
} else {
value = process ? process(obj.val()) : obj.val();
}
if (removeempty && (value == '' || value == null || ($.isArray(value) && value.length == 0)) && !sym.match(/null/i)) {
return true;
}
op[name] = sym;
filter[name] = value;
});
return { op: op, filter: filter };
},
listenToolbar: function(layFilter, tableId) {
table.on('toolbar(' + layFilter + ')', function(obj) {
// 搜索表单的显示
switch (obj.event) {
case 'TABLE_SEARCH':
var searchFieldsetId = 'searchFieldset_' + tableId;
var _that = $("#" + searchFieldsetId);
if (_that.hasClass("layui-hide")) {
_that.removeClass('layui-hide');
} else {
_that.addClass('layui-hide');
}
break;
}
});
},
renderSwitch: function (cols, tableInit, tableId, modifyReload) {
tableInit.modify_url = tableInit.modify_url || false;
cols = cols[0] || {};
tableId = tableId || init.table_render_id;
if (cols.length > 0) {
$.each(cols, function (i, v) {
v.filter = v.filter || false;
if (v.filter !== false && tableInit.modify_url !== false) {
yznTable.listenSwitch({filter: v.filter, url: tableInit.modify_url, tableId: tableId, modifyReload: modifyReload});
}
});
}
},
listenSwitch: function (option, ok) {
option.filter = option.filter || '';
option.url = option.url || '';
option.field = option.field || option.filter || '';
option.tableId = option.tableId || init.table_render_id;
option.modifyReload = option.modifyReload || false;
form.on('switch(' + option.filter + ')', function (obj) {
var checked = obj.elem.checked ? 1 : 0;
if (typeof ok === 'function') {
return ok({
id: obj.value,
checked: checked,
});
} else {
var data = {
id: obj.value,
param: option.field,
value: checked,
};
yzn.request.post({
url: option.url,
prefix: true,
data: data,
}, function (data,res) {
notice.success({ message: res.msg });
if (option.modifyReload) {
table.reload(option.tableId);
}
}, function (data,res) {
yzn.msg.error(res.msg, function () {
table.reload(option.tableId);
});
}, function () {
table.reload(option.tableId);
});
}
});
},
listenEdit: function(tableInit, layFilter, tableId, modifyReload) {
tableInit.modify_url = tableInit.modify_url || false;
tableId = tableId || init.table_render_id;
if (tableInit.modify_url !== false) {
table.on('edit(' + layFilter + ')', function(obj) {
var value = obj.value,
data = obj.data,
id = data.id,
field = obj.field;
var _data = {
id: id,
param: field,
value: value,
};
yzn.request.post({
url: tableInit.modify_url,
prefix: true,
data: _data,
}, function(data,res) {
notice.success({ message: res.msg });
if (modifyReload) {
table.reload(tableId);
}
}, function(data,res) {
yzn.msg.error(res.msg, function() {
table.reload(tableId);
});
}, function() {
table.reload(tableId);
});
});
}
},
toolSpliceUrl(url, field, data) {
data.id = typeof data.id !== 'undefined' ? data.id : 0;
url = url == null || url.length === 0 ? '' : url.toString();
//自动添加id参数
url = !url.match(/(?=([?&]id=)|(\/id\/)|(\{id}))/i) ?
url + (url.match(/(\?|&)+/) ? "&id=" : "?id=") + '{id}' : url;
url = url.replace(/\{(.*?)\}/gi, function (matched) {
matched = matched.substring(1, matched.length - 1);
if (matched.indexOf(".") !== -1) {
var temp = data;
var arr = matched.split(/\./);
for (var i = 0; i < arr.length; i++) {
if (typeof temp[arr[i]] !== 'undefined') {
temp = temp[arr[i]];
}
}
return typeof temp === 'object' ? '' : temp;
}
return data[matched];
});
return url;
},
buildOperatHtml: function(operat) {
var html = '';
operat.class = operat.class || '';
operat.icon = operat.icon || '';
//operat.auth = operat.auth || '';
operat.url = operat.url || '';
operat.extend = operat.extend || '';
operat.method = operat.method || 'open';
operat.field = operat.field || 'id';
operat.title = operat.title || operat.text;
operat.text = operat.text || operat.title;
var formatOperat = operat;
formatOperat.icon = formatOperat.icon !== '' ? ' ' : '';
formatOperat.class = formatOperat.class !== '' ? 'class="' + formatOperat.class + '" ' : '';
if (operat.method === 'open') {
formatOperat.method = formatOperat.method !== '' ? 'data-open="' + formatOperat.url + '" data-title="' + formatOperat.title + '" ' : '';
} else if (operat.method === 'href'){
formatOperat.method = formatOperat.method !== '' ? 'href="' + formatOperat.url + '" data-title="' + formatOperat.title + '" ' : '';
} else if (operat.method === 'none'){ // 常用于与extend配合,自定义监听按钮
formatOperat.method = '';
} else {
formatOperat.method = formatOperat.method !== '' ? 'data-request="' + formatOperat.url + '" data-title="' + formatOperat.title + '" ' : '';
}
html = '' + formatOperat.icon + formatOperat.text + '';
return html;
},
getItemField: function (item, field) {
var value = item;
if (typeof field !== 'string' || item.hasOwnProperty(field)) {
return item[field];
}
var props = field.split('.');
for (var p in props) {
if (props.hasOwnProperty(p)) {
value = value && value[props[p]];
}
}
return value;
},
formatter: {
tool: function(data) {
var that = this;
that.operat = that.operat || ['edit', 'delete'];
var elem = that.init.table_elem || init.table_elem;
var html = '';
$.each(that.operat, function(i, item) {
if (typeof item === 'string') {
switch (item) {
case 'edit':
var operat = {
class: 'layui-btn layui-btn-success layui-btn-xs',
method: 'open',
field: 'id',
icon: '',
auth: 'edit',
text: "",
title: '编辑信息',
url: that.init.edit_url,
extend: ""
};
operat.url = yznTable.toolSpliceUrl(operat.url, operat.field, data);
if (yzn.checkAuth(operat.auth, elem)) {
html += yznTable.buildOperatHtml(operat);
}
break;
case 'restore':
var operat = {
class: 'layui-btn layui-btn-xs',
method: 'request',
field: 'id',
icon: '',
auth: 'restore',
text: " 还原",
title: '还原',
url: that.init.restore_url,
extend: ""
};
operat.url = yznTable.toolSpliceUrl(operat.url, operat.field, data);
if (yzn.checkAuth(operat.auth, elem)) {
html += yznTable.buildOperatHtml(operat);
}
break;
case 'destroy':
var operat = {
class: 'layui-btn layui-btn-danger layui-btn-xs',
method: 'request',
field: 'id',
icon: '销毁',
auth: 'destroy',
text: " 销毁",
title: '',
url: that.init.destroy_url,
extend: ""
};
operat.url = yznTable.toolSpliceUrl(operat.url, operat.field, data);
if (yzn.checkAuth(operat.auth, elem)) {
html += yznTable.buildOperatHtml(operat);
}
break;
case 'delete':
var operat = {
class: 'layui-btn layui-btn-danger layui-btn-xs layui-tr-del',
method: 'href',
field: 'id',
icon: '',
auth: 'delete',
text: "",
title: '',
url: that.init.delete_url,
extend: ""
};
operat.url = yznTable.toolSpliceUrl(operat.url, operat.field, data);
if (yzn.checkAuth(operat.auth, elem)) {
html += yznTable.buildOperatHtml(operat);
}
break;
}
} else if (typeof item === 'object') {
$.each(item, function(i, operat) {
hidden = typeof operat.hidden === 'function' ? operat.hidden.call(yznTable, data, operat) : (typeof operat.hidden !== 'undefined' ? operat.hidden : false);
if (hidden) {
return true;
}
url = operat.url || '';
operat.class = operat.class || '';
operat.icon = operat.icon || '';
operat.auth = operat.auth || '';
operat.method = operat.method || 'open';
operat.field = operat.field || 'id';
operat.text = typeof operat.text === 'function' ? operat.text.call(yznTable, data, operat) : operat.text ? operat.text : '';
operat.title = typeof operat.title === 'function' ? operat.title.call(yznTable, data, operat) : operat.title ? operat.title : operat.text;
operat.extend = operat.extend || '';
operat.url = typeof url === 'function' ? url.call(yznTable, data, operat) : (url ? yznTable.toolSpliceUrl(url, operat.field, data) : 'javascript:;');
disable = typeof operat.disable === 'function' ? operat.disable.call(yznTable, data, operat) : (typeof operat.disable !== 'undefined' ? operat.disable : false);
if (disable) {
operat.class = operat.class + ' layui-btn-disabled';
}
//operat.url = yznTable.toolSpliceUrl(operat.url, operat.field, data);
if (yzn.checkAuth(operat.auth, elem)) {
html += yznTable.buildOperatHtml(operat);
}
});
}
});
return html;
},
status: function (data) {
var custom = {normal: 'success', hidden: 'gray', deleted: 'danger', locked: 'info'};
if (typeof this.custom !== 'undefined') {
custom = $.extend(custom, this.custom);
}
this.custom = custom;
this.icon = 'iconfont icon-circle-fill';
return yznTable.formatter.normal.call(this, data);
},
normal: function (data) {
var that = this;
var colorArr = ["danger", "success", "primary", "warning", "info", "gray", "red", "yellow", "aqua", "blue", "navy", "teal", "olive", "lime", "fuchsia", "purple", "maroon"];
var custom = {};
if (typeof that.custom !== 'undefined') {
custom = $.extend(custom, that.custom);
}
var field = that.field;
try {
var value = yznTable.getItemField(data,field);
value = value == null || value.length === 0 ? '' : value.toString();
} catch (e) {
var value = undefined;
}
value = value == null || value.length === 0 ? '' : value.toString();
var keys = typeof that.selectList === 'object' ? Object.keys(that.selectList) : [];
var index = keys.indexOf(value);
var color = value && typeof custom[value] !== 'undefined' ? custom[value] : null;
var display = index > -1 ? that.selectList[value] : null;
var icon = typeof that.icon !== 'undefined' ? that.icon : null;
if (!color) {
color = index > -1 && typeof colorArr[index] !== 'undefined' ? colorArr[index] : 'primary';
}
if (!display) {
display = value.charAt(0).toUpperCase() + value.slice(1);
}
var html = '' + (icon ? '' : '') + display + '';
if (that.search != false) {
html = '' + html + '';
}
return html;
},
flag: function (data) {
var that = this;
var field = that.field;
try {
var value = yznTable.getItemField(data,field);
value = value == null || value.length === 0 ? '' : value.toString();
} catch (e) {
var value = undefined;
}
//赤色 墨绿 蓝色 藏青 雅黑 橙色
var colorArr = {0:'red',1:'green',2:'blue',3:'cyan',4:'black',5:'orange'};
//如果字段列有定义custom
if (typeof that.custom !== 'undefined') {
colorArr = $.extend(colorArr, that.custom);
}
if (typeof that.selectList === 'object' && typeof that.custom === 'undefined') {
var i = 0;
var searchValues = Object.values(colorArr);
$.each(that.selectList, function (key, val) {
if (typeof colorArr[key] == 'undefined') {
colorArr[key] = searchValues[i];
i = typeof searchValues[i + 1] === 'undefined' ? 0 : i + 1;
}
});
}
//渲染Flag
var html = [];
var arr = value != '' ? value.split(',') : [];
var color, display, label;
$.each(arr, function (i, value) {
value = value == null || value.length === 0 ? '' : value.toString();
if (value == '')
return true;
color = value && typeof colorArr[value] !== 'undefined' ? colorArr[value] : 'green';
display = typeof that.selectList !== 'undefined' && typeof that.selectList[value] !== 'undefined' ? that.selectList[value] : value.charAt(0).toUpperCase() + value.slice(1);
label = '' + display + '';
if (that.search != false) {
html.push('' + label + '');
} else {
html.push(label);
}
})
return html.join(' ');
},
label: function (data) {
return yznTable.formatter.flag.call(this, data);
},
switch: function (data) {
var that = this;
var field = that.field;
that.filter = that.filter || that.field || null;
that.checked = that.checked || 1;
that.tips = that.tips || '开|关';
try {
var value = yznTable.getItemField(data,field);
} catch (e) {
var value = undefined;
}
var checked = value === that.checked ? 'checked' : '';
return laytpl('').render(data);
},
image: function(data) {
var that = this;
that.imageWidth = that.imageWidth || 80;
that.imageHeight = that.imageHeight || 30;
that.imageSplit = that.imageSplit || ',';
that.imageJoin = that.imageJoin || ' ';
that.title = that.title || that.field;
var field = that.field,
title = data[that.title];
try {
var value = yznTable.getItemField(data,field);
} catch (e) {
var value = undefined;
}
if (!value) {
return '';
} else {
var valuesHtml = [];
valuesHtml.push('
');
return valuesHtml.join(that.imageJoin);
}
},
images: function (data) {
var that = this;
that.imageWidth = that.imageWidth || 80;
that.imageHeight = that.imageHeight || 30;
that.imageSplit = that.imageSplit || ',';
that.imageJoin = that.imageJoin || ' ';
that.title = that.title || that.field;
var field = that.field,
title = data[that.title];
try {
var value = yznTable.getItemField(data,field);
} catch (e) {
var value = undefined;
}
if (!value) {
return '';
} else {
var values = value.split(that.imageSplit),
valuesHtml = [];
values.forEach((value, index) => {
valuesHtml.push('
');
});
return valuesHtml.join(that.imageJoin);
}
},
file: function(data) {
var that = this;
that.imageWidth = that.imageWidth || 80;
that.imageHeight = that.imageHeight || 30;
that.imageSplit = that.imageSplit || ',';
that.imageJoin = that.imageJoin || ' ';
that.title = that.title || that.field;
var field = that.field,
title = data[that.title];
try {
var value = yznTable.getItemField(data,field);
} catch (e) {
var value = undefined;
}
if (!value) {
return '';
} else {
var values = value.split(that.imageSplit),
valuesHtml = [];
values.forEach((value, index) => {
suffix = /[\.]?([a-zA-Z0-9]+)$/.exec(value);
suffix = suffix ? suffix[1] : 'file';
url = yzn.request.fixurl("admin/ajax/icon?suffix=" + suffix);
valuesHtml.push('
');
});
return valuesHtml.join(that.imageJoin);
}
},
files: function (data) {
return yznTable.formatter.image.file(this, data);
},
url: function(data) {
var field = this.field;
try {
var value = yznTable.getItemField(data,field);
} catch (e) {
var value = undefined;
}
return '';
},
price: function(data) {
var field = this.field;
try {
var value = yznTable.getItemField(data,field);
} catch (e) {
var value = undefined;
}
return '¥' + value + '';
},
icon: function(data) {
var field = this.field;
try {
var value = yznTable.getItemField(data,field);
} catch (e) {
var value = undefined;
}
return '';
},
text: function(data) {
var field = this.field;
try {
var value = yznTable.getItemField(data,field);
} catch (e) {
var value = undefined;
}
return '' + value + '';
},
value: function(data) {
var field = this.field;
try {
var value = yznTable.getItemField(data,field);
} catch (e) {
var value = undefined;
}
return '' + value + '';
},
datetime: function (data) {
var that = this;
var field = that.field;
try {
var value = yznTable.getItemField(data,field);
} catch (e) {
var value = undefined;
}
var datetimeFormat = typeof that.datetimeFormat === 'undefined' ? 'yyyy-MM-dd HH:mm:ss' : that.datetimeFormat;
if (value && isNaN(Date.parse(value))) {
return layui.util.toDateString(value * 1000, datetimeFormat)
} else if (value && !isNaN(Date.parse(value))) {
return layui.util.toDateString(Date.parse(value), datetimeFormat)
} else {
return '-';
}
}
},
}
exports(MOD_NAME, yznTable);
});