FlexGrid - Add/Remove columns

by Troy Taylor

HTML

<script src="http://cdn.wijmo.com/5.latest/controls/wijmo.min.js"></script>
<link rel="stylesheet" href="http://cdn.wijmo.com/5.latest/styles/wijmo.min.css">
<script src="http://cdn.wijmo.com/5.latest/controls/wijmo.input.min.js"></script>
<script src="http://cdn.wijmo.com/5.latest/controls/wijmo.grid.min.js"></script>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<div class="container text-center">

  <h1>FlexGrid</h1>
  <div>
  <button id="add" class="btn">Add Column</button>
  <button id="remove" class="btn">Remove Column</button>
  </div>
  <br>
  <div id="theGrid"></div>

</div>

CSS

.wj-flexgrid {
  height: 200px;
  margin-bottom:24px  
}

JavaScript

var addedColumns = 0;

// create some random data
var countries = 'US,Germany,UK,Japan,Italy,Greece'.split(','),
  data = [];
for (var i = 0; i < countries.length; i++) {
  data.push({
    country: countries[i],
    downloads: Math.round(Math.random() * 20000),
    sales: Math.random() * 10000,
    expenses: Math.random() * 5000
  });
}

// initialize a grid in code
var grid = new wijmo.grid.FlexGrid('#theGrid', {
  autoGenerateColumns: false,
  itemsSource: data,
  keyActionEnter: "None",
  selectionMode: "Cell",
  columns: [
    { binding: 'country', header: 'Country' },
    { binding: 'downloads', header: 'Downloads' },
    { binding: 'sales', header: 'Sales', format: 'c' },
    { binding: 'expenses', header: 'Expenses', format: 'c' }
  ]
});

document.getElementById('add').addEventListener('click', function(){
	//console.log('add');
	grid.columns.push(new wijmo.grid.Column({
  	header: "Added col #"+addedColumns++
  }));
});

document.getElementById('remove').addEventListener('click', function(){
	//console.log('remove');
	if (grid.columns.length > 4){
  	grid.columns.pop();
  	addedColumns--;
  }
});