Datagrid获取当前列信息
我们在使用datagrid列属性formatter时,有些场景下,在formatter函数内部,我们需要知道当前列的一些配置信息,比如说title,filed 等信息,
而formatter函数入参只有行索引,值,行数据,那么怎么获取title,filed等信息呢?其实formatter函数内部的this在运行时默认就是指向当前列的配置对象,
且组件内部并没有改变this指向,所以通过this就可以拿到所有信息了。
$('#dg').datagrid({
columns: [
[
{
field: 'userId',
title: 'User',
width: 80,
formatter: function(value,
row,
index){
console.log(this.title);//Userconsole.log(this.field);//userIdif(row.user){
return row.user.name;
}else{
return value;
}
}
}
]
]
});
类似的列属性还有styler和sorter