排序数组超链接
Sorting Array hyperlinks
我试图让这段代码根据标题进行排序,但显示带有超链接的标题。到目前为止,代码排序正确,但超链接未按正确的标题显示。它似乎是按标题的字母顺序排列列表链接,然后按标题排列子站点链接。
我要:
第一个子站点(www.test.com/firstsubsite)
Google(www.google.com) //<-在列表中
最后一个子站点(www.test.com/lastSubsite)
Yahoo(www.yahoo.com) //<- 在列表中
目前我得到:
第一个子站点 (www.google.com) //<-在列表中
Google(www.yahoo.com) //<- 在列表中
最后一个子站点(www.test.com/firstsubsite)
雅虎(www.test.com/lastSubsite)
function GetItems() {
var names = [];
var link = [];
$().SPServices({
operation: "GetListItems",
async: true,
listName: "GatheredSites",
CAMLViewFields: "<ViewFields><FieldRef Name='Title' /><FieldRef Name='Link_x0020_Url' /></ViewFields>",
completefunc: function(xData, Status) {
$(xData.responseXML).SPFilterNode("z:row").each(function() {
var url = $(this).attr("ows_Link_x0020_Url").split(",")[0];
var name = ($(this).attr("ows_Title"));
names.push(name);
link.push(url);
});
$().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"));
link.push($node.attr("Url"));
});
names.sort();
var output = $("#divDisplay");
for (var i = 0, len = names.length; i < len; i++) {
output.append("<li><a href='" + link[i] + "'>" + names[i] + "</li>");
}
}
});
}
});
}
一旦对names
数组进行排序,就无法匹配到links
数组中的相应索引。
您可以在 xml 的每次迭代中创建一个同时具有名称和 link 的对象,并将该对象放入一个数组中。
然后按名称对单个对象数组进行排序属性
$(xData.responseXML).find("Webs > Web").each(function () {
var $node = $(this);
// single object to store all needed properties from xml
var item = {
name: $node.attr("Title"),
link: $node.attr("Url")
}
// push object to array
names.push(item);
// link.push($node.attr("Url")); - remove links array, no longer needed
});
// sort array by name property
names.sort(function (a, b) {
return a.name > b.name
});
var output = $("#divDisplay");
for (var i = 0, len = names.length; i < len; i++) {
// note properties used for link and name
output.append("<li><a href='" + names[i].link + "'>" + names[i].name + "</li>");
}
我试图让这段代码根据标题进行排序,但显示带有超链接的标题。到目前为止,代码排序正确,但超链接未按正确的标题显示。它似乎是按标题的字母顺序排列列表链接,然后按标题排列子站点链接。
我要:
第一个子站点(www.test.com/firstsubsite)
Google(www.google.com) //<-在列表中
最后一个子站点(www.test.com/lastSubsite)
Yahoo(www.yahoo.com) //<- 在列表中
目前我得到:
第一个子站点 (www.google.com) //<-在列表中
Google(www.yahoo.com) //<- 在列表中
最后一个子站点(www.test.com/firstsubsite)
雅虎(www.test.com/lastSubsite)
function GetItems() {
var names = [];
var link = [];
$().SPServices({
operation: "GetListItems",
async: true,
listName: "GatheredSites",
CAMLViewFields: "<ViewFields><FieldRef Name='Title' /><FieldRef Name='Link_x0020_Url' /></ViewFields>",
completefunc: function(xData, Status) {
$(xData.responseXML).SPFilterNode("z:row").each(function() {
var url = $(this).attr("ows_Link_x0020_Url").split(",")[0];
var name = ($(this).attr("ows_Title"));
names.push(name);
link.push(url);
});
$().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"));
link.push($node.attr("Url"));
});
names.sort();
var output = $("#divDisplay");
for (var i = 0, len = names.length; i < len; i++) {
output.append("<li><a href='" + link[i] + "'>" + names[i] + "</li>");
}
}
});
}
});
}
一旦对names
数组进行排序,就无法匹配到links
数组中的相应索引。
您可以在 xml 的每次迭代中创建一个同时具有名称和 link 的对象,并将该对象放入一个数组中。
然后按名称对单个对象数组进行排序属性
$(xData.responseXML).find("Webs > Web").each(function () {
var $node = $(this);
// single object to store all needed properties from xml
var item = {
name: $node.attr("Title"),
link: $node.attr("Url")
}
// push object to array
names.push(item);
// link.push($node.attr("Url")); - remove links array, no longer needed
});
// sort array by name property
names.sort(function (a, b) {
return a.name > b.name
});
var output = $("#divDisplay");
for (var i = 0, len = names.length; i < len; i++) {
// note properties used for link and name
output.append("<li><a href='" + names[i].link + "'>" + names[i].name + "</li>");
}