Total Pageviews

2015/02/13

How to get selected row's value from ng-grid AngularJS in click event

If we are using ng-grid to display data as bellows:


As user click specific row, it should get this row's issueSeqNo and show alert message



 We have a ng-grid like below:
1
2
3
4
5
6
7
 <div class="row text-center">
  <div class="col-sm-12">
    <div class="grid-style" data-ng-grid="itemGrid"
         style="display: inline-block; height: 300px; width: 80%;">
    </div>
  </div>
 /div>

 and our javascript would be
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
   $scope.itemGrid = {
     data : 'itemData',
     selectedItems: $scope.selectedRow,
     columnDefs : [
         { field : 'issueSeqNo', displayName : 'issueSeqNo', width: 120, cellClass: 'text-left'},
         { field : 'issueSerial', displayName : '公債代號', width: 150, cellClass: 'text-left'}, 
         { field : 'issueDate',   displayName : '發行日期', width: 150, cellClass: 'text-left'}, 
         { field : 'debtName',    displayName : '公債名稱', width: 600, cellClass: 'text-left'}],
     enableColumnResize :true,
     i18n:'zh-tw',
     multiSelect : false,
     showFooter: true
 };


If we would like to get selected row's value from ng-grid, just add afterSelectionChange event as bellow:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
  $scope.itemGrid = {
     data : 'itemData',
     selectedItems: $scope.selectedRow,
     columnDefs : [
         { field : 'issueSeqNo', displayName : 'issueSeqNo', width: 120, cellClass: 'text-left'},
         { field : 'issueSerial', displayName : '公債代號', width: 150, cellClass: 'text-left'}, 
         { field : 'issueDate',   displayName : '發行日期', width: 150, cellClass: 'text-left'}, 
         { field : 'debtName',    displayName : '公債名稱', width: 600, cellClass: 'text-left'}],
     enableColumnResize :true,
     i18n:'zh-tw',
     multiSelect : false,
     showFooter: true,
     afterSelectionChange: function () {
         if(!angular.isUndefined($scope.selectedRow[0])){
             alert($scope.selectedRow[0].issueSeqNo);
         }
      }
 };



Reference
[1] http://stackoverflow.com/questions/16911156/how-to-get-the-cell-value-from-ng-grid

No comments: