Total Pageviews

2015/04/23

[AngularJS] How to Clear Selection in ng-grid

Problem
As I selected a specific data row in ng-grid, it should fill in its data into data form

As I click clear button, it should clear the form data and selected item in ng-grid. But it does not work.



Here is the code snippet. I had set the the length of selectedItem to zero, but in vain.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
 $scope.resetTab2 = function() {
     cAlerter.clear();
     $scope.clearValidationMessages();
     
     $scope.dbm100eTab2FormBean.seqNo = 0;
     //預算別
     $scope.dbm100eTab2FormBean.budgetCode = "";
     //債務別
     $scope.dbm100eTab2FormBean.debtCode = "";
     //發行額
     $scope.dbm100eTab2FormBean.debtIssueAmt = 0;
     //實收額
     $scope.dbm100eTab2FormBean.realAmount = 0;
     //溢(折)價金額
     $scope.dbm100eTab2FormBean.diversityAmount = 0;
     //發行成本額
     $scope.dbm100eTab2FormBean.issueCostAmount = 0;
     
     $scope.tab2CreateBtn = false;
     $scope.tab2EditBtn = true;
     $scope.dbm100eFormGrid2.selectedItems.length = 0;
 };

Solution
In addition to set selectedItems' length to zero, I also need to set selectAll to false.
Here is the revised code snippet:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 $scope.resetTab2 = function() {
     cAlerter.clear();
     $scope.clearValidationMessages();
     
     $scope.dbm100eTab2FormBean.seqNo = 0;
     //預算別
     $scope.dbm100eTab2FormBean.budgetCode = "";
     //債務別
     $scope.dbm100eTab2FormBean.debtCode = "";
     //發行額
     $scope.dbm100eTab2FormBean.debtIssueAmt = 0;
     //實收額
     $scope.dbm100eTab2FormBean.realAmount = 0;
     //溢(折)價金額
     $scope.dbm100eTab2FormBean.diversityAmount = 0;
     //發行成本額
     $scope.dbm100eTab2FormBean.issueCostAmount = 0;
     
     $scope.tab2CreateBtn = false;
     $scope.tab2EditBtn = true;
     $scope.dbm100eFormGrid2.selectedItems.length = 0;
     $scope.dbm100eFormGrid2.selectAll(false);
 };

Then it works now.



Reference
[1] https://github.com/angular-ui/ng-grid/issues/2243

No comments: