0

I m newbie in jquery space and hope to learn much through Stackoverflow.

I have a Sharepoint 2010 list data item which has Choice data type allowing a user to checkbox more than one value. I m using SPService to get the list items. Below is the code:

    $().SPServices({
                  webURL: 'http://abc.def.com/Products',
                  operation: 'GetListItems',
                  listName: listName,
                  async: false,
                  CAMLQuery: query,
                  CAMLViewFields: fields,
                  CAMLRowLimit: 11,

                  completefunc:
                  function (xData, Status) 
    {        $(xData.responseXML).find("[nodeName=z:row]").each(function () 

    {
           $("#ProdList").val("-1");//Pre-filled with selected val.
           var myProd= $(this).attr("ows_Prod"); //displays in this format ;#myProd1;#myProd2;#

          //Here 1. i want to store myProd as an array.
                 2. eliminate ;#
                 3. store final array elements as myProd1, myProd2
                 4. Display myProd1
                            myProd2
                                    in the dropdown list.
           //


           $("#ProdList").append('<option>' + myProd+ '</option>');
      });   
    }

     });      

     });

    <body>
    <select class="Dropdowns" id="ProdList">
    <option value="-1">My ProdLists</option>
    </select>
    </body>
  Any pointers? Thanks.          
0

1 Answer 1

1

It's a bit hard to figure out what the specifics are of your question. I'm guessing that the comments in your code are describing the question. Next time, I'd suggest you be a lot more specific with the details of your question in your text, not in the code. But, given that, here's my guess at what you're trying to do.

You have a string that looks like this:

";#myProd1;#myProd2;#"

And, you'd like to get myProd1 and myProd2 into an array and then from that array added to a drop-down list.

// get product list
var myProd = $(this).attr("ows_Prod");
// clean up the ends
myProd = myProd.replace(/^;#|;#$/g, "");
// split into pieces
var myProdArray = myProd.split(";#");
// add each piece as an option to the drop-down
for (var i = 0; i < myProdArray.length; i++) {
    $("#ProdList").append('<option>' + myProdArray[i] + '</option>')
}

You can sort the array in dictionary order with:

myProdArray.sort();

You can remove dups from a sorted array with this:

var noDupArray = [];
noDupArray.push(myProdArray[0]);
for (var i = 1; i < myProdArray.length; i++) {
    if (myProdArray[i] != myProdArray[i-1]) {
        noDupArray.push(myProdArray[i]);
    }
}

So, if you wanted to both sort and remove dups, you could use this code:

var myProd = $(this).attr("ows_Prod");
myProd = myProd.replace(/^;#|;#$/g, "");
var myProdArray = myProd.split(";#");
// sort
myProdArray.sort();
// remove dups
var noDupArray = [];
noDupArray.push(myProdArray[0]);
for (var i = 1; i < myProdArray.length; i++) {
    if (myProdArray[i] != myProdArray[i-1]) {
        noDupArray.push(myProdArray[i]);
    }
}
// add each piece as an option to the drop-down
for (i = 0; i < noDupArray.length; i++) {
    $("#ProdList").append('<option>' + noDupArray[i] + '</option>')
}
Sign up to request clarification or add additional context in comments.

17 Comments

Thanks a bunch jfriend00. It worked. I will definitely try to be more specific in describing a question next time. Thanks again for your help. :)
How do I make sure that I don't have duplicates in my dropdown list for the above code? I also want my myProdArray to be sorted (ASC). Any pointers? Thanks.
Sorting and dedupping added to my answer.
I have two list items and they have same values for myProd.For eg. let's say SharePoint2010 I have to make sure I only put SharePoint 2010 once in the dropdown list. With the code above, I could not eliminate the duplicacy. Any guidance please? Thanks a lot for prompt response.
It seems to work fine here in this sample jsfiddle.net/jfriend00/yCauH if implemented as above. There are multiple dups in the attribute, but no dups are put in the drop-down. If you want further help, you'll have to show your actual implementation.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.