tags:

views:

103

answers:

3

I have the jquery code below that returns a list of ID's like this 1|2|3|4|5|6|7|8|9 and so on for every List item I have on the page, I also have a dropdown selection box named topfriendNumber. I need to take the value from topfriendNumber dropdown box and make sure my list of ID's does not exceed it.

So if the dropdown is selected as 4 then it should only allow 4 numbers 1|2|3|4

Based on this code below can you help?

This could probably be added into the bottom part where it is already using the number from the dropdown box

<select name="topfriendNumber" id="topfriendNumber">
    <option value="3">3</option>
    <option value="6" selected="selected">6</option>
    <option value="9">9</option>
    <option value="12">12</option>
</select>

function saveOrder() {
    var serialStr = "";
    $("#topfriends li").each(function (i, elm) {
        serialStr += (i > 0 ? "|" : "") + $(elm).attr("friendID");
    });
    // alert(serialStr);   //1|2|3|4|5|6|7|8|9
    $('select[name=topfriendNumber]').change();
    $.ajax({
        url: "exampe.php/SaveListOrder",
        data: '{"ids":"' + serialStr + '"}',
        dataType: "json",
        type: "POST",
        contentType: "application/json; charset=utf-8"
    });
};


//changes the background colors depending on the amount selected from the dropdown list
$('select[name=topfriendNumber]').change(function() {
    var val = $(this).val();
    //reset style
    $('ul#topfriends > li').css("background-color", "");
    //apply to all LIs before value of select
    $('ul#topfriends > li:lt(' + val + ')').css("background-color", "red");
}).change();
A: 
var newSerialStr = serialStr.split('|').splice(0,topfriendNumber).join('|');
Nick Franceschina
+1  A: 

May I suggest using an array to store the order first:

function saveOrder() {
    var serials = []; // create an empty array
    $("#topfriends li").each(function (i, elm) {
        serials.push( $(elm).attr("friendID") ); // add Friend ID
    });

    // quick way to get your old format string
    // alert(serials.join("|"));   //returns: 1|2|3|4|5|6|7|8|9

    // assuming this is where that "number" is. 
    var $numberSelect = $('select[name=topfriendNumber]');

    var numOfFriends = parseInt($numberSelect.val(),10); // get the value
    if (serials.length<numOfFriends) numOfFriends = serials.length;

    // get an array from 0, numofFriends in length.
    var serialStr = serials.slice(0, numOfFriends).join("|");
    // 1|2|3|4 if numOfFriends is 4.

    // ....
};

If you are instead starting from a string:

 function getListItems(listStr, length) {
   var parts = listStr.split("|"); // split the string up into an array.
   if (!length || (parts.length<length)) return parts.join("|");
   return parts.splice(0,length).join("|");
 }

 alert(getListItems("1|2|3|4|5|6|7|8|9", 4)); // 1|2|3|4

VoteyDisciple's answer brings up a good point - you dont really need all the friend IDs.

function saveOrder() {
    // assuming this is where that "number" is. 
    var $numberSelect = $('select[name=topfriendNumber]');
    var numOfFriends = parseInt($numberSelect.val(),10); // get the value

    var serials = []; // create an empty array
    $("#topfriends li").slice(0,numOfFriends).each(function (i, elm) {
        serials.push( $(elm).attr("friendID") ); // add Friend ID
    });

    var serialStr = serials.join("|");
    //....
 }
gnarf
that works great, thank you
jasondavis
+3  A: 

Why not stop short of that, and not even generate the list beyond what you need?

var needed = $('select[name=topfriendNumber]').val();
$("#topfriends li").each(function (i, elm) {
    serialStr += (i > 0 ? "|" : "") + $(elm).attr("friendID");
    if (i > needed) {
        return false;
    }
});
VoteyDisciple
`$("#topfriends li").slice(0,needed).each(...)` safer. And does returning false from each shortcut it? I never knew ;)
gnarf
+1 - This makes much sense.
karim79
The only complaint I would have is the lack of curly braces after the if statement - a classic pretense for a nasty bug to accidentally get written in, I will edit.
karim79
thanks to all of you
jasondavis