KB - Wijmo 5 - Skip ReadOnly on Paste

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/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" class="container">

  <h1>Hello Wijmo 5 and AngularJS!</h1>

  <p>
    In this grid, selection skips over read-only columns (Sales)
    and the maximum length of country cells is limited to five.
  </p>
  <wj-flex-grid id="grid"
    items-source="data"
    initialized="initialized(s,e)"
    selection-changed="selectionChanged(s,e)"
    prepare-cell-for-edit="prepareCellForEdit(s,e)"
    pasted-cell="pastedCell(s,e)">
    <wj-flex-grid-column header="Country" binding="country"></wj-flex-grid-column>
    <wj-flex-grid-column header="Sales" binding="sales" is-read-only="true"></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>

CSS

.wj-flexgrid {
  height: 150px; 
  margin-top:10px;
 }
 .wj-flexchart {
   height: 300px;
 }

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 = [],
    flexGrid,
    readOnlyColumn = 1,
    cellPastedValue;
  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
    });
  }

  // expose data as a CollectionView to get events
  $scope.data = new wijmo.collections.CollectionView(data);
  
  $scope.initialized = function(s, e) {
  	flexGrid = s;
  }

	// skip over read-only columns
  $scope.selectionChanged = function(s, e) {
  	var sel = s.selection;
  	if (s.columns[sel.col].isReadOnly) {
    	s.select(sel.row, sel.col + 1);
    }
  }
  
  // limit 'country' length to three characters
  $scope.prepareCellForEdit = function(s, e) {
  	if (s.columns[e.col].binding == 'country') {
    	s.activeEditor.maxLength = 5;
    }
  }
  
  $scope.pastedCell = function(s, e) {
  	flexGrid.setCellData(flexGrid.selection.row, flexGrid.selection.col, cellPastedValue)
  }
  
  document.addEventListener('paste', function(e) {
  	var clipBoardData = e.clipboardData.getData('text/plain');
    var splitBoard = clipBoardData.split('\t');
    var currentItemIndex = 0;
    cellPastedValue = splitBoard[0];
    if(splitBoard.length <= 1) {
    	flexGrid.setCellData(flexGrid.selection.row, flexGrid.selection.col, clipBoardData);
    } else {
    	if(splitBoard.length > readOnlyColumn + 1) {
      	for(var i = 0; currentItemIndex < splitBoard.length; i++) {
        	if(i == readOnlyColumn) {
          	i++;
          }
          flexGrid.setCellData(flexGrid.selection.row, i, splitBoard[currentItemIndex]);
          currentItemIndex++;
        }
      } else {
      	// Paste the data like normal
      }
    }
   ...