我有一个带ListViewByQuery和Button控件的aspx网站。单击按钮后,我想从ListViewByQuery控件中获取选定的项目。我使用了以下代码,但变量“选择”始终为0。
<asp:Content ID="PageHead" ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">
<script type="text/javascript">
$(document).ready(function () {
var button = $("input[id$=SaveButton]");
button.click(function () {
var context = SP.ClientContext.get_current();
var selection = SP.ListOperation.Selection.getSelectedItems(context);
var fileItems = [];
var currentItem;
for (currentItem in selectedItems) {
fileItems.push(currentItem);
context.load(currentItem, 'Id');
}
context.executeQueryAsync(Function.createDelegate(this, function () {
for (var i = 0; i < fileItems.length; i++) {
alert(fileItems[i].get_id());
}
}));
});
});
</script>
</asp:Content>
<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
<div id="pnlDiv">
<SharePoint:ListViewByQuery runat="server" ID="MyListView" Enabled="True" />
<asp:Button ID="SaveButton" runat="server" Text="Button" />
</div>
</asp:Content>
<asp:Content ID="PageTitle" ContentPlaceHolderID="PlaceHolderPageTitle" runat="server">
Application Page
</asp:Content>
<asp:Content ID="PageTitleInTitleArea" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea"
runat="server">
My Application Page
</asp:Content>
其他事实:
-从文件后面的代码中获取列表
-从DL编辑表单后,事件接收器(“ SPEventReceiverStatus.CancelWithRedirectUrl”)调用aspx站点。
最佳答案
方法ListModule.Util.getSelectedItemsDict
(core.js
)可用于检索ListViewByQuery Control中的选定项目。
范例1。
该示例演示如何从“工作流历史记录”列表(wrkstat.aspx
)中检索选定的项目。
function getSelectedItemsInfo(ctxNumber)
{
var listCtx = GetListContextFromContextNumber(ctxNumber);
return ListModule.Util.getSelectedItemsDict(listCtx);
}
//Note: Context Number for Workflow History list on page wrkstat.aspx is 2, since it contains 2 ListViewByQuery controls on the page. If page contains only single ListViewByQuery control then Context Number is 1
var selectedItems = getSelectedItemsInfo(2);
printItemsInfo(selectedItems);
function printItemsInfo(items)
{
if(!items)
return;
for (var key in items){
var item = items[key];
var itemId = item.id;
var itemType = item.fsObjType;
console.log(String.format('Selected List Item Id: {0}',itemId));
}
}
注意:要识别
ListViewByQuery
控件,上下文编号必须被指定。对于
wrkstat.aspx
页上的“工作流历史记录”列表,2,因为它在页面上包含2个
ListViewByQuery
控件。如果页面仅包含单个
ListViewByQuery
控件,则上下文编号为1个
示例2
以下示例演示如何从“工作流历史记录”列表(
wrkstat.aspx
)中的选定项目中检索ListItem集合。function getSelectedItems(ctxNumber, success,failure)
{
var listCtx = GetListContextFromContextNumber(ctxNumber);
var selectedItems = ListModule.Util.getSelectedItemsDict(listCtx);
if(!selectedItems) {
success(null);
return;
}
var context = SP.ClientContext.get_current();
var list = context.get_web().get_lists().getByTitle(listCtx.ListTitle);
var listItems = [];
for (var key in selectedItems){
var item = selectedItems[key];
var listItem = list.getItemById(item.id);
context.load(listItem);
listItems.push(listItem);
}
context.executeQueryAsync(
function () {
success(listItems);
},
failure);
}
//Usage
getSelectedItems(2,printWorkflowHistory,logError);
function printWorkflowHistory(items)
{
if(!items)
return;
for (var i = 0; i < items.length;i++){
var item = items[i];
console.log(String.format('Selected List Item Id: {0}',item.get_item('Description')));
}
}
function logError(error){
console.log(JSON.stringify(error));
}
关于javascript - Sharepoint-从ListViewByQuery控件中获取选定的项目-ASPX网站,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25970161/