Keep existing option in select when using filed update

Keep existing option in select when using filed update

5320835@qq.com5320835@qq.com Posts: 15Questions: 5Answers: 0

Hi, I'm using field().update( options ) to adding options for my select filed, there is note mention that existing option will be replaced by update option, so my existing option gone when I editing my record, can you kindly advise me how can I keep existing option while add new update from database

Here is my code

editor.on('open', function (e, mode, action) {
    if (action !== "create") {
        var openMerchantName = editor.get("MerchantName"), openStoreName = editor.get("StoreName"), openMethodTypeJoin = editor.get("MethodTypeJoin");
    }
    var selectMerchant = [], selectStore = [], selectMethodTypeJoin = [];
    $.ajax({
        "url": 'https://mbeta.pw/mocdbapi/RaymSP_GatewayPaymentMerchant_Get',
        "type": "POST",
        "async": true,
        "crossDomain": true,
        "data": {
            "token": SecurityManager.generate(),
            "username": SecurityManager.username
        },
        "success": function (data) {
            data = data.ResultSets[0]
            for (var item in data) {
                if (data[item].table === 'Merchant') {


                    selectMerchant.push({ label: data[item].label, value: data[item].value });
                }
                if (data[item].table === 'Store') {
                    selectStore.push({ label: data[item].label, value: data[item].value });
                }
                if (data[item].table === 'MethodTypeJoin') {
                    selectMethodTypeJoin.push({ label: data[item].label, value: data[item].value });
                }
            }
            editor.field("MerchantName").update(selectMerchant);
            editor.field("StoreName").update(selectStore);
            editor.field("MethodTypeJoin").update(selectMethodTypeJoin);
            editor.field("MerchantName") = openMerchantName;
        }

    })

});

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,819Questions: 1Answers: 10,517 Site admin

    There is no API method to keep the existing options I'm afraid. You aren't the first to ask about this though, so it is something that I will look into adding for 1.6.

    At the moment you would need to store the list of existing options and add them back in.

    Regards,
    Allan

  • 5320835@qq.com5320835@qq.com Posts: 15Questions: 5Answers: 0

    Hi Allan,

    Thanks for your reply, can you give a example to store a existing option and add them back in, Many Thanks.

    Regards,
    Wenbin

  • allanallan Posts: 63,819Questions: 1Answers: 10,517 Site admin
    Answer ✓
    var options = [];
    
    // Get existing options
    $('option', editor.field('mySelect').input()).each( function () {
      options.push( { label: $(this).text(), value: $(this).attr('value') } );
    } );
    
    // Add new options
    options.push( { label: 1, value: 1 } );
    
    // Update options
    editor.field('mySelect').update( options );
    

    should do it!

    Allan

  • allanallan Posts: 63,819Questions: 1Answers: 10,517 Site admin

    Quick update on this. I've added an option to the field().update() method for the select, radio and checkbox field types which will allow values to be appended, as well as retaining the current action (which is the default) of replacing all existing items.

    This will be included in v1.6 which should drop in December.

    Regards,
    Allan

This discussion has been closed.