2

I am using following code to get Sp List Items and display them

$().SPServices({

            operation: 'GetListItems',
            async: false,
            webURL: '/',
            listName: 'Sample',

            completefunc: function (xData, Status) {

                $(xData.responseXML).SPFilterNode('z:row').each(function () {
                    var groupname;
                    groupname = $(this).attr("ows_Group");
                    if (groupname == $(this).attr("ows_Group")) {
                        var ibcresultList = "<div class='shortcutsItem'><a href='" + $(this).attr("ows_URL") + "'>" + $(this).attr("ows_Header") + "</a></div>";
                        $("#asSamTab").append(ibcresultList);
                    }
                    else {
                        var ibcgroupchange = "<div class='shortcutsItem LobWhiteSpaceleft'></div><div class='shortcutsItem LobWhiteSpaceRight'></div>";
                        $("#asSamTab").append(ibcgroupchange);
                    }
                });
            }

my list items are follows

Title URL Group
row1  //   A
row2  //   A 
row3  //   A
row4  //   B
row5  //   B
row6  //   A

My ouptput should be as follows

row1   row2
row3   row6
----   ----
row4   row5

can anyone help how to achieve this

2

It looks like you need to add this in before your completefunc to force ordering by Group then Title in the results returned, if not you need to do your own sorting after getting the results back:

CAMLQuery: "<Query><OrderBy><FieldRef Name='Group' /><FieldRef Name='Title' /></OrderBy></Query>",

Edit: I have verified against other lists this will return the data in the correct output. Use the browser development tools to see that in fact the data is returned in the correct order.

However looking at your code, you'll never get the results you are looking for even with ordering the CAML query above. In your completefunc, you are creating a new variable called groupname. You are setting the current item groupname to this variable. Then you are evaluating if this value equals itself, which will always return true. You are never going to hit your else function with this code.

Edit2: I would do it like this, (untested):

$().SPServices({
    operation: 'GetListItems',
    async: false,
    webURL: '/',
    listName: 'Sample',
    CAMLQuery: "<Query><OrderBy><FieldRef Name='Group' /><FieldRef Name='Title' /></OrderBy></Query>",
    completefunc: function (xData, Status) {
        //create a variable to evaluate against
        var groupname = "";
        $(xData.responseXML).SPFilterNode('z:row').each(function () {
            //create a variable holding the current group
            var thisGroup = $(this).attr("ows_Group");
            if (groupname === thisGroup || groupname === "") {
                var ibcresultList = "<div class='shortcutsItem'><a href='" + $(this).attr("ows_URL") + "'>" + $(this).attr("ows_Header") + "</a></div>";
                $("#asSamTab").append(ibcresultList);
            }
            else {
                var ibcgroupchange = "<div class='shortcutsItem LobWhiteSpaceleft'></div><div class='shortcutsItem LobWhiteSpaceRight'></div>";
                $("#asSamTab").append(ibcgroupchange);
            }
            //finally update the groupname for the next pass through the function
            groupname = thisGroup;
        });
    }
});
12
  • its not working not sure between Title is working but group order is not working
    – raky
    Jul 7 '14 at 22:27
  • What is not working with it? The results should be coming back ordered by group then title. Jul 7 '14 at 22:33
  • i am seeing title are in order but group is not in order row1 to row6 are in order irrespective of group
    – raky
    Jul 7 '14 at 22:44
  • I've verified on several lists that this will return things in proper order, it looks like you also have bad logic with this:var groupname; groupname = $(this).attr("ows_Group"); if (groupname == $(this).attr("ows_Group")) groupname is always going to equal groupname because you are setting the value to the current item Group value. This is likely the source of your problem. Jul 7 '14 at 23:00
  • can you try by removing title and order by group
    – raky
    Jul 7 '14 at 23:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.