使用 Javascript 获取列表项,我可以更改项目顺序吗?
Using Javascript to get list items, can I change item order?
我正在使用 JavaScript 获取列表项并通过 html 和 CSS 自定义外观,但是它们按 ID 号自动排序,我需要按自定义排序字段,'EventDate'.
我无法通过搜索找到任何东西。我试过更改默认列表视图排序顺序等,但到目前为止没有任何效果。
<script type="text/javascript" src="//ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js"></script>
<script type="text/javascript" src="_layouts/15/sp.runtime.js"></script>
<script type="text/javascript" src="_layouts/15/sp.js"></script>
<script type="text/javascript" src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<script type="text/javascript">
$(function() {
GetProducts();
GetNews();
});
function GetProducts() {
var clientContext = new SP.ClientContext.get_current();
var oList = clientContext.get_web().get_lists().getByTitle('HRKPI');
this.collListItem = oList.getItems("");
clientContext.load(collListItem);
clientContext.executeQueryAsync(
Function.createDelegate(this, this.onQuerySucceeded),
Function.createDelegate(this, this.onQueryFailed)
);
}
function GetNews() {
var nclientContext = new SP.ClientContext.get_current();
var nList = nclientContext.get_web().get_lists().getByTitle('NewsInfo');
this.ncollListItem = nList.getItems("");
nclientContext.load(ncollListItem);
nclientContext.executeQueryAsync(
Function.createDelegate(this, this.onQuerySucceeded),
Function.createDelegate(this, this.onQueryFailed)
);
}
function onQuerySucceeded(sender, args) {
var listItemEnum = collListItem.getEnumerator();
var nlistItemEnum = ncollListItem.getEnumerator();
var ProductInfo = '<div>';
var NewsInfoHTML = '<div>';
while (listItemEnum.moveNext()) {
var oListItem = listItemEnum.get_current();
ProductInfo += '<div id="pParentSquareKPI"> <p id="pKPIName">' +
oListItem.get_item('Name') + '</p>' +
'<p id="pKPIValue">' + oListItem.get_item('Value') + '</p> </div>';
}
while (nlistItemEnum.moveNext()) {
var nListItem = nlistItemEnum.get_current();
NewsInfoHTML += '<table style="width:97%;text-align:center;color:#3C4F66;border: 1px solid #b1b8c1;margin-bottom:15px;"> <col width="95"> <tr style="margin-bottom:20px;"> <td style="background-color:#3C4F66;color:white;border-right: 1px solid #b1b8c1;font-size:20px;">' +
nListItem.get_item('EventMonth') +
'</td> <td rowspan="3" style="text-align:left;padding:10px;background-color:#f1f1f1;">' +
nListItem.get_item('News') +
'</td> </tr> <tr> <td style="border-right: 1px solid #b1b8c1;font-size:38px;">' +
nListItem.get_item('EventDay') +
'</td> </tr> <tr> <td style="background-color:#f2f2f2;border-right: 1px solid #b1b8c1;">' +
nListItem.get_item('EventYear') + '</td> </tr> </table>';
}
ProductInfo += '</div>';
$('#products').html(ProductInfo);
NewsInfoHTML += '</div>';
$('#news').html(NewsInfoHTML);
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
</script>
它根据需要输出项目,但它们是按 ID 排序的,这是不可编辑的。我需要按自定义列 'EventDate'.
对其进行排序
尝试设置 caml query 对数据进行排序。
示例脚本。
var nList = nclientContext.get_web().get_lists().getByTitle('NewsInfo');
var query = new SP.CamlQuery();
query.set_viewXml('<View><Query><OrderBy><FieldRef Name="EventDate" Ascending="FALSE"/></OrderBy></Query></View>');
this.ncollListItem = nList.getItems(query);
我正在使用 JavaScript 获取列表项并通过 html 和 CSS 自定义外观,但是它们按 ID 号自动排序,我需要按自定义排序字段,'EventDate'.
我无法通过搜索找到任何东西。我试过更改默认列表视图排序顺序等,但到目前为止没有任何效果。
<script type="text/javascript" src="//ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js"></script>
<script type="text/javascript" src="_layouts/15/sp.runtime.js"></script>
<script type="text/javascript" src="_layouts/15/sp.js"></script>
<script type="text/javascript" src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<script type="text/javascript">
$(function() {
GetProducts();
GetNews();
});
function GetProducts() {
var clientContext = new SP.ClientContext.get_current();
var oList = clientContext.get_web().get_lists().getByTitle('HRKPI');
this.collListItem = oList.getItems("");
clientContext.load(collListItem);
clientContext.executeQueryAsync(
Function.createDelegate(this, this.onQuerySucceeded),
Function.createDelegate(this, this.onQueryFailed)
);
}
function GetNews() {
var nclientContext = new SP.ClientContext.get_current();
var nList = nclientContext.get_web().get_lists().getByTitle('NewsInfo');
this.ncollListItem = nList.getItems("");
nclientContext.load(ncollListItem);
nclientContext.executeQueryAsync(
Function.createDelegate(this, this.onQuerySucceeded),
Function.createDelegate(this, this.onQueryFailed)
);
}
function onQuerySucceeded(sender, args) {
var listItemEnum = collListItem.getEnumerator();
var nlistItemEnum = ncollListItem.getEnumerator();
var ProductInfo = '<div>';
var NewsInfoHTML = '<div>';
while (listItemEnum.moveNext()) {
var oListItem = listItemEnum.get_current();
ProductInfo += '<div id="pParentSquareKPI"> <p id="pKPIName">' +
oListItem.get_item('Name') + '</p>' +
'<p id="pKPIValue">' + oListItem.get_item('Value') + '</p> </div>';
}
while (nlistItemEnum.moveNext()) {
var nListItem = nlistItemEnum.get_current();
NewsInfoHTML += '<table style="width:97%;text-align:center;color:#3C4F66;border: 1px solid #b1b8c1;margin-bottom:15px;"> <col width="95"> <tr style="margin-bottom:20px;"> <td style="background-color:#3C4F66;color:white;border-right: 1px solid #b1b8c1;font-size:20px;">' +
nListItem.get_item('EventMonth') +
'</td> <td rowspan="3" style="text-align:left;padding:10px;background-color:#f1f1f1;">' +
nListItem.get_item('News') +
'</td> </tr> <tr> <td style="border-right: 1px solid #b1b8c1;font-size:38px;">' +
nListItem.get_item('EventDay') +
'</td> </tr> <tr> <td style="background-color:#f2f2f2;border-right: 1px solid #b1b8c1;">' +
nListItem.get_item('EventYear') + '</td> </tr> </table>';
}
ProductInfo += '</div>';
$('#products').html(ProductInfo);
NewsInfoHTML += '</div>';
$('#news').html(NewsInfoHTML);
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
</script>
它根据需要输出项目,但它们是按 ID 排序的,这是不可编辑的。我需要按自定义列 'EventDate'.
对其进行排序尝试设置 caml query 对数据进行排序。
示例脚本。
var nList = nclientContext.get_web().get_lists().getByTitle('NewsInfo');
var query = new SP.CamlQuery();
query.set_viewXml('<View><Query><OrderBy><FieldRef Name="EventDate" Ascending="FALSE"/></OrderBy></Query></View>');
this.ncollListItem = nList.getItems(query);