Select2 Dependent dropdown lists

泄露秘密 提交于 2019-12-03 02:47:28
Lee Baker

Igor has come back to me with a way to do this

var data=[...];    
$().select2({data: function() {return {results:data};}});    
/// later    
data=[...something else];    
// next query select2 will use 'something else' data
igor.vaynberg

The correct format is:

.select2("data", {...})

For Select2 v4.x, here is a small js class.

Using this, options of a select2 list box will be loaded/refreshed by ajax based on selection of another select2 list box. And the dependency can be chained.

For example -

new Select2Cascade($('#country'), $('#province'), 'path/to/geocode', {type:"province", parent_id: ''});
new Select2Cascade($('#province'), $('#district'), 'path/to/geocode', {type:"district", parent_id: ''});

Check the demo on codepen. Also here is a post on how to use it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!