原文地址:点击打开链接
在SharePoint 2013中,引入了JS link这个新特性,如果你自定义了一个lookup类型的field,可以使用JS Link这个属性,来读取并显示lookup的值,这种方式比之前的XSL方便很多。而且不需要像之前的处理方式那样需要移除"itemId;#"来获得真正的值。
具体方法是:
var lookupSample = lookupSample || {};
lookupSample.CustomizeFieldRendering = function () {
// Intialize the variables for overrides objects
var overrideCtx = {
Templates: {
Fields: {
'singleLookupValue': {
'View' : lookupSample.singleLookupValue
},
'multiLookupValue': {
'View' : lookupSample.multiLookupValue
}
}
}
};
// Register the override of the field
SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCtx);
}
lookupSample.singleLookupValue = function (ctx) {
var output = [];
var field = ctx.CurrentItem[ctx.CurrentFieldSchema.Name];
// Check if field contains data
if (field.length > 0) {
// field[0].lookupId -> gives you the linked item ID
var lookupValue = field[0].lookupValue;
}
// Push the value to the array
output.push(lookupValue);
return output.join('');
}
lookupSample.multiLookupValue = function (ctx) {
var output = [];
var field = ctx.CurrentItem[ctx.CurrentFieldSchema.Name];
// Check if field contains data
if (field.length > 0) {
output.push('<ul>');
for (i = 0; i < field.length; i++) {
output.push('<li>');
output.push(field[i].lookupValue);
output.push('</li>');
}
output.push('</ul>');
}
return output.join('');
}
lookupSample.CustomizeFieldRendering();
以上代码包含了两个例子:
1. 第一个例子“singleLookupSample”,用来展示单值lookup的值。
2. 第二个例子“multiLookupSample”,用来展示多值lookup的值。
展示的结果(左侧是单值,右边是多值的情况):
5604

被折叠的 条评论
为什么被折叠?



