// 单元格数据格式化
formatter: {
icon: function (value, row, index) {
if (!value)
return '';
value = value === null ? '' : value.toString();
value = value.indexOf(" ") > -1 ? value : "fa fa-" + value;
//渲染fontawesome图标
return '<i class="' + value + '"></i> ' + value;
},
image: function (value, row, index) {
value = value ? value : '/assets/img/blank.gif';
var classname = typeof this.classname !== 'undefined' ? this.classname : 'img-sm img-center';
return '<a href="javascript:"><img class="' + classname + '" src="' + Fast.api.cdnurl(value) + '" /></a>';
},
images: function (value, row, index) {
value = value === null ? '' : value.toString();
var classname = typeof this.classname !== 'undefined' ? this.classname : 'img-sm img-center';
var arr = value.split(',');
var html = [];
$.each(arr, function (i, value) {
value = value ? value : '/assets/img/blank.gif';
html.push('<a href="javascript:"><img class="' + classname + '" src="' + Fast.api.cdnurl(value) + '" /></a>');
});
return html.join(' ');
},
content: function (value, row, index) {
var width = this.width != undefined ? (this.width.match(/^\d+$/) ? this.width + "px" : this.width) : "250px";
return "<div style='white-space: nowrap; text-overflow:ellipsis; overflow: hidden; max-width:" + width + ";'>" + value + "</div>";
},
status: function (value, row, index) {
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 = 'fa fa-circle';
return Table.api.formatter.normal.call(this, value, row, index);
},
normal: function (value, row, index) {
var colorArr = ["primary", "success", "danger", "warning", "info", "gray", "red", "yellow", "aqua", "blue", "navy", "teal", "olive", "lime", "fuchsia", "purple", "maroon"];
var custom = {};
if (typeof this.custom !== 'undefined') {
custom = $.extend(custom, this.custom);
}
value = value === null ? '' : value.toString();
var keys = typeof this.searchList === 'object' ? Object.keys(this.searchList) : [];
var index = keys.indexOf(value);
var color = value && typeof custom[value] !== 'undefined' ? custom[value] : null;
var display = index > -1 ? this.searchList[value] : null;
var icon = typeof this.icon !== 'undefined' ? this.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 = '<span class="text-' + color + '">' + (icon ? '<i class="' + icon + '"></i> ' : '') + display + '</span>';
if (this.operate != false) {
html = '<a href="javascript:;" class="searchit" data-toggle="tooltip" title="' + __('Click to search %s', display) + '" data-field="' + this.field + '" data-value="' + value + '">' + html + '</a>';
}
return html;
},
toggle: function (value, row, index) {
var table = this.table;
var options = table ? table.bootstrapTable('getOptions') : {};
var pk = options.pk || "id";
var color = typeof this.color !== 'undefined' ? this.color : 'success';
var yes = typeof this.yes !== 'undefined' ? this.yes : 1;
var no = typeof this.no !== 'undefined' ? this.no : 0;
var url = typeof this.url !== 'undefined' ? this.url : '';
var confirm = '';
var disable = false;
if (typeof this.confirm !== "undefined") {
confirm = typeof this.confirm === "function" ? this.confirm.call(this, value, row, index) : this.confirm;
}
if (typeof this.disable !== "undefined") {
disable = typeof this.disable === "function" ? this.disable.call(this, value, row, index) : this.disable;
}
return "<a href='javascript:;' data-toggle='tooltip' title='" + __('Click to toggle') + "' class='btn-change " + (disable ? 'btn disabled no-padding' : '') + "' data-index='" + index + "' data-id='"
+ row[pk] + "' " + (url ? "data-url='" + url + "'" : "") + (confirm ? "data-confirm='" + confirm + "'" : "") + " data-params='" + this.field + "=" + (value == yes ? no : yes) + "'><i class='fa fa-toggle-on text-success text-" + color + " " + (value == yes ? '' : 'fa-flip-horizontal text-gray') + " fa-2x'></i></a>";
},
url: function (value, row, index) {
value = value === null ? '' : value.toString();
return '<div class="input-group input-group-sm" style="width:250px;margin:0 auto;"><input type="text" class="form-control input-sm" value="' + value + '"><span class="input-group-btn input-group-sm"><a href="' + value + '" target="_blank" class="btn btn-default btn-sm"><i class="fa fa-link"></i></a></span></div>';
},
search: function (value, row, index) {
var field = this.field;
if (typeof this.customField !== 'undefined' && typeof row[this.customField] !== 'undefined') {
value = row[this.customField];
field = this.customField;
}
return '<a href="javascript:;" class="searchit" data-toggle="tooltip" title="' + __('Click to search %s', value) + '" data-field="' + field + '" data-value="' + value + '">' + value + '</a>';
},
addtabs: function (value, row, index) {
var url = Table.api.replaceurl(this.url, row, this.table);
var title = this.atitle ? this.atitle : __("Search %s", value);
return '<a href="' + Fast.api.fixurl(url) + '" class="addtabsit" data-value="' + value + '" title="' + title + '">' + value + '</a>';
},
dialog: function (value, row, index) {
var url = Table.api.replaceurl(this.url, row, this.table);
var title = this.atitle ? this.atitle : __("View %s", value);
return '<a href="' + Fast.api.fixurl(url) + '" class="dialogit" data-value="' + value + '" title="' + title + '">' + value + '</a>';
},
flag: function (value, row, index) {
var that = this;
value = value === null ? '' : value.toString();
var colorArr = {index: 'success', hot: 'warning', recommend: 'danger', 'new': 'info'};
//如果字段列有定义custom
if (typeof this.custom !== 'undefined') {
colorArr = $.extend(colorArr, this.custom);
}
var field = this.field;
if (typeof this.customField !== 'undefined' && typeof row[this.customField] !== 'undefined') {
value = row[this.customField];
field = this.customField;
}
//渲染Flag
var html = [];
var arr = value.split(',');
var color, display, label;
$.each(arr, function (i, value) {
value = value === null ? '' : value.toString();
if (value == '')
return true;
color = value && typeof colorArr[value] !== 'undefined' ? colorArr[value] : 'primary';
display = typeof that.searchList !== 'undefined' && typeof that.searchList[value] !== 'undefined' ? that.searchList[value] : __(value.charAt(0).toUpperCase() + value.slice(1));
label = '<span class="label label-' + color + '">' + display + '</span>';
if (that.operate) {
html.push('<a href="javascript:;" class="searchit" data-toggle="tooltip" title="' + __('Click to search %s', display) + '" data-field="' + field + '" data-value="' + value + '">' + label + '</a>');
} else {
html.push(label);
}
});
return html.join(' ');
},
label: function (value, row, index) {
return Table.api.formatter.flag.call(this, value, row, index);
},
datetime: function (value, row, index) {
var datetimeFormat = typeof this.datetimeFormat === 'undefined' ? 'YYYY-MM-DD HH:mm:ss' : this.datetimeFormat;
if (isNaN(value)) {
return value ? Moment(value).format(datetimeFormat) : __('None');
} else {
return value ? Moment(parseInt(value) * 1000).format(datetimeFormat) : __('None');
}
},
operate: function (value, row, index) {
var table = this.table;
// 操作配置
var options = table ? table.bootstrapTable('getOptions') : {};
// 默认按钮组
var buttons = $.extend([], this.buttons || []);
// 所有按钮名称
var names = [];
buttons.forEach(function (item) {
names.push(item.name);
});
if (options.extend.dragsort_url !== '' && names.indexOf('dragsort') === -1) {
buttons.push(Table.button.dragsort);
}
if (options.extend.edit_url !== '' && names.indexOf('edit') === -1) {
Table.button.edit.url = options.extend.edit_url;
buttons.push(Table.button.edit);
}
if (options.extend.del_url !== '' && names.indexOf('del') === -1) {
buttons.push(Table.button.del);
}
return Table.api.buttonlink(this, buttons, value, row, index, 'operate');
}
,
buttons: function (value, row, index) {
// 默认按钮组
var buttons = $.extend([], this.buttons || []);
return Table.api.buttonlink(this, buttons, value, row, index, 'buttons');
}
},
https://ask.fastadmin.net/article/8071.html