使用Javascript获取列表项,我可以更改项目顺序吗?

时间:2019-05-24 13:49:10

标签: javascript list sharepoint-2013

我正在使用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”对其进行排序。

1 个答案:

答案 0 :(得分:1)

尝试设置caml query对数据进行排序。

示例脚本。

select ms.status, count(m.status)
from (select unnest(enum_range(null::mem_status))) as ms(status)
left join mems as m
  on ms.status = m.status
group by ms.status;