Select2 Dependent dropdown lists

前端 未结 3 760
[愿得一人]
[愿得一人] 2021-02-06 01:55

I am trying to use the Select2 plugin to have 4 dropdown lists that depend on each other. I have struggled to find the right way to update the data that loads the options in.

相关标签:
3条回答
  • 2021-02-06 02:26

    The correct format is:

    .select2("data", {...})
    
    0 讨论(0)
  • 2021-02-06 02:34

    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
    
    0 讨论(0)
  • 2021-02-06 02:34

    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.

    0 讨论(0)
提交回复
热议问题