FlexGrid ODataCollection (Paging, Searching, and Grouping)
This fiddle demonstrates how to combine Paging, Searching, and Grouping in a FlexGrid.
by Joel Parks
HTML
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<link rel="stylesheet" href="http://cdn.wijmo.com/5.latest/styles/wijmo.min.css">
<script src="http://cdn.wijmo.com/5.latest/controls/wijmo.min.js"></script>
<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>
<script src="http://cdn.wijmo.com/5.latest/controls/wijmo.chart.min.js"></script>
<script src="http://cdn.wijmo.com/5.latest/controls/wijmo.grid.filter.min.js"></script>
<script src="http://cdn.wijmo.com/5.latest/controls/wijmo.odata.min.js"></script>
<div class="container">
<h1>
FlexGrid
</h1>
<p>
The FlexGrid allows you to visualize and edit tabular data. It provides a variety of options about how to present and perform operations over the data, including selection, sorting, filtering, grouping, paging, editing, formatting, etc.
</p>
<p>
Populate the FlexGrid by setting its <b>itemsSource</b> property to an array containing regular JavaScript objects. The grid will automatically generate columns to display the data items, and will allow users to select, sort, and edit the data.</p>
<div id="flexGrid" class="custom-icons" style="font-family:Courier New"></div>
<div id="pager">
<button id="btnFirst" class="btn"><span class="wj-glyph-step-backward"></span></button>
<button id="btnPrev" class="btn"><span class="wj-glyph-left"></span></button>
<span id="spanCurrent"></span>
<button id="btnNext" class="btn"><span class="wj-glyph-right"></span></button>
<button id="btnLast" class="btn"><span class="wj-glyph-step-forward"></span></button>
</div>
</div>
CSS
.wj-flexgrid {
max-height: 250px;
margin-bottom: 12px;
}
.header {
display: inline-block;
width: 150px;
text-align: right;
font-weight: normal;
}
// Changes the icon for filtering the grid
.custom-icons .wj-glyph-filter {
background-image:url('http://icons.iconseeker.com/png/fullsize/aspnet/filter-1.png');
background-repeat: no-repeat;
background-position: bottom right;
margin-top: 4px;
width: 1em; height: 1em;
border: none;
}
.custom-icons .wj-glyph-filter:after {
display: none;
}
/* make active filter images 25% larger */
.custom-icons .wj-filter-on .wj-glyph-filter {
transform: scale(1.25)
}
JavaScript
onload = function() {
// use ODataCollectionView to demonstrate server-based paging
var url = 'http://services.odata.org/Northwind/Northwind.svc';
var viewOd = new wijmo.odata.ODataCollectionView(url, 'Customers', {
pageSize: 50,
pageOnServer: true,
sortOnServer: true,
//filterDefinition: 'startswith(Country, \'G\')',
//filterOnServer: true,
pageChanged: updateCurrentPageOd,
loaded: updateCurrentPageOd
});
// update pager status
viewOd.onPageChanged();
function updateCurrentPageOd() {
var curr = wijmo.format('Page {index:n0} of {cnt:n0}', {
index: viewOd.pageIndex + 1,
cnt: viewOd.pageCount
});
document.getElementById('spanCurrent').textContent = curr;
}
// implement pager
document.getElementById('pager').addEventListener('click', function(e) {
var btn = wijmo.closest(e.target, 'button');
var id = btn ? btn.id : '';
switch (id) {
case 'btnFirst':
viewOd.moveToFirstPage();
break;
case 'btnPrev':
viewOd.moveToPreviousPage();
break;
case 'btnNext':
viewOd.moveToNextPage();
break;
case 'btnLast':
viewOd.moveToLastPage();
break;
}
});
// show the OData in a grid
var flexOd = new wijmo.grid.FlexGrid('#flexGrid', {
itemsSource: viewOd,
showAlternatingRows: false,
headersVisibility: 'Column',
isReadOnly: true // the Northwind sample service is read-only!
});
//console.log(flexOd);
viewOd.groupDescriptions.push(new wijmo.collections.PropertyGroupDescription('Country'));
}