1- import { TranslateService } from '@ngx-translate/core' ;
21import { Injectable } from '@angular/core' ;
32import { CellArgs , Column , GridOption , OnEventArgs } from './../models/index' ;
43import { ExtensionService } from './extension.service' ;
@@ -18,8 +17,14 @@ export class GridService {
1817 onItemAdded = new Subject < any | any [ ] > ( ) ;
1918 onItemDeleted = new Subject < any | any [ ] > ( ) ;
2019 onItemUpdated = new Subject < any | any [ ] > ( ) ;
20+ onItemUpserted = new Subject < any | any [ ] > ( ) ;
2121
22- constructor ( private extensionService : ExtensionService , private filterService : FilterService , private gridStateService : GridStateService , private sortService : SortService , private translate : TranslateService ) { }
22+ constructor (
23+ private extensionService : ExtensionService ,
24+ private filterService : FilterService ,
25+ private gridStateService : GridStateService ,
26+ private sortService : SortService
27+ ) { }
2328
2429 /** Getter for the Grid Options pulled through the Grid Object */
2530 private get _gridOptions ( ) : GridOption {
@@ -360,7 +365,7 @@ export class GridService {
360365 */
361366 deleteItem ( item : any , shouldTriggerEvent = true ) {
362367 if ( ! item || ! item . hasOwnProperty ( 'id' ) ) {
363- throw new Error ( `deleteItem() requires an item object which includes the "id" property` ) ;
368+ throw new Error ( `Deleting an item requires the item to include an "id" property` ) ;
364369 }
365370 const itemId = ( ! item || ! item . hasOwnProperty ( 'id' ) ) ? undefined : item . id ;
366371 this . deleteItemById ( itemId , shouldTriggerEvent ) ;
@@ -374,7 +379,7 @@ export class GridService {
374379 deleteItems ( items : any [ ] , shouldTriggerEvent = true ) {
375380 // when it's not an array, we can call directly the single item delete
376381 if ( ! Array . isArray ( items ) ) {
377- this . deleteItem ( items ) ;
382+ this . deleteItem ( items , shouldTriggerEvent ) ;
378383 }
379384 items . forEach ( ( item : any ) => this . deleteItem ( item , false ) ) ;
380385
@@ -456,7 +461,7 @@ export class GridService {
456461 const itemId = ( ! item || ! item . hasOwnProperty ( 'id' ) ) ? undefined : item . id ;
457462
458463 if ( itemId === undefined ) {
459- throw new Error ( `Could not find the item in the grid or it's associated "id"` ) ;
464+ throw new Error ( `Calling Update of an item requires the item to include an "id" property ` ) ;
460465 }
461466
462467 return this . updateItemById ( itemId , item , shouldHighlightRow , shouldTriggerEvent ) ;
@@ -472,7 +477,7 @@ export class GridService {
472477 updateItems ( items : any | any [ ] , shouldHighlightRow = true , shouldTriggerEvent = true ) : number [ ] {
473478 // when it's not an array, we can call directly the single item update
474479 if ( ! Array . isArray ( items ) ) {
475- this . updateItem ( items , shouldHighlightRow ) ;
480+ this . updateItem ( items , shouldHighlightRow , shouldTriggerEvent ) ;
476481 }
477482
478483 const gridIndexes : number [ ] = [ ] ;
@@ -509,7 +514,7 @@ export class GridService {
509514 const rowNumber = this . _dataView . getRowById ( itemId ) ;
510515
511516 if ( ! item || rowNumber === undefined ) {
512- throw new Error ( `Could not find the item in the grid or it's associated "id"` ) ;
517+ throw new Error ( `Deleting an item requires the item to include an "id" property ` ) ;
513518 }
514519
515520 const gridIdx = this . _dataView . getIdxById ( itemId ) ;
@@ -537,38 +542,35 @@ export class GridService {
537542 * Insert a row into the grid if it doesn't already exist or update if it does.
538543 * @param item object which must contain a unique "id" property and any other suitable properties
539544 * @param shouldHighlightRow do we want to highlight the row after update
545+ * @param shouldResortGrid defaults to false, do we want the item to be sorted after insert? When set to False, it will add item on first row (default)
540546 * @param shouldTriggerEvent defaults to true, which will trigger an event (used by at least the pagination component)
541547 */
542- upsertItem ( item : any , shouldHighlightRow = true , shouldTriggerEvent = true ) : number {
548+ upsertItem ( item : any , shouldHighlightRow = true , shouldResortGrid = false , shouldTriggerEvent = true ) : number {
543549 const itemId = ( ! item || ! item . hasOwnProperty ( 'id' ) ) ? undefined : item . id ;
550+
544551 if ( itemId === undefined ) {
545- throw new Error ( `The item to be Upsert in the grid must have an associated "id" for it to be valid ` ) ;
552+ throw new Error ( `Calling Upsert of an item requires the item to include an "id" property ` ) ;
546553 }
547554
548- const rowNumber = this . _dataView . getRowById ( itemId ) ;
549-
550- if ( rowNumber === undefined ) {
551- return this . addItem ( item , shouldHighlightRow , shouldTriggerEvent ) ;
552- } else {
553- return this . updateItem ( item , shouldHighlightRow , shouldTriggerEvent ) ;
554- }
555+ return this . upsertItemById ( itemId , item , shouldHighlightRow , shouldResortGrid , shouldTriggerEvent ) ;
555556 }
556557
557558 /**
558559 * Update an array of existing items with new properties inside the datagrid
559560 * @param item object arrays, which must contain unique "id" property and any other suitable properties
560561 * @param shouldHighlightRow do we want to highlight the row after update
562+ * @param shouldResortGrid defaults to false, do we want the item to be sorted after insert? When set to False, it will add item on first row (default)
561563 * @param shouldTriggerEvent defaults to true, which will trigger an event (used by at least the pagination component)
562564 */
563- upsertItems ( items : any | any [ ] , shouldHighlightRow = true , shouldTriggerEvent = true ) : number [ ] {
565+ upsertItems ( items : any | any [ ] , shouldHighlightRow = true , shouldResortGrid = false , shouldTriggerEvent = true ) : number [ ] {
564566 // when it's not an array, we can call directly the single item update
565567 if ( ! Array . isArray ( items ) ) {
566- return [ this . upsertItem ( items , shouldHighlightRow ) ] ;
568+ return [ this . upsertItem ( items , shouldHighlightRow , shouldResortGrid , shouldTriggerEvent ) ] ;
567569 }
568570
569571 const gridIndexes : number [ ] = [ ] ;
570572 items . forEach ( ( item : any ) => {
571- gridIndexes . push ( this . upsertItem ( item , false , false ) ) ;
573+ gridIndexes . push ( this . upsertItem ( item , false , false , false ) ) ;
572574 } ) ;
573575
574576 // only highlight at the end, all at once
@@ -579,8 +581,36 @@ export class GridService {
579581
580582 // do we want to trigger an event after updating the item
581583 if ( shouldTriggerEvent ) {
582- this . onItemUpdated . next ( items ) ;
584+ this . onItemUpserted . next ( items ) ;
583585 }
584586 return gridIndexes ;
585587 }
588+
589+ /**
590+ * Update an existing item in the datagrid by it's id and new properties
591+ * @param itemId: item unique id
592+ * @param item object which must contain a unique "id" property and any other suitable properties
593+ * @param shouldHighlightRow do we want to highlight the row after update
594+ * @param shouldResortGrid defaults to false, do we want the item to be sorted after insert? When set to False, it will add item on first row (default)
595+ * @param shouldTriggerEvent defaults to true, which will trigger an event (used by at least the pagination component)
596+ * @return grid row index
597+ */
598+ upsertItemById ( itemId : number | string , item : any , shouldHighlightRow = true , shouldResortGrid = false , shouldTriggerEvent = true ) : number {
599+ if ( itemId === undefined ) {
600+ throw new Error ( `Calling Upsert of an item requires the item to include a valid and unique "id" property` ) ;
601+ }
602+
603+ let rowNumber : number ;
604+ if ( this . _dataView . getRowById ( itemId ) === undefined ) {
605+ rowNumber = this . addItem ( item , shouldHighlightRow , shouldResortGrid , shouldTriggerEvent ) ;
606+ } else {
607+ rowNumber = this . updateItem ( item , shouldHighlightRow , shouldTriggerEvent ) ;
608+ }
609+
610+ // do we want to trigger an event after updating the item
611+ if ( shouldTriggerEvent ) {
612+ this . onItemUpserted . next ( item ) ;
613+ }
614+ return rowNumber ;
615+ }
586616}
0 commit comments