easyui的formatter属性的用法

easyui的formatter属性可以帮助我们更加灵活的显示数据库中的数据。


比如,我有一个启用禁用字段,使用数字表示,1表示启用,2表示禁用,展示给客户的时候我当然希望是中文的形式。


只需要写这么一个formatter方法:(isu就是这个字段)

	function formatterIsu(value, row, index){
		if(row.isu==1){
			return "启用";
		}else if(row.isu==2){
			return "禁用";
		}
	}

然后在标签里的将这个函数赋值给formatter属性:

easyui的formatter属性的用法_第1张图片


最后页面上输出的就是中文的启用,禁用了。

很多时候,创建表格头也是通过js来完成,里面也是可以加入formatter函数来格式化字段的:

$(document).ready(function() {
		$('#dg').datagrid({
			url : "/bboo/resource/gj-resource!queryResource.do",
			columns : [ [ {
				field : 'resourceId',
				title : '菜单id',
				width : 50,
				sortable : true
			}, {
				field : 'description',
				title : '描述',
				width : 50,
				sortable : true
			}, {
				field : 'name',
				title : '名称',
				width : 50,
				sortable : true
			}, {
				field : 'sort',
				title : '分类',
				width : 50,
				sortable : true
			}, {
				field : 'type',
				title : '类型',
				width : 50,
				sortable : true
			}, {
				field : 'value',
				title : 'url值',
				width : 50,
				sortable : true
			}, {
				field : 'pid',
				title : '父菜单',
				width : 80,
				formatter : function(value, row, index) {
					if (row.parent) {
						return row.parent.resourceId;
					} else {
						return value;
					}
				}
			} ] ]
		});
	});

上面的pid字段就是格式化后的数据。


你可能感兴趣的:(easyui)