hkalbertl / jquery.appendGrid

The dynamic table input JavaScript plugin
https://appendgrid.azurewebsites.net
MIT License
148 stars 76 forks source link

select option dynamically on click #135

Closed malohtie closed 4 years ago

malohtie commented 4 years ago

let say i have a button outside the grid and want to select the first option in all select available in appendGrid, cant find an option in the api is this possible ? thanks

hkalbertl commented 4 years ago

Hi malohtie,

I guess the most straight forward way is to get the number of rows by getRowCount first and then get the select DOM element by getCellCtrl and change the selectedIndexof it. For example,

// Initilize the grid
var myAppendGrid = new AppendGrid({ 
  columns:[
    {
      name: 'dropdown',
      type: 'select'
    }
  ]
});

// When your button clicked
$('#button').on('click', function(){
  var total = myAppendGrid.getRowCount();
  for (var z = 0; z < total; z++) {
    // Get the select element
    var select = myAppendGrid.getCellCtrl('dropdown', z);
    // Change the selected index
    select.selectedIndex = 0;
    // Optional: Trigger change event if required
    $(select).trigger('change');
  }
});