Filter data source of a dropdown list based on the selection of another dropdown list in grid

I have a Grid that has several fields, two of them are emp_id (table of companies) and rol_id (table of roles linked to companies).


Both fields get their data from the database, however, I should not show all the content of rol_id, I need to filter the rol_id data based on emp_id. In the attached image is the example, I have two companies: erpuda and GABI, however, the roles "ADMINISTRADOR" and "CONTADOR" belong to "erpuda" and should only be shown when "erpuda" is selected, and "Contadores" when GABI is selected. How can I do this?


Sin título.png




7 Replies 1 reply marked as answer

RR Rajapandi Ravi Syncfusion Team September 19, 2022 01:40 PM UTC

Hi Diego,


Greetings from Syncfusion support


Based on your query we could see that you like to change the datasource of the dropdown based on another dropdown selection, since your requirement was related to our cascading dropdown feature. You can achieve the Cascading DropDownList with grid Editing by using the Cell Edit Template feature. We have already discussed about your requirement in our documentation. Please refer the below documentation for more information.


Documentation: https://ej2.syncfusion.com/javascript/documentation/grid/how-to/cascading-drop-down-list-with-grid-editing/


Regards,

Rajapandi R



DI Diego replied to Rajapandi Ravi September 26, 2022 11:40 PM UTC

Thanks a lot! it worked.



RR Rajapandi Ravi Syncfusion Team September 27, 2022 04:52 AM UTC

Hi Diego,


We are happy to hear that our suggestion was helpful to achieve your requirement.


Please get back to us if you need further assistance.


Regards,

Rajapandi R



DI Diego replied to Rajapandi Ravi October 5, 2022 04:46 PM UTC

Hi There. One more question, how can I prevent the selected item in the drop-down list from being deleted when editing?

As soon as I start editing, the selected items in the two drop-down lists are deleted.


On list:


2022-10-05_11-40.png

On edit:


2022-10-05_11-40_1.png



RR Rajapandi Ravi Syncfusion Team October 6, 2022 01:18 PM UTC

Hi Diego,


Thanks for the update


Based on your query we have prepared a sample and tried to reproduce your reported problem, but it was unsuccessful. Please refer the below code example and sample for more information.


var grid = new ej.grids.Grid({

      dataSource: cascadeData,

      toolbar: ['Add', 'Edit', 'Delete', 'Update', 'Cancel'],

      editSettings: { allowEditing: true, allowAdding: true, allowDeleting: true, mode: 'Normal' },

      columns: [

           .  .  .  .  .  .  .  .  .  .  .  .

           .  .  .  .  .  .  .  .  .  .  .  .

          {

              field: 'ShipCountry', headerText: 'Ship Country', width: 120, edit: {

                  create: function(){

                      countryElem = document.createElement('input');

                      return countryElem;

                  },

                  read: function(){

                      return countryObj.text;

                  },

                  destroy: function(){

                      countryObj.destroy();

                  },

                  write: function(args){

                      countryObj = new ej.dropdowns.DropDownList({

                          text: args.rowData.ShipCountry,

                          dataSource: country,

                          fields: { value: 'countryId', text: 'countryName' },

                          change: function(){

                              stateObj.enabled = true;

                              var tempQuery = new ej.data.Query().where('countryId', 'equal', countryObj.value);

                              stateObj.query = tempQuery;

                              stateObj.text = null;

                              stateObj.dataBind();

                          },

                          placeholder: 'Select a country',

                          floatLabelType: 'Never'

                      });

                      countryObj.appendTo(countryElem);

                  }

              }

          },

          {

              field: 'ShipState', headerText: 'Ship State', width: 150, edit: {

                  create: function(){

                      stateElem = document.createElement('input');

                      return stateElem;

                  },

                  read: function(){

                      return stateObj.text;

                  },

                  destroy: function(){

                      stateObj.destroy();

                  },

                  write: function(args){

                      stateObj = new ej.dropdowns.DropDownList({

                          text: args.rowData.ShipState,

                          dataSource: state,

                          fields: { value: 'stateId', text: 'stateName' },

                          enabled: false,

                          placeholder: 'Select a state',

                          floatLabelType: 'Never'

                      });

                      stateObj.appendTo(stateElem);

                  }

              }

          }

      ],

      height: 273

  });

  grid.appendTo('#Grid');  

 


Sample: https://stackblitz.com/edit/ajewkh?file=index.js


Screenshot:



If you still face the issue, please share any simple issue reproducible sample or try to reproduce the problem with our above attached sample and also share the problem scenario in video demonstration format.


Regards,

Rajapandi R


Marked as answer

DI Diego replied to Rajapandi Ravi October 6, 2022 08:22 PM UTC

Thank you so much!



RR Rajapandi Ravi Syncfusion Team October 7, 2022 04:17 AM UTC

Hi Diego,


We are happy to hear that our suggested solution was helpful to resolve the problem.


Please get back to us if you need further assistance.


Regards,

Rajapandi R


Loader.
Up arrow icon