facebook

Populating Selectlist dynamically

💡
Our Forums Have Moved

For help with installation, bugs reports or feature requests, please head over to our new forums.
Genuitec Community on GitHub

  1. MobiOne Archive
  2.  > 
  3. Getting Help – General
Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #346406 Reply

    billPeach
    Member

    Can you please help me with this
    I have trawled the Blog and cheat lists, using snippets where available, but cannot get this to work I have created a sample attached to demonstrate the problem

    I am trying to populate a select list dynamically with data ( represented with an array) which I successfully can do loading the text into the text field, but it seems that I am not loading the val field. When I try to select the item which loads into a text field I get the val of the original Selectlist rather than the newly loaded one

    I have racked the brain on this one for days now and have just reached a block

    Are you able to point me in the right direction or give me an example or tell me where I am going wrong!!!

    Frustrated

    Bill Peach

    Attachments:
    You must be logged in to view attached files.
    #346407

    Code_A
    Member

    We are in the same boat here as I am struggling with the exact same issue right now! Seems like it shouldn’t be that difficult. See my last 2 posts in this thread viewtopic.php?f=8&t=6752

    I will be watching your post to see if you get this resolved. If I figure something out, I’ll let you know. Sorry, I can’t help.

    #346412

    billPeach
    Member

    Thanks for reassuring me that it may not be my code

    This has been driving me nuts!!

    Any clues out there!!!

    #346548

    Code_A
    Member

    Apparently this issue may be a bug that the dev team is now investigating. See viewtopic.php?f=8&t=6752.

    #346556

    billPeach
    Member

    Thanks Code A

    It is reassuring that I am not a complete coding ‘fruit loop’. I have spent hours on this!

    It is good to know that it is being looked at and looking forward to an ETA for a fix or workaround

    #346563

    Code_A
    Member

    You can see the updated example that the support team posted that makes this work at viewtopic.php?f=8&t=6752.

    Basically, there are 3 lines of code that need added to the function that dynamically builds the list. I have indicated them below as ‘<—NEW!’:

    // Array of items to fill SL
    var items = ["First", "Second", "Third"];
    // Selected item index
    var selItemIndex = 0;
    var $selectionList = $('#m1-sl-filling-list1');
    // Store old items (will remove them later)
    var olditems = $selectionList.children();
    // First of old items is our template
    var templ = $(olditems[0]);
    // "<select>" element
    var $select = $('#' + $selectionList.attr('data-hiddeninputid'));    //<--- NEW!
    $select.empty(); //<--- NEW!
    
    // Do for each data item
    $.each(items, function(i, val) {
        // Clone template
        var clone = templ.clone();
        // Set correct value for "first" class
        clone[(i == 0) ? "addClass" : "removeClass"](m1Design.css('first'));
        // Set correct value for "last" class
        clone[(i == (items.length-1)) ? "addClass" : "removeClass"](m1Design.css('last'));
        // Process selected item
        clone[(i == selItemIndex) ? "addClass" : "removeClass"](m1Design.css('selected'));
        // Set "value" item (it will be set to <select><option VALUE> attribute when doign POST to server)
        clone.attr('data-val', "val" + i);
        // Set text label
        clone.find('label').text(val);
        // We're done, add element to list item
        clone.appendTo(templ.parent());
        
        $select.append("<option value='" + val + "'>" + val + "</option>"); //<--- NEW!
    });
    
    // Remove old items
    olditems.remove();
    // Don't forget to sync internal phoneui state
    phoneui.preprocessDOM(phoneui.getCurrentPageId());

    And then this is how you retrieve the selected value:

    alert("Val: " + $('select[name="list1"] :selected').val())
    #346565

    Hi Code A,

    Thanks for sharing updated example on this thread with the same issue.


    @Bill
    ,

    Feel free to ping us if you need further assistance.

    #346574

    billPeach
    Member

    Thanks Octavio

    Seems to have resolved it

    I will now plug it into the main and check it works

Viewing 8 posts - 1 through 8 (of 8 total)
Reply To: Populating Selectlist dynamically

You must be logged in to post in the forum log in