Paste New Rows in Grid

We can paste data in last row by paste method

by Manish Gupta

HTML

<script src="http://cdn.wijmo.com/5.20161.151/controls/wijmo.min.js"></script>
<link rel="stylesheet" href="http://cdn.wijmo.com/5.20161.151/styles/wijmo.min.css">
<script src="http://cdn.wijmo.com/5.20161.151/controls/wijmo.grid.min.js"></script>
<script src="http://cdn.wijmo.com/5.20161.151/interop/angular/wijmo.angular.min.js"></script>
 <div ng-app="app">
        <div ng-controller="appCtrl">
            <!-- This is a grid -->
            <wj-flex-grid items-source="data" pasted="pasted(s,e)"  control="flex"> 
                <wj-flex-grid-column header="id" binding="id"></wj-flex-grid-column>              
                <wj-flex-grid-column header="Country" binding="country"></wj-flex-grid-column>              
                <wj-flex-grid-column header="Date" binding="date"></wj-flex-grid-column>              
                <wj-flex-grid-column header="Amount" binding="amount"></wj-flex-grid-column>              
            </wj-flex-grid>
        </div>
    </div>

JavaScript

var pastedData;// contain data to be paste 
        document.addEventListener('paste', function (e) {          
            clipboardData = e.clipboardData || window.clipboardData;
            pastedData = clipboardData.getData('Text');
        });
        // declare app module
        var app = angular.module('app', ['wj']);
        // app controller provides data
        app.controller('appCtrl', function appCtrl($scope) {
            // generate some random data
            var countries = 'US,Germany,U K,Japan,Italy,Greece'.split(','),
                data = [];
            for (var i = 0; i < 5; i++) {
                data.push({
                    id: i,
                    country: countries[i % countries.length],
                    date: new Date(2014, i % 12, i % 28),
                    amount: Math.random() * 10000,
                    active: i % 4 == 0
                });
            }
            // add data array to scope
            $scope.data = new wijmo.collections.CollectionView(data);

            $scope.pasted = function (s, e) {
                var pastedDataLength = s.rows.length - e.row;                
                if (pastedDataLength < pastedData.split('\n').length) {                    
                    var pastedDataArray = pastedData.split("\n");
                    console.log(pastedDataArray)
                    for (var i = pastedDataLength; i < pastedDataArray.length ; i++) {
                        // Add data to collection View
                        var newItem = s.collectionView.addNew();
                        var itemToAdd = pastedDataArray[i].split("\t");
                        newItem.id = itemToAdd[0];
                        newItem.country = itemToAdd[1];
                        newItem.date = itemToAdd[2];
                        newItem.amount = itemToAdd[3];
                       // newItem.active = itemToAdd[4];  no active column 
                        s.collectionView.commitNew();
                  ...