代码之家  ›  专栏  ›  技术社区  ›  rjvim

手持式Select2相关下拉列表

  •  0
  • rjvim  · 技术社区  · 9 年前

    我有两个select2下拉列表,我想根据第一个下拉列表更改第二个下拉列表的选项。

    例如

            {
              data: 'country_id',
              editor: 'select2', 
              renderer: customDropdownRenderer,
              select2Options: { 
                      data: {!! $production_units !!} ,
                      dropdownAutoWidth: true,
              }
            },
            {
              data: 'state_id',
              editor: 'select2', 
              renderer: customDropdownRenderer,
              select2Options: { 
                      data: [],
                      dropdownAutoWidth: true,
                      width: 'resolve'
              }
            },
    

    根据country_id,我想更改state_id的select2选项。我知道如何使用select2来实现这一功能,但我无法确定如何使用可手动操作。

    我在更改后更改了select2Options,但如何做到这一点?

      afterChange: function (change, source) {
    
            if(change)
            {
    
              if(change[0][1] == 'country_id')
              {
                $.get("/api/states?country="+change[0][3], function(data){
    
                     //What should be done here?
    
                });
              }
    
            }
    
          },
    
    1 回复  |  直到 9 年前
        1
  •  0
  •   mpusarla    9 年前

    你检索该国家的所有州。确保状态列表的格式与select2预期的格式相同。

    然后循环遍历列列表,并确定已修改的当前列的从属列。

    获取单元格属性:

    var cellProperties = instance.getCellMeta(rowIndex, dependentColumnIndex);
    

    更新select2源:

    cellProperties['select2Options'].data = [states list];