【问题标题】:Sorting Array hyperlinks排序数组超链接
【发布时间】:2015-10-14 14:44:03
【问题描述】:

我试图让此代码根据标题排序,但显示带有超链接的标题。到目前为止,代码排序正确,但超链接并没有按正确的标题显示。似乎是按标题的字母顺序排列列表链接,然后按标题的顺序排列子站点链接。

我想要:
第一个子站点(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>");
                    }
                }
            });
        }
    });
}

【问题讨论】:

    标签: javascript arrays sorting sharepoint spservices


    【解决方案1】:

    一旦对names数组进行排序,就无法匹配links数组中的对应索引。

    您可以在 xml 的每次迭代中创建一个同时具有名称和链接的对象,并将该对象推送到一个数组中。

    然后按名称属性对单个对象数组进行排序

    $(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>");
    }
    

    【讨论】:

      猜你喜欢
      • 2018-09-13
      • 2012-04-25
      • 2019-12-17
      • 2021-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-23
      • 1970-01-01
      相关资源
      最近更新 更多