Demo - CollectionView Sorting
HTML
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdn.grapecity.com/wijmo/5.latest/styles/wijmo.min.css">
<script src="https://cdn.grapecity.com/wijmo/5.latest/controls/wijmo.min.js"></script>
<script src="https://cdn.grapecity.com/wijmo/5.latest/controls/wijmo.grid.min.js"></script>
<div class="container">
<h1>
CollectionView Sorting
</h1>
<p>
Use the <b>CollectionView.sortDescriptions</b> property to specify
which fields should be sorted on and in which direction.</p>
<p>
To see an example, choose one of the options:</p>
<label>
<input type="radio" name="sort" value="" checked="true" />
No Sort
</label>
<label>
<input type="radio" name="sort" value="country" />
Sort by Country
</label>
<label>
<input type="radio" name="sort" value="country,sales" />
Sort by Country and Sales
</label>
<label>
<input type="radio" name="sort" value="country,sales,downloads" />
Sort by Country, Sales, and Downloads
</label>
<p>
Result:
</p>
<div id="theGrid">
</div>
</div>
CSS
.wj-flexgrid {
max-height: 250px;
}
label {
width: 100%
}
JavaScript
onload = function() {
// create some random data
var countries = 'US,Germany,UK,Japan,Italy,Greece'.split(',');
var data = [];
for (var i = 0; i < 1000; i++) {
data.push({
country: countries[i % countries.length],
downloads: Math.round(Math.random() * 20000),
sales: Math.random() * 10000,
expenses: Math.random() * 5000
});
}
// create a CollectionView
var view = new wijmo.collections.CollectionView(data);
// show the data in a grid
var flex = new wijmo.grid.FlexGrid('#theGrid', {
itemsSource: view,
showSort: true,
allowSorting: false, // sorting done by us, not by the grid
showAlternatingRows: false,
headersVisibility: 'Column'
});
// change the sort
document.addEventListener('change', function(e) {
// remove the old sort
view.sortDescriptions.clear();
// add the new sorts
var props = e.target.value.split(',');
for (var i = 0; i < props.length; i++) {
var asc = props[i] == 'country'; // country in ascending order, other in descending order
var sd = new wijmo.collections.SortDescription(props[i], asc);
view.sortDescriptions.push(sd);
}
});
}