使用JS Link属性读取lookup field(查阅栏)的值

原文地址:点击打开链接

在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的值。

展示的结果(左侧是单值,右边是多值的情况):



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值