我正在实现一个包含共享点项目的分页系统。我想要从 10 到 20 获取项目的 caml 查询。
我尝试使用运算符在一个 ID 和另一个 ID 之间选择项目,但我的项目 ID 发生了变化,所以我无法使用它们。参数 RowLimit 可以限制我们得到的行数,但我正在寻找"StartRow"或类似的东西。
我正在寻找这样的查询:
<View><StartRow>10</StartRow><RowLimit>10</RowLimit></View>
但是参数 StartRow 似乎不存在。
是否有像 StartRow 这样的现有参数?
我们可以使用 CAMLQuery set_listItemCollectionPosition方法设置列表项集合位置,以下示例代码供您参考。
var context,
web,
spItems,
position,
nextPagingInfo,
previousPagingInfo,
listName = 'ContactsList',
pageIndex = 1, // default page index value
pageSize = 4, // default page size value
list,
camlQuery;
// This code runs when the DOM is ready and creates a context object which is needed to use the SharePoint object model
$(document).ready(function () {
context = SP.ClientContext.get_current();
list = context.get_web().get_lists().getByTitle(listName);
camlQuery = new SP.CamlQuery();
$("#btnNext").click(function () {
pageIndex = pageIndex + 1;
if (nextPagingInfo) {
position = new SP.ListItemCollectionPosition();
position.set_pagingInfo(nextPagingInfo);
}
else {
position = null;
}
GetListItems();
});
$("#btnBack").click(function () {
pageIndex = pageIndex - 1;
position = new SP.ListItemCollectionPosition();
position.set_pagingInfo(previousPagingInfo);
GetListItems();
});
GetListItems();
});
function GetListItems() {
//Set the next or back list items collection position
//First time the position will be null
camlQuery.set_listItemCollectionPosition(position);
// Create a CAML view that retrieves all contacts items with assigne RowLimit value to the query
camlQuery.set_viewXml("<View>" +
"<ViewFields>" +
"<FieldRef Name='FirstName'/>" +
"<FieldRef Name='Title'/>" +
"<FieldRef Name='Company'/>" +
"</ViewFields>" +
"<RowLimit>" + pageSize + "</RowLimit></View>");
spItems = list.getItems(camlQuery);
context.load(spItems);
context.executeQueryAsync(
Function.createDelegate(this, onSuccess),
Function.createDelegate(this, onFail)
);
}
// This function is executed if the above OM call is successful
// This function render the returns items to html table
function onSuccess() {
var listEnumerator = spItems.getEnumerator();
var items = [];
var item;
while (listEnumerator.moveNext()) {
item = listEnumerator.get_current();
items.push("<td>" + item.get_item('FirstName') + "</td><td>" + item.get_item('Title') + "</td><td>" + item.get_item('Company') + "</td>");
}
var content = "<table><tr><th>First Name</th><th>Last Name</th><th>Company</th></tr><tr>"
+ items.join("</tr><tr>") + "</tr></table>";
$('#content').html(content);
managePagerControl();
}
function managePagerControl() {
if (spItems.get_listItemCollectionPosition()) {
nextPagingInfo = spItems.get_listItemCollectionPosition().get_pagingInfo();
} else {
nextPagingInfo = null;
}
//The following code line shall add page information between the next and back buttons
$("#pageInfo").html((((pageIndex - 1) * pageSize) + 1) + " - " + ((pageIndex * pageSize) - (pageSize - spItems.get_count())));
previousPagingInfo = "PagedPrev=TRUE&Paged=TRUE&p_ID=" + spItems.itemAt(0).get_item('ID');
if (pageIndex <= 1) {
$("#btnBack").attr('disabled', 'disabled');
}
else {
$("#btnBack").removeAttr('disabled');
}
if (nextPagingInfo) {
$("#btnNext").removeAttr('disabled');
}
else {
$("#btnNext").attr('disabled', 'disabled');
}
}
更多信息:
SharePoint JSOM 列表分页(分页(
SharePoint2013:使用 SharePoint 客户端对象模型进行分页