Sharepoint 2013. 使用 JavaScript 的多值查找字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22694749/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 23:35:01  来源:igfitidea点击:

Sharepoint 2013. Multivalue lookup field with JavaScript

javascriptsharepoint

提问by Marco

Is there a way to edit a multivalue lookup field using the JavaScript Client Object Model? I need to remove one or more lookup values and, eventually, add one or more values.

有没有办法使用 JavaScript 客户端对象模型编辑多值查找字段?我需要删除一个或多个查找值,并最终添加一个或多个值。

I search everywhere, I read MSDN documentation, ..., I also take a look under my desk!

我到处搜索,我阅读MSDN文档,...,我也在我的桌子底下看看!

Thanks.

谢谢。

回答by Vadim Gremyachev

Multiple-Column Lookupvalue is represented as an array of SP.FieldLookupValueobjects.

Multiple-Column Lookupvalue 表示为SP.FieldLookupValue对象的数组。

How to read multiple Lookupfield value

如何读取多个Lookup字段值

var context = new SP.ClientContext.get_current();
var web = context.get_web();
var list = web.get_lists().getByTitle(listTitle);
var listItem = list.getItemById(1);   
context.load(listItem);
context.executeQueryAsync(
   function() {
       var lookupVals = listItem.get_item(fieldName); //get multi lookup value (SP.FieldLookupValue[])
       for(var i = 0;i < lookupVals.length;i++) {
           console.log(lookupVals[i].get_lookupId()); //print Id
           console.log(lookupVals[i].get_lookupValue()); //print Value
       }
   },
   function(sender,args){
       console.log(args.get_message());
   }
);

How to update multiple Lookupfield value

如何更新多个Lookup字段值

For updating multiple Lookup value you need to specify value of type SP.FieldLookupValue[]. Note, SP.FieldLookupValuecould be initialized by specifying LookupIdonly.

要更新多个 Lookup 值,您需要指定 type 的值SP.FieldLookupValue[]。注意,SP.FieldLookupValue只能通过指定来初始化LookupId

var context = new SP.ClientContext.get_current();
var web = context.get_web();
var list = web.get_lists().getByTitle(listTitle);
var listItem = list.getItemById(1);   

var lookupVals = [];
//set 1st Lookup value
var lookupVal1 = new SP.FieldLookupValue();
lookupVal1.set_lookupId(1);
lookupVals.push(lookupVal1);
//set 2nd Lookup value
var lookupVal2 = new SP.FieldLookupValue();
lookupVal2.set_lookupId(2);
lookupVals.push(lookupVal2);

listItem.set_item(fieldName,lookupVals);
listItem.update();

context.executeQueryAsync(
   function() {
        console.log('Multi lookup field has been updated');
   },
   function(sender,args){
       console.log(args.get_message());
   }
);

回答by suoko

You can get lookup values via js this way:

您可以通过这种方式通过 js 获取查找值:

clientContext.executeQueryAsync(function () {

clientContext.executeQueryAsync(function () {

    var listItemEnumerator = collListItem.getEnumerator();

    while (listItemEnumerator.moveNext()) {
                var oListItem = listItemEnumerator.get_current();
                chartX.push(oListItem.get_item(xAxisName));


            }
    lookupValue1= chartX["0"].g_1;
    lookupValue2= chartX["1"].g_1;