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

[AngularJS] How to Unregister $scope.$watch

Problem
As I fill out values in this form and click create button

The data will insert into database, and the result will show in data grid which implement by AngularJS ng-grid.

As I click specific data row, the afterSelectionChange event will be triggered.
Owing to "預算別" and "債務別" are cascading dropdown list, the second dropdown list options will change as the value of the first dropdown list changed.
Therefore, we will watch the second dropdown list and fill in values in the form.

The problem is......if I just change the second dropdown list instead of click data grid, it will also go into watch. Because of it had start to watch, if you change the value of the second dropdwon list, it will call the watch code even you don't trigger afterSelectionChange.



Here is the 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
24
  afterSelectionChange : function (newValue, oldValue) {
     if(!angular.isUndefined($scope.grid2SelectedRow[0])){
          $scope.tab2EditBtn = false;
          $scope.dbm100eTab2FormBean.budgetCode = String($scope.grid2SelectedRow[0].budgetCode);
          //等待債務別下拉單長出來
          $scope.$watch("dbm100eTab2FormBean.debtCode", function(newValue, oldValue) {
              $scope.dbm100eTab2FormBean.debtCode = String($scope.grid2SelectedRow[0].debtCode);
              //發行額
              $scope.dbm100eTab2FormBean.debtIssueAmt    = $scope.grid2SelectedRow[0].issueAmount;
              //實收額
              $scope.dbm100eTab2FormBean.realAmount      = $scope.grid2SelectedRow[0].realAmount;
              //溢(折)價金額
              $scope.dbm100eTab2FormBean.diversityAmount = $scope.grid2SelectedRow[0].diversityAmount;
              //發行成本額
              $scope.dbm100eTab2FormBean.issueCostAmount = $scope.grid2SelectedRow[0].issueCostAmount;
              //PK
              $scope.dbm100eTab2FormBean.seqNo           = $scope.grid2SelectedRow[0].seqNo;
              
              $scope.tab2CreateBtn = true;
              $scope.tab2EditBtn = false;
              
          }, true);
                            }
 }


Solution
Therefore, we need to unregister watch if we do not need to watch.
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
24
25
26
27
28
29
 afterSelectionChange : function (newValue, oldValue) {
    if(!angular.isUndefined($scope.grid2SelectedRow[0])){
         $scope.tab2EditBtn = false;
         $scope.dbm100eTab2FormBean.budgetCode = String($scope.grid2SelectedRow[0].budgetCode);
         //等待債務別下拉單長出來
         var unregister = $scope.$watch("dbm100eTab2FormBean.debtCode", function(newValue, oldValue) {
             $scope.dbm100eTab2FormBean.debtCode = String($scope.grid2SelectedRow[0].debtCode);
             //發行額
             $scope.dbm100eTab2FormBean.debtIssueAmt    = $scope.grid2SelectedRow[0].issueAmount;
             //實收額
             $scope.dbm100eTab2FormBean.realAmount      = $scope.grid2SelectedRow[0].realAmount;
             //溢(折)價金額
             $scope.dbm100eTab2FormBean.diversityAmount = $scope.grid2SelectedRow[0].diversityAmount;
             //發行成本額
             $scope.dbm100eTab2FormBean.issueCostAmount = $scope.grid2SelectedRow[0].issueCostAmount;
             //PK
             $scope.dbm100eTab2FormBean.seqNo           = $scope.grid2SelectedRow[0].seqNo;
             
             $scope.tab2CreateBtn = true;
             $scope.tab2EditBtn = false;
             
         }, true);
         
         //unregister watch after 3 sec.
         setTimeout(function() {
             unregister();  
         }, 3000);
  }
}


Reference
[1] http://stackoverflow.com/questions/14957614/angular-js-clear-watch

2015/04/20

Fail to set background color in iReport

Problem
Customer ask to set the background color of the last column to yellow.

But it does not work after I set backcolor to yellow.


Resolution
In addition to set background color, you also need to check Opaque attribute.

Then it will work now:



Reference
[1] http://community.jaspersoft.com/questions/817869/defining-color-band-background

2015/04/11

Breaking Even Is Hard to Do

The intelligent Investor一書有有提到
  • 如果一你開始就遭逢巨大損失,你需要了解,break even (損益兩平)非常困難
  • 賠錢是投資中無法避免的一個部分,你無法完全預防。但是,身為一位智慧型投資者,你應該負起責任,確保不會失去你大部分或全部的資金,讓你元氣大傷,難以恢復 
  • 投資時,別在一開始就投入太多資金,你應該要降低你的資產被瞬間摧毀的機會 
  • 投資要賺錢,要訣就是不要賠錢


假設一開始你買進某檔股票,股價為50元,一次買5張,你的成本是250
若運氣不好,第二年,遭逢市場劇變(如dot com泡沫),從50元跌到10元

倘若第三年開始,此檔股票每年持續以5%成長,你也要到第35年才能回本
年度
股價
市值
損益
1
50.00
250.00
0.00
2
10.00
50.00
(200.00)
3
10.50
52.50
(197.50)
4
11.03
55.13
(194.88)
5
11.58
57.88
(192.12)
6
12.16
60.78
(189.22)
7
12.76
63.81
(186.19)
8
13.40
67.00
(183.00)
9
14.07
70.36
(179.64)
10
14.77
73.87
(176.13)
11
15.51
77.57
(172.43)
12
16.29
81.44
(168.56)
13
17.10
85.52
(164.48)
14
17.96
89.79
(160.21)
15
18.86
94.28
(155.72)
16
19.80
99.00
(151.00)
17
20.79
103.95
(146.05)
18
21.83
109.14
(140.86)
19
22.92
114.60
(135.40)
20
24.07
120.33
(129.67)
21
25.27
126.35
(123.65)
22
26.53
132.66
(117.34)
23
27.86
139.30
(110.70)
24
29.25
146.26
(103.74)
25
30.72
153.58
(96.42)
26
32.25
161.25
(88.75)
27
33.86
169.32
(80.68)
28
35.56
177.78
(72.22)
29
37.33
186.67
(63.33)
30
39.20
196.01
(53.99)
31
41.16
205.81
(44.19)
32
43.22
216.10
(33.90)
33
45.38
226.90
(23.10)
34
47.65
238.25
(11.75)
35
50.03
250.16
0.16


假定這家公司非常厲害,研發出殺手級應用,變成全球技術領先者,第三年開始以每年20%的成長率,持續成長,你也要到第11年才能回本
年度
股價
市值
損益
1
50.00
250.00
0.00
2
10.00
50.00
(200.00)
3
12.00
60.00
(190.00)
4
14.40
72.00
(178.00)
5
17.28
86.40
(163.60)
6
20.74
103.68
(146.32)
7
24.88
124.42
(125.58)
8
29.86
149.30
(100.70)
9
35.83
179.16
(70.84)
10
43.00
214.99
(35.01)
11
51.60
257.99
7.99


從下表可以得知,即便是每年有25%的成長,也要花10年的時間才能損益兩平
成長率
需花多少年回本
5%
35
10%
19
15%
14
20%
11
25%
10


由以上可以了解,若遭逢重大損失,恢復元氣要花很長的一段時間,也難怪Warren Buffett曾說,投資的原則:
Rule No.1: Never lose money.
Rule No.2: Never forget rule No.1.