How to remove select option using Javascript

We have a select field and the available options are depending: in edit view I use all, in create view they must be filtered.

I tried different ways but nothing works. For example to filter the list before rendering. I have no idea to access the list.

({ extendsFrom: 'CreateActionsView',

    initialize: function (options) {      

        this._super('initialize', [options]);    

        this._filterRtQueue();

    },

    _filterRtQueue : function() {

        //TODO remove some entries from select list

        //console.info(this.app.metadata.getStrings('rt_queue_list'));     

    }  

});

Or after rendering. I can't manipulate the HTML this way, the result is undefined:

({ extendsFrom: 'CreateActionsView',

    initialize: function (options) {       

        this._super('initialize', [options]);     

        //this._filterRtQueue();

    },

    _filterRtQueue : function() {

        console.log(this.$('.select2-result'));

    },

   

    _render : function() {

        this._super("_render");

        this._filterRtQueue();

    } 

});

Any idea?

  • Hi Harald Kampen

    If you want to remove options from dropdown let say wanted to remove value that had a text name with "ramana" in it.

    Try this code:

    ({
        extendsFrom: 'CreateActionsView',
        initialize: function (options) {
           console.log('initalized CreateActionView');
           this._super('initialize', [options]);
           this.hideDropdownValues();
        },
       hideDropdownValues: function(){
         // hide dropdown items from country classification dropdown
         var territory_list = app.lang.getAppListStrings('country_classification_list');
         Object.keys(territory_list).forEach(function(key) {
            var check = territory_list[key].match(/ramana/);
            if(territory_list[key].match(/ramana/)){
               delete territory_list[key];
            }
         });
         this.model.fields['country_classification'].options = territory_list;
       },
       _dispose: function() {
          this._super('_dispose');
       }
    })
    

    Hope this Helps

    Best Regards

    S Ramana Raju

  • Thank you! Exacly what I'm looking for. Now I can replace the list with another and manage both in the frontend of Sugar.

    ({ extendsFrom: 'CreateActionsView',
        initialize: function (options) {       
            this._super('initialize', [options]);     
            this._filterRtQueue();
        },
        _filterRtQueue : function() {
            this.model.fields['queue'].options = app.lang.getAppListStrings('rt_queue_short_list'); 
        },
        _dispose: function() { 
            this._super('_dispose'); 
        }     
    });
    
  • Hi Harald

    I believe it would be better to manage it through dependencies

    Cheers

    André Lopes
    Lampada Global
    Skype: andre.lampada
  • Hi André, I found dependencies but no documentations or examples. In all examples I found the trigger values uses equal/(). In the two cases equal() was not what I needed.