JasonDavis
JasonDavis

Reputation: 48933

Truncate a string of numbers to a certain length

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 IDs 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>

<script>
    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();
</script>

Upvotes: 1

Views: 634

Answers (3)

Nicholas Franceschina
Nicholas Franceschina

Reputation: 6147

var newSerialStr = serialStr.split('|').splice(0,topfriendNumber).join('|');

Upvotes: 0

VoteyDisciple
VoteyDisciple

Reputation: 37803

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;
    }
});

Upvotes: 3

gnarf
gnarf

Reputation: 106332

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

Upvotes: 1

Related Questions