gpt4 book ai didi

javascript - SharePoint JavaScript CSOM : Best way to deal with Lookup fields?

转载 作者:行者123 更新时间:2023-12-03 05:05:46 24 4
gpt4 key购买 nike

我是 CSOM 新手(到目前为止我已经使用过 SPServices),并且我正在尝试学习获取列表项的基础知识。我有一个包含很多列的列表,其中一些是对其他列表的查找。当我尝试获取并打印出查找列之一时,它显示为 [Object object]。我猜测查找字段将作为包含查找列表中的整个条目的对象返回。那是对的吗?如果是这样,从对象获取我想要显示的字段的最佳方法是什么?请参阅下面的代码:

<script src="/_layouts/1033/init.js" type="text/javascript"></script>
<script src="/_layouts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="/_layouts/sp.core.js" type="text/javascript"></script>
<script src="/_layouts/sp.runtime.js" type="text/javascript"></script>
<script src="/_layouts/sp.js" type="text/javascript"></script>
<script type="text/javascript" src="../SiteAssets/js/jquery-1.12.3.min.js"></script>

<SCRIPT type=text/javascript>
$(document).ready(function() {

function GetListItemsFromSPList(listId) {
var context = new SP.ClientContext.get_current();
var web = context.get_web();
var list = web.get_lists().getById(listId);
var query = SP.CamlQuery.createAllItemsQuery();
var allItems = list.getItems(query);
context.load(allItems, 'Include(Id, ContractType)');
context.executeQueryAsync(Function.createDelegate(this, function () { onQuerySuccess(allItems); }),
Function.createDelegate(this, this.onQueryFailed));
}

function onQuerySuccess(allItems) {
var listItemInfo = '';
var ListEnumerator = allItems.getEnumerator();
while (ListEnumerator.moveNext()) {
var currentItem = ListEnumerator.get_current();
listItemInfo += '\nID: ' + currentItem.get_id() +
'\nContractType: ' + currentItem.get_item('ContractType');

}

alert(listItemInfo.toString());
}
function onQueryFailed(sender, args) {
alert('Error: ' + args.get_message() + '\n' + args.get_stackTrace());
}

var listId = "{STRING-OF-LISTID-CHARACTERS}";
GetListItemsFromSPList(listId);

});

</SCRIPT>

警报打印出来:身份证号码:1合约类型:[对象对象]

最佳答案

Lookup 字段存储具有两个属性的对象(例如 5;#someValue)

  1. 查找 ID (5)
  2. 查找值 (someValue)

请尝试通过下面提到的方式获取查找值。

var lookupObject = oListItem.get_item('lookupFieldName');
var lookupValue = lookupObject.get_lookupValue();
var lookupId = lookupObject.get_lookupId();

关于javascript - SharePoint JavaScript CSOM : Best way to deal with Lookup fields?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42009997/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com