Demo - Responsive Layouts
This FlexGrid shows all columns on large devices, and only a summary column on small devices.
by Emmanuel Garcia
May 16, 2018
HTML
<link rel="stylesheet" href="http://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>Responsive Layouts</h1>
<p>
This FlexGrid shows all columns on large devices,
and only one summary column on devices with
narrow screens:</p>
<div id="theGrid"></div>
</div>
CSS
/* set grid height */
.wj-flexgrid {
height: 350px;
-moz-user-select: none;
}
/* disable alternating rows */
.wj-alt:not(.wj-header):not(.wj-group):not(.wj-state-selected):not(.wj-state-multi-selected) {
background: #fff;
}
/* format detail cells */
.item-header {
font-weight: bold;
font-size: 150%;
}
.item-detail {
margin-left: 12px;
}
JavaScript
window.onload = function() {
// create the grid
var theGrid = new wijmo.grid.FlexGrid('#theGrid', {
autoGenerateColumns: false,
headersVisibility: 'Column',
columns: [
{ header: "Company", isReadOnly: true, visible: false, allowDragging: false, width: "*" },
{ header: "Company Name", binding: "CompanyName", width: "20*" },
{ header: "Contact Name", binding: "ContactName", width: "15*" },
{ header: "Contact Title", binding: "ContactTitle", width: "20*" },
{ header: "Address", binding: "Address", width: "20*" },
{ header: "City", binding: "City", width: "10*" },
{ header: "Country", binding: "Country", width: "15*" }
],
formatItem: function (s, e) { // show company summary on narrow layout
if (e.panel == s.cells && e.col == 0) {
var html = wijmo.format('<div class="item-header">{CompanyName}</div>' +
'<div class="item-detail">{ContactName}, {ContactTitle}</div>' +
'<div class="item-detail">{Address}, {City}, {Country}</div>',
s.rows[e.row].dataItem);
e.cell.innerHTML = html;
}
}
});
// store default row height
var defaultRowHeight = theGrid.rows.defaultSize;
// make it responsive
theGrid.addEventListener(window, 'resize', updateGridLayout);
function updateGridLayout() {
// show/hide columns
var narrow = theGrid.hostElement.clientWidth < 600;
theGrid.columns.forEach(function(col) {
col.visible = col.index == 0 ? narrow : !narrow;
});
// make rows taller on phone layout
theGrid.rows.defaultSize = defaultRowHeight * (narrow ? 3 : 1);
// hide column headers on narrow layouts
theGrid.headersVisibility = narrow ? 'None' : 'Column';
}
// get some data
wijmo.httpRequest('http://services.odata.org/Northwind/Northwind.svc/Customers?$format=json', {
success: function (xhr) {
var response = JSON.parse(xhr.responseText);
theGrid.itemsSource = response.value;
updateGridLayout();
}
});
}