Handling options injection in “select” element by Ext.js

Handling options injection in “select” element by Ext.js

So you would like to make dynamic dropdown list using Ext.js and you want it to be browser compatible, including IE browsers. You could use innerHtml to inject options in your “select” element of a dropdown list. Which looks like:

Ext.Ajax.request({
url: '< ?php someurl ?>',
method: 'POST',
success: function(datax){
jsonData = Ext.util.JSON.decode(datax.responseText);

var objDropdown = document.getElementById('tablefields');

var tablefieldsInnerHTML = '';
tablefieldsInnerHTML += '<option value="">Select...</option>';

Ext.each(jsonData,function(el){
tablefieldsInnerHTML += '<option value="'+el.value+'">'+el.label+'</option>';
});

Ext.get('tablefields').dom.innerHTML = tablefieldsInnerHTML;
},
failure: function(datax){
Ext.get('tablefields').dom.innerHTML = '';
Ext.Msg.alert('Status', 'Ajax error occured while trying to retrieve the fileds for '+selectedOptionElementValue+' table');
},

params: {
selectedIndexValue: selectedOptionElementValue,
}
});

When you test this code in browsers it works in all of them except IE, because the IE doesn’t support injection of innerHtml in the select elements.

If you wish to inject something in select elements, for it to work in IE you have to use DOM function. Now the part of code that injects options in select element looks like this:

var objDropdown = document.getElementById('tablefields');

var elll = Ext.get('tablefields').dom;

Ext.each(jsonData,function(el){
elll.options.add(new Option(el.value, el.label));
});

And if you now check it in the browsers it works in all of them, including IE 6+.
Cheers!

1
Top

Enjoyed this post?

Subscribe to our RSS Feed, Follow us on Twitter and spread it to your friends!

Author

Iva Korlevic

Ex. Inchooer

Iva worked as a Magento frontend developer in Inchoo from 2010 to 2012.

Other posts from this author

Discussion 1 Comment

Add Comment
  1. Congratulations Iva on your first Inchoo article.

    Message to the world: Iva is our first female eCommerce developer in the team. She joined recently and we all hope we will see more articles from her ;)

    Great one!

Add Your Comment

Please wrap all source codes with [code][/code] tags.
Top