Wijmo 5 - Responsive FlexGrid

This FlexGrid shows all columns on large devices, and only a summary column on small devices.

by Ross Dederer

HTML

<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<script src="http://cdn.wijmo.com/5.20151.63/controls/wijmo.min.js"></script>
<link rel="stylesheet" href="http://cdn.wijmo.com/5.20151.63/styles/wijmo.min.css">
<script src="http://cdn.wijmo.com/5.20151.63/controls/wijmo.grid.min.js"></script>
<h3>Responsive Column Layout</h3>

<p>This FlexGrid shows all columns on large devices, and only one summary column on small devices:</p>
<div id="theGrid"></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 grid = new wijmo.grid.FlexGrid('#theGrid', {
        autoGenerateColumns: false,
        headersVisibility: wijmo.grid.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*" }
        ],
    });
    
	// store default row height
    var rowHeight = grid.rows.defaultSize;

    // make it responsive
    grid.addEventListener(window, 'resize', showColumns);
    function showColumns() {

        // show/hide colummns
        var phone = grid.hostElement.getBoundingClientRect().width < 600,
            cols = grid.columns;
        for (var i = 0; i < cols.length; i++) {
			grid.columns[i].visible = (i == 0 ? phone : !phone);
		}

        // make rows taller on phone layout
        grid.rows.defaultSize = phone ? rowHeight * 3 : rowHeight;

        // hide column headers on phone layout
        grid.headersVisibility = phone ? wijmo.grid.HeadersVisibility.None : wijmo.grid.HeadersVisibility.Column;
	}
    
	// show company summary on phone layout
    grid.formatItem.addHandler(function (s, e) {
		if (e.panel == grid.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>',
                grid.rows[e.row].dataItem);
			e.cell.innerHTML = html;
		}
	});
   
    // get some data
   ...