Sharepoint学习笔记—ECMAScript对象模型系列-- 7、获取和修改List的Lookup字段
生活随笔
收集整理的這篇文章主要介紹了
Sharepoint学习笔记—ECMAScript对象模型系列-- 7、获取和修改List的Lookup字段
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在前面我們提到了如何使用ECMAscript對象模型來操作普通的List Items,但如果我們操作的List包含有Lookup字段,那么我們又該怎么做呢?
首先參考此文搭建我們本文的測試環境
Sharepoint學習筆記---SPList--創建一個帶有Lookup字段的List
一、對于獲取操作,我們使用如下代碼
<script?type="text/javascript">????function?GetLookupValue()?{
????????var?context?=?new?SP.ClientContext.get_current();
????????var?web?=?context.get_web();
????????var?list?=?web.get_lists().getByTitle('TestSale');
????????var?query?=?SP.CamlQuery.createAllItemsQuery();
????????allItems?=?list.getItems(query);
????????context.load(allItems,?'Include(ProductName,LookupStaffName)');
????????context.executeQueryAsync(Function.createDelegate(this,?this.successGetLookupValue),
???????????????????????????Function.createDelegate(this,?this.failedGetLookupValue));
????}
????function?successGetLookupValue()?{
????????var?TextFiled?=?"";
????????var?ListEnumerator?=?this.allItems.getEnumerator();
????????while?(ListEnumerator.moveNext())?{
????????????var?currentItem?=?ListEnumerator.get_current();
????????????TextFiled?+=?currentItem.get_item('ProductName')?+?'-'?+?currentItem.get_item('LookupStaffName').get_lookupValue()?+?'\n';
????????}
????????alert(TextFiled);
????}
????function?failedGetLookupValue(sender,?args)?{
????????alert("failed.?Message:"?+?args.get_message());
????}
</script>
效果如下:
二、對于修改操作,我們使用如下代碼
<script?type="text/javascript">????function?SetLookupValue()?{
????????var?context?=?new?SP.ClientContext.get_current();
????????var?web?=?context.get_web();
????????var?list?=?web.get_lists().getByTitle('TestSale');
????????var?query?=?SP.CamlQuery.createAllItemsQuery();
????????allItems?=?list.getItems(query);
????????context.load(allItems,?'Include(ProductName,LookupStaffName)');
????????context.executeQueryAsync(Function.createDelegate(this,?this.successSetLookupValue),?Function.createDelegate(this,?this.failedSetLookupValue));
????}
????function?successSetLookupValue()?{
????????var?TextFiled?=?""
????????var?ListEnumerator?=?this.allItems.getEnumerator();
????????while?(ListEnumerator.moveNext())?{
????????????var?currentItem?=?ListEnumerator.get_current();
????????????var?newLookupField?=?new?SP.FieldLookupValue();
????????????
????????????newLookupField.set_lookupId(2)??//Updated?with?some?lookup?value?for?LookupStaffName?column
????????????????????????????????????????????//?Here?is?an?id?of?your?lookup?list?item?'Steve';
????????????currentItem.set_item('LookupStaffName',?newLookupField);
????????????currentItem.update();
????????????//?Call?context.executeQueryAsync?again
????????}
????}
????function?failedSetLookupValue(sender,?args)?{
????????alert("failed.?Message:"?+?args.get_message());
????}
</script>
?效果如下:
?
需要說明的是,在修改中,我們使用到代碼
newLookupField.set_lookupID(2)此代碼中參數的含意就是從LookupItem所綁定的數據源List中去取值,如下圖:
轉載于:https://www.cnblogs.com/wsdj-ITtech/archive/2012/06/06/2418912.html
總結
以上是生活随笔為你收集整理的Sharepoint学习笔记—ECMAScript对象模型系列-- 7、获取和修改List的Lookup字段的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 页面的生命周期
- 下一篇: java怎么来用urlrewrite伪静