javascriptjqueryjquery-easyui

Dynamically load data to easyui combobox based on condition


I have a basic Easyui combobox where i need to add dynamic options based on some condition. My Html :

<input id="org_type" name="org_type" class="easyui-combobox" data-options="required: true,valueField:'value',textField:'text',prompt:'Select Org Type'" style="width:100%;" >

Now i need to load some options based on some conditions. for example :

if(level == 1){
   <option value="volvo">Volvo</option>
   <option value="saab">Saab</option>
}else{
   <option value="vw">Volvo</option>
   <option value="audi">Saab</option>
}

Though that's not the right approach i know. I need actually something like this.


Solution

  • Finally i got my own solution. Simply i added these JavaScript code when i need to add options dynamically.

    let options  = [];
    if(level == "1"){
    
    options = [
        {
            text: 'Volvo',
            value: 'volvo'
        },
        {
            text: 'Saab',
            value: 'saab'
        }
    ];
    
    }else if(org_level == "2"){
    options = [
        {
            text: 'Marcedes',
            value: 'marcedes'
        },
        {
            text: 'Scania',
            value: 'scania'
        },
        {
            text: 'BMW',
            value: 'bmw'
        }
    ];
    

    }

    $('#org_type').combobox({
            data: options
    })
    

    And it work's fine for me. Thanks.