Handsontable Select2 Dependent Dropdowns

浪子不回头ぞ 提交于 2019-12-12 05:48:57

问题


I have two select2 dropdowns and I want to change options the second dropdown depending on first dropdown.

For example,

        {
          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'
          }
        },

Depending on country_id, I want to change select2 options of state_id. I know how to make this work with just select2, but I am not able to figure out how to make it work with handsontable.

I have change select2Options in afterChange, but how to do that?

  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:


You retrieve all the states for that country. Make sure the states list is in the same format that select2 expects.

Then loop through the columns list and determine the dependent column for the current column that has been modified.

Get the cell properties:

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

Update the select2 source:

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


来源:https://stackoverflow.com/questions/31691214/handsontable-select2-dependent-dropdowns

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