SharePoint:按日期排序

时间:2015-03-10 15:46:55

标签: javascript sharepoint-2013

SharePoint新手。 我想以反向日期顺序从“页面”中检索项目。

这是我用来从页面获取详细信息的代码。它目前按页面顺序填充列表,但希望通过将最近添加的项目列在列表顶部来对列表进行排序。

var listTitle = "Pages";

function getEvents() {
    var clientContext = new SP.ClientContext('https://int-canada.wrberkley.com/resources');
    //alert("1");
    var oList = clientContext.get_web().get_lists().getByTitle(listTitle); 
    var camlQuery = new SP.CamlQuery()
    camlQuery.set_viewXml("<View></View>");
    namedListItem = oList.getItems(camlQuery);
    clientContext.load(namedListItem);
    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

function onQuerySucceeded(sender, args) {
    var count = 0;
    var count = namedListItem.get_count();
    if (count < 1) {}
    else
    { 
        var ctrl = document.getElementById("Company Resources");
        var listItemTR = "<tr><td>{1}</td><td><a href='{2}'><h2>{3}</h2></a><br />{4}</td></tr>";
        var listItemInfo = "";
        var listItemEnumerator = namedListItem.getEnumerator();
        var CT = 0;
        while (listItemEnumerator.moveNext()) {
            var oListItem = listItemEnumerator.get_current();
            var newsTitle = oListItem.get_item('Title');
            if (newsTitle != 'Home')  //Do not include "default.aspx"
            {
                var newsComment = oListItem.get_item('Comments');
                var newsImageURL = oListItem.get_item('PublishingRollupImage');
                var newsFileRef = oListItem.get_item('FileRef');

                var strResult = listItemTR;         
                strResult = strResult.replace("{1}", newsImageURL);
                strResult = strResult.replace("{2}", newsFileRef);
                strResult = strResult.replace("{3}", newsTitle);
                strResult = strResult.replace("{4}", newsComment);  
                listItemInfo += strResult;
            }
        CT +=1;
        if (CT > 1){
        break;}
        }


        ctrl.parentNode.innerHTML = "<table>" + listItemInfo + "</table>";
    }
}

function onQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

1 个答案:

答案 0 :(得分:0)

我强烈建议您使用网站上的underscore.js样本:

//array of objects
var stooges = [{name: 'moe', age: 40}, {name: 'larry', age: 50}, {name: 'curly', age: 60}];

//underscore in action
_.sortBy(stooges, 'name');

//output
=> [{name: 'curly', age: 60}, {name: 'larry', age: 50}, {name: 'moe', age: 40}];