1
1
import { Component , OnInit , Injectable , ViewEncapsulation } from '@angular/core' ;
2
- import { AngularGridInstance , Column , Editors , FieldType , Formatters , GridOption , GridService , OnEventArgs } from './../modules/angular-slickgrid' ;
2
+ import {
3
+ AngularGridInstance ,
4
+ Column ,
5
+ Editors ,
6
+ FieldType ,
7
+ Formatter ,
8
+ Formatters ,
9
+ GridOption ,
10
+ GridService ,
11
+ OnEventArgs
12
+ } from './../modules/angular-slickgrid' ;
13
+
14
+ // custom formatter to display priority (from 1 to 3) loop through that count and display them as x number of icon(s)
15
+ const customPriorityFormatter : Formatter = ( row , cell , value , columnDef , dataContext ) => {
16
+ let output = '' ;
17
+ const count = + ( value >= 3 ? 3 : value ) ;
18
+ const color = count === 3 ? 'red' : ( count === 1 ? 'grey' : 'orange' ) ;
19
+ const icon = `<i class="fa fa-fire ${ color } " aria-hidden="true"></i>` ;
20
+
21
+ for ( let i = 1 ; i <= count ; i ++ ) {
22
+ output += icon ;
23
+ }
24
+ return output ;
25
+ } ;
3
26
4
27
@Component ( {
5
- styles : [ '.duration-bg { background-color: #e9d4f1 !important }' ] ,
28
+ templateUrl : './grid-additem.component.html' ,
29
+ styleUrls : [ './grid-additem.component.scss' ] ,
6
30
encapsulation : ViewEncapsulation . None ,
7
- templateUrl : './grid-additem.component.html'
8
31
} )
9
32
@Injectable ( )
10
33
export class GridAddItemComponent implements OnInit {
@@ -25,6 +48,7 @@ export class GridAddItemComponent implements OnInit {
25
48
<ul>
26
49
<li>Example, click on button "Highlight Rows with Duration over 50" to see row styling changing. <a href="https://github.com/ghiscoding/Angular-Slickgrid/wiki/Dynamically-Add-CSS-Classes-to-Item-Rows" target="_blank">Wiki doc</a></li>
27
50
</ul>
51
+ <li>Context Menu... Right+Click on any row to show a Context Menu allowing you to change the "Priority" field</li>
28
52
</ul>
29
53
` ;
30
54
@@ -52,6 +76,48 @@ export class GridAddItemComponent implements OnInit {
52
76
this.grid.invalidate();
53
77
this.grid.render();
54
78
*/
79
+
80
+ const data = angularGrid . dataView . getItems ( ) ;
81
+
82
+ const contextMenuTitle = 'Set priority' ;
83
+ const contextMenuList = [
84
+ { option : 1 , label : 'Low' } ,
85
+ { option : 2 , label : 'Medium' } ,
86
+ { option : 3 , label : 'High' } ,
87
+ ] ;
88
+
89
+ let liOptions = '' ;
90
+ for ( let i = 0 ; i < contextMenuList . length ; i ++ ) {
91
+ if ( contextMenuList . hasOwnProperty ( i ) ) {
92
+ const contextItem = contextMenuList [ i ] ;
93
+ liOptions += `<li data="${ contextItem . option } ">${ contextItem . label } </li>` ;
94
+ }
95
+ }
96
+
97
+ const htmlString = `<ul id="contextMenu">
98
+ <span class="title">${ contextMenuTitle } </span>
99
+ ${ liOptions }
100
+ </ul>` ;
101
+
102
+ // create context menu, hide it & append it to the body
103
+ const contextElm = $ ( htmlString ) ;
104
+ contextElm . css ( 'display' , 'none' ) ;
105
+ contextElm . appendTo ( $ ( 'body' ) ) ;
106
+
107
+ contextElm . click ( ( e : any ) => {
108
+ if ( ! $ ( e . target ) . is ( 'li' ) ) {
109
+ return ;
110
+ }
111
+ if ( ! this . grid . getEditorLock ( ) . commitCurrentEdit ( ) ) {
112
+ return ;
113
+ }
114
+ const row = contextElm . data ( 'row' ) ;
115
+ data [ row ] . priority = $ ( e . target ) . attr ( 'data' ) ;
116
+ this . grid . updateRow ( row ) ;
117
+ this . angularGrid . gridService . setSelectedRows ( [ row ] ) ;
118
+
119
+ // callback, user might want to do something else
120
+ } ) ;
55
121
}
56
122
57
123
ngOnInit ( ) : void {
@@ -115,6 +181,15 @@ export class GridAddItemComponent implements OnInit {
115
181
formatter : Formatters . dateIso , sortable : true ,
116
182
type : FieldType . date
117
183
} ,
184
+ {
185
+ id : 'priority' ,
186
+ name : 'Priority' ,
187
+ field : 'priority' ,
188
+ minWidth : 100 ,
189
+ filterable : true ,
190
+ sortable : true ,
191
+ formatter : customPriorityFormatter ,
192
+ } ,
118
193
{
119
194
id : 'effort-driven' , name : 'Effort Driven' , field : 'effortDriven' ,
120
195
formatter : Formatters . checkmark ,
@@ -153,6 +228,7 @@ export class GridAddItemComponent implements OnInit {
153
228
percentCompleteNumber : randomPercent ,
154
229
start : new Date ( randomYear , randomMonth , randomDay ) ,
155
230
finish : new Date ( randomYear , ( randomMonth + 1 ) , randomDay ) ,
231
+ priority : i % 5 ? 1 : ( i % 2 ? 2 : 3 ) ,
156
232
effortDriven : ( i % 5 === 0 )
157
233
} ;
158
234
}
@@ -174,6 +250,7 @@ export class GridAddItemComponent implements OnInit {
174
250
percentCompleteNumber : randomPercent ,
175
251
start : new Date ( randomYear , randomMonth , randomDay ) ,
176
252
finish : new Date ( randomYear , ( randomMonth + 2 ) , randomDay ) ,
253
+ priority : 1 ,
177
254
effortDriven : true
178
255
} ;
179
256
this . angularGrid . gridService . addItem ( newItem , { position : insertPosition } ) ;
@@ -249,4 +326,20 @@ export class GridAddItemComponent implements OnInit {
249
326
scrollGridTop ( ) {
250
327
this . angularGrid . slickGrid . navigateTop ( ) ;
251
328
}
329
+
330
+ onContextMenu ( e ) {
331
+ e . preventDefault ( ) ;
332
+ const cell = this . angularGrid . slickGrid . getCellFromEvent ( e ) ;
333
+ const contextElm = $ ( '#contextMenu' ) ;
334
+
335
+ // reposition the context menu where we right+clicked
336
+ contextElm
337
+ . data ( 'row' , cell . row )
338
+ . css ( 'top' , e . pageY )
339
+ . css ( 'left' , e . pageX )
340
+ . show ( ) ;
341
+
342
+ // hide the context menu once user choose an option
343
+ $ ( 'body' ) . one ( 'click' , ( ) => contextElm . hide ( ) ) ;
344
+ }
252
345
}
0 commit comments