How to implement a Master/Detail grid with editing options for both tables?

I saw the following example to implement a master/detail grid:

https://ej2.syncfusion.com/javascript/demos/#/bootstrap5/grid/master-detail.html

I have doubts about how to implement the CRUD in both tables without problems.


3 Replies 1 reply marked as answer

MA Mohammed Ansar Sathik Ali Syncfusion Team May 27, 2022 12:24 PM UTC

Hi Luis Guerreo,


Greetings from the Syncfusion support.


Query:How to implement a Master/Detail grid with editing options for both tables?


Based on your query, you want to enable the editing on both master and detail grid. We have achieved this by using the editSettings properties to both the grid. kindly ensure to define IsPrimaryKey column whose value is unique. Based on primarykey column value only edit operation will take place


var mastergrid = new ej.grids.Grid({

  dataSource: masterdata,

  selectedRowIndex: 1,

  editSettings: {

    allowEditing: true,

    allowAdding: true,

    allowDeleting: true,

    mode: 'Normal',

  },

  columns: [

     { field: 'ContactName', headerText: 'Customer Name',isPrimaryKey: true,  width: 150 },

    { field: 'CompanyName', headerText: 'Company Name', width: 150 },

    { field: 'Address', headerText: 'Address', width: 150 },

    { field: 'Country', headerText: 'Country', width: 130 },

  ],

  rowSelected: rowSelected,

});

mastergrid.appendTo('#MasterGrid');

 

function rowSelected(args) {

  var selRecord = args.data;

  grid.dataSource = window.data

    .filter(function (record) {

      return record.CustomerName === selRecord.ContactName;

    })

    .slice(0, 5);

  document.getElementById('key').innerHTML = selRecord.ContactName;

}

 

var grid = new ej.grids.Grid({

  allowSelection: false,

  editSettings: {

    allowEditing: true,

    allowAdding: true,

    allowDeleting: true,

    mode: 'Normal',

  },

 

  columns: [

{

      field: 'OrderID',

      isPrimaryKey: true,

      headerText: 'Order ID',

      width: 100,

      textAlign: 'Right',

    },

    {

      field: 'Freight',

      headerText: 'Freight',

      width: 100,

      format: 'C2',

      type: 'number',

    },

    { field: 'ShipName', headerText: 'Ship Name', width: 200 },

    { field: 'ShipCountry', headerText: 'Ship Country', width: 150 },

    { field: 'ShipAddress', headerText: 'Ship Address', width: 200 },

  ],

});

grid.appendTo('#DetailGrid');


Sample: https://stackblitz.com/edit/wyu7w4-zrth9m?file=index.js


Documentation: https://ej2.syncfusion.com/documentation/grid/editing/edit/


Please get back to us if you need further assistance on this.


Regards,

Mohammed Ansar ,



LG Luis Guerrero May 27, 2022 10:29 PM UTC

Thanks, is there a way to use the ToolBar to add, edit, update and delete both the master and detail grids?



MA Mohammed Ansar Sathik Ali Syncfusion Team May 30, 2022 12:27 PM UTC

Hi Luis Guerreo,


Query: How to implement a Master/Detail grid with editing options for both tables?


Based on your query, you want to enable the toolbar to both grids. We have achieved this by following method


import { Grid, Edit, Toolbar, Selection } from '@syncfusion/ej2-grids';

Grid.Inject(Edit, Toolbar, Selection);

 

var mastergrid = new ej.grids.Grid({

  dataSource: masterdata,

  selectedRowIndex: 1,

 

  toolbar: ['Add', 'Edit', 'Delete', 'Update', 'Cancel'],

  editSettings: {

    allowEditing: true,

    allowAdding: true,

    allowDeleting: true,

    mode: 'Normal',

  },

  columns: [

    {

      field: 'ContactName',

      headerText: 'Customer Name',

      isPrimaryKey: true,

      width: 150,

    },

    { field: 'CompanyName', headerText: 'Company Name', width: 150 },

    { field: 'Address', headerText: 'Address', width: 150 },

    { field: 'Country', headerText: 'Country', width: 130 },

  ],

  rowSelected: rowSelected,

});

mastergrid.appendTo('#MasterGrid');

 

var grid = new ej.grids.Grid({

  allowSelection: false,

  toolbar: ['Add', 'Edit', 'Delete', 'Update', 'Cancel'],

  editSettings: {

    allowEditing: true,

    allowAdding: true,

    selectionSettings: { type: 'Single' },

    allowDeleting: true,

    mode: 'Normal',

  },

 

  columns: [

    {

      field: 'OrderID',

      isPrimaryKey: true,

      headerText: 'Order ID',

      width: 100,

      textAlign: 'Right',

    },

    {

      field: 'Freight',

      headerText: 'Freight',

      width: 100,

      format: 'C2',

      type: 'number',

    },

    { field: 'ShipName', headerText: 'Ship Name', width: 200 },

    { field: 'ShipCountry', headerText: 'Ship Country', width: 150 },

    { field: 'ShipAddress', headerText: 'Ship Address', width: 200 },

  ],

});

grid.appendTo('#DetailGrid');

 


Sample: https://stackblitz.com/edit/wyu7w4-188riq?file=index.js,index.html


Please get back to us if you need further assistance on this.


Regards,

Mohammed Ansar ,


Marked as answer
Loader.
Up arrow icon