I am new to writing javascript, I want to print out the items in a list as well as subsites in alphabetical order. How can I print this combined output in alphabetical order ? Current output is displaying list items in ABC order then subsites in ABC order.
$(document).ready(function() {
GetItems();
});
function GetItems() {
$().SPServices({
operation: "GetListItems",
async: true,
listName: "GatheredSites",
CAMLViewFields: "<ViewFields><FieldRef Name='Title' /></ViewFields>",
CAMLQuery: "<Query><OrderBy><FieldRef Name='Title' Ascending='True' /></OrderBy></Query>",
completefunc: function(xData, Status) {
if (Status == 'success') {
$(xData.responseXML).SPFilterNode("z:row").each(function() {
var name = ($(this).attr("ows_Title"));
$("#divItems").append("<li>" + name + "</li>");
});
Subsites();
} else {
jQuery("#displayItems").append("<div>Empty</div>");
}
}
});
}
function Subsites() {
$().SPServices({
operation: "GetWebCollection",
webURL: "*url*",
async: true,
completefunc: function(xData, Status) {
$(xData.responseXML).find("Webs > Web").each(function() {
var $node = $(this);
var liHtml = "<li> <a href='" + $node.attr("Url") + "' >" + $node.attr("Title") + "</a> </li>";
$("#displayItems").append(liHtml);
});
}
});
}
< div id = "displayItems" >
< /div>
You can save all the list and subsite names into the same array, then sort the array (using Array.sort()
) before looping through it to print the results.
function GetItems() {
var names = [];
$().SPServices({
operation: "GetListItems",
async: true,
listName: "GatheredSites",
CAMLViewFields: "<ViewFields><FieldRef Name='Title' /></ViewFields>",
CAMLQuery: "<Query><OrderBy><FieldRef Name='Title' Ascending='True' /></OrderBy></Query>",
completefunc: function (xData, Status) {
if (Status == 'success') {
$(xData.responseXML).SPFilterNode("z:row").each(function () {
var name = ($(this).attr("ows_Title"));
names.push(name);
});
$().SPServices({
operation: "GetWebCollection",
webURL: "*url*",
async: true,
completefunc: function (xData, Status) {
$(xData.responseXML).find("Webs > Web").each(function () {
var $node = $(this);
names.push($node.attr("Title"));
});
names.sort();
var output = $("#divItems");
for (var i = 0, len = names.length; i < len; i++) {
output.append("<li>" + names[i] + "</li>");
}
}
});
} else {
jQuery("#displayItems").append("<div>Empty</div>");
}
}
});
}