@@ -7,11 +7,13 @@ import {
77 Editors ,
88 FieldType ,
99 Filters ,
10+ Formatter ,
1011 Formatters ,
1112 GridOption ,
1213 OnEventArgs ,
1314} from './../modules/angular-slickgrid' ;
1415import { EditorNgSelectComponent } from './editor-ng-select.component' ;
16+ import { CustomActionFormatterComponent } from './custom-actionFormatter.component' ;
1517import { CustomAngularComponentEditor } from './custom-angularComponentEditor' ;
1618import { CustomAngularComponentFilter } from './custom-angularComponentFilter' ;
1719import { CustomTitleFormatterComponent } from './custom-titleFormatter.component' ;
@@ -23,6 +25,17 @@ declare var $: any;
2325
2426const NB_ITEMS = 100 ;
2527
28+ const customActionFormatter : Formatter = ( row : number , cell : number , value : any , columnDef : Column , dataContext : any , grid : any ) => {
29+ // use the same button text "Action" as the "CustomActionFormatterComponent" button text
30+ // we basically recreate a dropdown on top of this one here which is just an empty one to show something in the grid
31+ return `<div id="myDrop-r${ row } -c${ cell } " class="dropdown">
32+ <button class="btn btn-default btn-xs dropdown-toggle" type="button">
33+ Action
34+ <span class="caret"></span>
35+ </button>
36+ </div>` ;
37+ } ;
38+
2639@Component ( {
2740 templateUrl : './grid-angular.component.html' ,
2841 styleUrls : [ './grid-angular.component.scss' ] ,
@@ -218,7 +231,8 @@ export class GridAngularComponent implements OnInit {
218231 editor : {
219232 model : Editors . date
220233 } ,
221- }
234+ } ,
235+ { id : 'action' , name : 'Action' , field : 'id' , formatter : customActionFormatter , width : 70 }
222236 ] ;
223237
224238 this . gridOptions = {
@@ -329,8 +343,46 @@ export class GridAngularComponent implements OnInit {
329343 const componentOutput = this . angularUtilService . createAngularComponent ( colDef . params . component ) ;
330344 Object . assign ( componentOutput . componentRef . instance , { item : dataContext } ) ;
331345
332- // use a delay to make sure Angular ran at least a full cycle and it finished rendering the Component
346+ // use a delay to make sure Angular ran at least a full cycle and make sure it finished rendering the Component
333347 setTimeout ( ( ) => $ ( cellNode ) . empty ( ) . html ( componentOutput . domElement ) ) ;
334348 }
335349 }
350+
351+ /* Create an Action Dropdown Menu */
352+ deleteCell ( rowNumber : number ) {
353+ const item = this . angularGrid . dataView . getItem ( rowNumber ) ;
354+ this . angularGrid . gridService . deleteItemById ( item . id ) ;
355+ }
356+
357+ onActiveCellChanged ( event , args ) {
358+ if ( args . cell !== 6 ) {
359+ return ; // don't do anything unless it's the Action column which is at position 6 in this grid
360+ }
361+
362+ $ ( '#myDrop' ) . remove ( ) ; // make sure to remove previous Action dropdown, you don't want to have 100 after a 100 clicks...
363+ const cell = args . cell ;
364+ const row = args . row ;
365+
366+ // hide the dropdown we created as a Formatter, we'll redisplay it later
367+ const cellPos = $ ( `#myDrop-r${ row } -c${ cell } ` ) . offset ( ) ;
368+
369+ const componentOutput = this . angularUtilService . createAngularComponent ( CustomActionFormatterComponent ) ;
370+
371+ // pass "this" and the row number to the Component instance (CustomActionFormatter) so that we can call "parent.deleteCell(row)" with (click)
372+ Object . assign ( componentOutput . componentRef . instance , { parent : this , row : args . row } ) ;
373+
374+ // use a delay to make sure Angular ran at least a full cycle and make sure it finished rendering the Component before using it
375+ setTimeout ( ( ) => {
376+ const elm = $ ( componentOutput . domElement ) ;
377+ elm . appendTo ( 'body' ) ;
378+ elm . css ( 'position' , 'absolute' ) ;
379+ elm . css ( 'top' , cellPos . top + 5 ) ;
380+ elm . css ( 'left' , cellPos . left ) ;
381+ $ ( '#myDrop' ) . addClass ( 'open' ) ;
382+
383+ $ ( '#myDrop' ) . on ( 'hidden.bs.dropdown' , ( ) => {
384+ $ ( `#myDrop-r${ row } -c${ cell } ` ) . show ( ) ;
385+ } ) ;
386+ } ) ;
387+ }
336388}
0 commit comments