We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Hierarchy Grid. requestType: Save Different data from Add and Edit

Add has all the row data listed:



Edit does not have all the row data:

My problem is that on Save both triggered by Edit or Add record I want to loop over the rows collect all the workers for all rows into an array and write it to database in a separate place.

In the case of adding a record this worked fine with it being on actionComplete, but it breaks when editing for the above reason. So instead I used databound trigger and used the same data as the datasource but this means it triggers this on every databound event and I am trying to keep the main grid expanded on databound event so I get a lot of flickering on each databound event.  

Is there a way for me access row data on actioncomplete so I could do it so

    actionComplete: (args: any) => {
      console.log(args)
      switch (args.requestType) {
        case 'save':
          this.firebasedb.updateJobs(JSON.parse(JSON.stringify(args.data)),this.parentKey);
          this.storeExpandedIndexes();
          let workers: string[] = [];
          workers = [];
         args.data.forEach((row: any) => {
            if (row.data.worker) workers.push(row.data.worker);
          });
          this.firebasedb.updateWorkers(workers, this.parentKey);
          break;
        case 'delete':

        args.data.forEach((row: any) => {
          this.firebasedb.removeJob(row.key, this.parentKey);
        });
       

          break;
      }
    },



So as I could not figure out any other way to grab row data before it triggers the action complete save

export class AdminComponent {
  parentKey: any;
  childdatasource

  constructor(
    public firebase: AngularFireDatabase,
    private fns: AngularFireFunctions,
    private firebasedb: FirebaseService,
    private toast: HotToastService
  ) {
    firebase
      .list('/main')
      .valueChanges()
      .subscribe((users) => {
        this.storeExpandedIndexes();
        this.mainGrid.dataSource = users; //intial data binding to grid
\;
      });

    firebase
      .list('/main')
      .snapshotChanges()
      .subscribe((users) => {
        this.storeExpandedIndexes();
        this.mainGrid.dataSource = users; // sync server data changes to grid
\
      });
  }

  @ViewChild('mainGrid')
  public mainGrid: GridComponent;
  public editSettings: EditSettingsModel;
  public toolbar: ToolbarItems[];
  public filterOption: FilterSettingsModel = { type: 'Excel',  columns: [
    { field: 'status', matchCase: false, operator: 'notEqual', predicate: 'or', value: 'Finished' }] };
  sortOptions: { columns: { field: string; direction: string }[] };
  public commandsInvoice: CommandModel[];

  public dropdownRates: object[] = [
    { value: '1', text: 'Hourly' },
    { value: '0', text: 'Flat' },
  ];
  public dropdown2: object[] = [
    { uid: 'Waiting', countryId: '1' },
    { uid: 'In Progress', countryId: '2' },
    { uid: 'Finished', countryId: '3' },
  ];
  public dropdownParams2 = {
    params: {
      allowFiltering: true,
      dataSource: new DataManager(this.dropdown2),
      fields: { text: 'uid', value: 'uid' },
      query: new Query(),
      actionComplete: () => false,
    },
  };
  public userparams = {
    params: {
      allowFiltering: true,
      fields: { text: 'email', value: 'email' },
      query: new Query(),
      actionComplete: () => false,
    },
  };

  public childGrid: GridModel = {
    toolbar: ['Add', 'Delete'],
    queryString: 'key',
    allowResizing: true,
    editSettings: {
      allowEditing: true,
      allowAdding: true,
      allowDeleting: true,
      mode: 'Dialog',
    },
    columns: [
      {
        field: 'jobTitle',
        headerText: 'Job Title',
        textAlign: 'Right',
        allowEditing: true,
        width: 120,
      },
      {
        field: 'worker',
        headerText: 'Worker Name',
        width: 100,
        editType: 'dropdownedit',
        edit: this.userparams,
      },
      {
        field: 'rateType',
        headerText: 'Rate Type',
        width: 50,
        allowEditing: true,
        foreignKeyValue: 'text',
        foreignKeyField: 'value',
        dataSource: this.dropdownRates,
      },
      {
        field: 'rateQty',
        headerText: 'Rate Qty',
        width: 50,
        allowEditing: true,
      },
      { field: 'ebay', headerText: 'Ebay', width: 50, allowEditing: true },
      {
        field: 'euroCarParts',
        headerText: 'EuroCarParts',
        width: 50,
        allowEditing: true,
      },
      { field: 'sdl', headerText: 'SDL', width: 50, allowEditing: true },
      { field: 'imex', headerText: 'IMEX', width: 50, allowEditing: true },
      { field: 'dawmac', headerText: 'Dawmac', width: 50, allowEditing: true },
      {
        field: 'salvageYard',
        headerText: 'SalvageYard',
        width: 50,
        allowEditing: true,
      },
      { field: 'oil', headerText: 'Oil', width: 50 },
      {
        field: 'priceMulti',
        headerText: 'Price Multi',
        width: 50,
        allowEditing: true,
      },
      { field: 'time', headerText: 'Spent Time', width: 50 },
      {
        field: 'date',
        headerText: 'Date',
        width: 70,
        type: 'date',
        format: 'dd/MM/yyyy',
        editType: 'datepickeredit',
      },
      { field: 'cof', headerText: 'Cof', width: 50, allowEditing: true },

      {
        field: 'price',
        headerText: 'Final Price',
        width: 50,
        allowEditing: false,
      },
      {
        field: 'salary',
        headerText: 'Worker Salary',
        width: 50,
        allowEditing: false,
      },
      { field: 'profit', headerText: 'Profit', width: 50, allowEditing: false },
    ],

   
    dataBound:(args: any) => {
      let workers: string[] = [];
      workers = [];
      this.childdatasource.forEach((row: any) => {
        if (row.worker) workers.push(row.worker);
      });
      this.firebasedb.updateWorkers(workers, this.parentKey);
  },



    actionComplete: (args: any) => {
      console.log(args)
      switch (args.requestType) {
        case 'save':
          this.firebasedb.updateJobs(JSON.parse(JSON.stringify(args.data)),this.parentKey);
          this.storeExpandedIndexes();
 
          break;
        case 'delete':

        args.data.forEach((row: any) => {
          this.firebasedb.removeJob(row.key, this.parentKey);
        });
       

          break;
      }
    },
  };

  public actionCompleteMain(args: any): void {
    switch (args.requestType) {
      case 'save':
        this.firebasedb.updateMain(JSON.parse(JSON.stringify(args.data)));
        break;
      case 'delete':
        args.data.forEach((row: any) => {
          this.firebasedb.removeMain(row.key);
        });

        break;
    }
  }



public indexes;
public expandFlag = false;
 
// Function for storing expanded parent row indexes
storeExpandedIndexes() {
    // Retrieves the expanded row elements
    var expandedElements = this.mainGrid.element.querySelectorAll('.e-detailrowexpand');
    this.indexes = [];
    // // Parent row indexes of the expanded child Grid are pushed to global variable – “indexes”
    expandedElements.forEach(ele => this.indexes.push(parseInt(ele.closest('.e-row').getAttribute("aria-rowindex"))))
    console.log(this.indexes)
}
 
public expandedIndexes($event) {
  this.mainGrid.detailRowModule.expand(this.indexes[0]-1)
  console.log("test")
}


  detailDataBound(e: any) {
    this.parentKey = e.data.key;

   
    this.firebase
      .list(`/main/${this.parentKey}/children`)
      .snapshotChanges()
      .subscribe((users) => {
 
        e.childGrid.query = new Query();
        e.childGrid.dataSource = users; // sync server data changes to grid
        this.childdatasource = users;
      });

    this.firebase
      .list(`/main/${this.parentKey}/children`)
      .valueChanges()
      .subscribe((users) => {
   
        e.childGrid.query = new Query();
        e.childGrid.dataSource = users; //intial data binding to grid
        this.childdatasource = users;
      });

     
  }

1 Reply

SI Santhosh Iruthayaraj Syncfusion Team April 27, 2023 09:56 AM UTC

Hi Frank,


Greetings from Syncfusion support.


Based on your query, you can get all the visible records data using the currentViewData property of the Grid. Please refer to the below code snippet to get current view records for child Grid inside actionComplete event.


[app.component.ts]

 

    let detail = new Grid({

      .  .  .  .  .      

      actionComplete(argsany) {

        if (args.requestType === 'save' && args.action === 'add') {

          console.log(args.rows);

        } else if (args.requestType === 'save' && args.action === 'edit') {

          console.log(args.row.closest('.e-childgrid').ej2_instances[0].currentViewData);

        }

      },

    });

 


Sample: https://stackblitz.com/edit/angular-l8ye2l-v2abcy?file=src%2Fapp.component.ts


API: https://ej2.syncfusion.com/angular/documentation/api/grid/#currentviewdata


Let us know if you have any further queries or concerns.


Regards,

Santhosh I


Loader.
Up arrow icon