KB - Wijmo 5 - Prevent rowEditEnding from firing twice when a new row is added
This sample shows how to prevent rowEditEnding from firing twice when a new row is added to the 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/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>
RowEditEnded event</h1>
<p>
Click the checkboxes on the grid and see the output in the console:</p>
<wj-flex-grid
items-source="data"
allow-add-new=true
new-row-at-top="true"
row-edit-ending="rowEditEnding(s,e)"
cell-edit-ended="cellEditEnded(s, e)">
</wj-flex-grid>
</div>
CSS
.wj-flexgrid {
max-height: 300px;
}
JavaScript
// define app, include Wijmo 5 directives
var app = angular.module('app', ['wj']);
// controller
app.controller('appCtrl', function($scope) {
$scope.cellEditEnded = function(s, e) {
var flex = s, // FlexGrid is the event sender
row = flex.rows[e.row], // row that was edited
col = flex.columns[e.col]; // column that was edited
// do something with the data object that was changed
if (col.dataType == wijmo.DataType.Boolean) {
console.log('you changed the value of the "' + col.binding + '" property for this object: ');
console.log(JSON.stringify(row.dataItem));
}
}
$scope.rowEditEnding = function(s, e) {
if(s.rows.length - 1 == $scope.sourceLength && e.row == 0) {
// Do nothing
}else if(s.rows.length - 1 == $scope.sourceLength){
console.log('A row was edited...');
}else{
console.log('A row was added...');
$scope.sourceLength++;
}
}
// 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,
active: i % 3 == 0
});
}
// expose data to view
$scope.data = data;
$scope.sourceLength = $scope.data.length;
});