Wijmo 5 - Custom Checkboxes

by Joel Parks

HTML

<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<script src="http://cdn.wijmo.com/5.20153.102/controls/wijmo.min.js"></script>
<link rel="stylesheet" href="http://cdn.wijmo.com/5.20153.102/styles/wijmo.min.css">
<script src="http://cdn.wijmo.com/5.20153.102/controls/wijmo.grid.min.js"></script>
<script src="http://cdn.wijmo.com/5.20153.102/interop/angular/wijmo.angular.min.js"></script>
<!-- mark this as an Angular application and give it a controller -->
<div ng-app="app" ng-controller="appCtrl">
    
<h1>Wijmo Checkbox Highlight Rows</h1>
    <p>
        This fiddle shows how to select an entire row when a checkbox in the FlexGrid has been checked. When the user clicks the checkbox, setting it to <b>true</b>, the entire row will be selected. When the checkbox gets set to <b>false</b>, the row will no longer be highlighted.
    </p>
    <wj-flex-grid initialized="initGrid(s,e)" items-source="data" style="height:250px;margin-top:10px">
        <wj-flex-grid-column header="Custom" binding="custom"></wj-flex-grid-column>
        <wj-flex-grid-column header="Country" binding="country"></wj-flex-grid-column>
        <wj-flex-grid-column header="Sales" binding="sales"></wj-flex-grid-column>
        <wj-flex-grid-column header="Expenses" binding="expenses"></wj-flex-grid-column>
        <wj-flex-grid-column header="Downloads" binding="downloads"></wj-flex-grid-column>
    </wj-flex-grid>
</div>

JavaScript

// define app, include Wijmo 5 directives
var app = angular.module('app', ['wj']);

// controller
app.controller('appCtrl', function ($scope) {

    // 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,
            custom: i % 3 == 0
        });
    }

    // expose data through scope
    $scope.data = data;
    
    $scope.initGrid = function(s, e) {
        var flex = s,
            binding = 'custom';
        
 		// add a checkbox to the 'custom' column
        var className = 'my-custom-checkbox';
        flex.formatItem.addHandler(function (s, e) {
            if (e.panel == flex.cells && flex.columns[e.col].binding == binding) {
                var chk = document.createElement('input');
                chk.type = 'checkbox';
                chk.checked = flex.rows[e.row].dataItem[binding];
                chk.style.marginLeft = '6px';
                chk.className = className;
                e.cell.innerHTML = '';
                e.cell.appendChild(chk);
            }
        });

         // prevent built-in editors in 'id' column
        flex.beginningEdit.addHandler(function (s, e) {
            if (e.panel == flex.cells && flex.columns[e.col].binding == binding) {
                e.cancel = true;
            }
        });
        
 		// handle click events on custom checkboxes
        flex.addEventListener(flex.hostElement, 'click', function (e) {
            if (wijmo.hasClass(e.target, className)) {
                flex.setCellData(flex.selection.row, binding, e.target.checked);
                
                //CHECKS TO SEE IF THE CHECKBOX IS TRUE OR FALSE, THEN DETERMINES WHETHER TO HIGHLIGHT THE ROW
                if(e.target.checked){
       ...