disable delete action for child grid if number of row equal 1

how I can disable delete action for costume  child grid if number of rows for this child is equal 1 ?

otherwise if any child have more than one row enable delete action 

Image_9853_1695488088972


3 Replies 1 reply marked as answer

DM Dineshnarasimman Muthu Syncfusion Team September 25, 2023 06:36 PM UTC

Hi Enas,


Greetings from Syncfusion Support.


We have reviewed your requirement about disabling the delete item in toolbar when the child grid has only one record. This requirement have been achieved by using actionComplete event and dataBound event to the child grid. In the actionComplete event, using row length for condition and disable the delete toolbar Item. The delete item in toolbar is hidden in dataBound event for initial rendering. The code snippet of the implementation and sample have been attached for your reference.

.


  actionComplete(args) {

    if (args.requestType === 'delete') {

      let toolbarItems = (this as any).toolbarModule.toolbar.items;

      if (args.rows.length < 2) {

        for (let i = 0i < toolbarItems.lengthi++) {

          if (toolbarItems[i].text === 'Delete') {

            toolbarItems[i].disabled = true;

          }

        }

      }

    }

  }

 

  dataBound(args) {

    let toolbarItems = (this as any).toolbarModule.toolbar.items;

    if ((this as any).getRows().length < 2) {

      for (let i = 0i < toolbarItems.lengthi++) {

        if (toolbarItems[i].text === 'Delete') {

          toolbarItems[i].disabled = true;

        }

      }

    }

  }

 

 



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



Please let us know if you need any further assistance.


Regards,

Dineshnarasimman M


Marked as answer

EN enas September 26, 2023 09:10 AM UTC

It did not work for me. The delete action is still active

this is my code :


 childGrid: {

                        dataSource: "",

                        allowFiltering: true,

                        filterSettings: { type: 'Menu' },

                        queryString: 'WH_REQ_ID',

                        allowPaging: true,

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

                        editSettings: { allowEditing: true, allowAdding: true, allowDeleting: true, mode: 'Normal', newRowPosition: 'Top' },

                        columns: [

                            {

                                field: 'WH_REQUESTS_ITEM_ID', headerText: gLungPage.WH_REQUESTS_ITEM_ID, isPrimaryKey: true, textAlign: 'Right',

                                width: 90, validationRules: { required: true }, visible: false

                            },

                            {

                                field: 'WH_ITEM_NAME', headerText: gLungPage.lblItem, textAlign: 'Right',

                                width: 300, validationRules: { required: true },

                                editType: 'dropdownedit', allowEditing: false,

                                edit: {

                                    params: {

                                        popupHeight: '300px',

                                        query: new ej.data.Query(),

                                        dataSource: dataSource_dropdown_ITems,

                                        fields: { value: 'text', text: 'text' },

                                        allowFiltering: true,

                                        enableFilterSearch: true,

                                        filterType: "Contains",

                                        change: onChange

                                    }



                                }

                            },

                            {

                                field: 'WH_ITEM_ID', headerText: gLungPage.lblNumItem, textAlign: 'Right',

                                width: 140, validationRules: { required: true }, allowEditing: false

                            },

                            {

                                field: 'ITEM_CODE', headerText: gLungPage.lblUnitCode, textAlign: 'Right', allowEditing: false,

                                width: 140, validationRules: { required: true }

                            },

                            {

                                field: 'UNIT_PRICE', headerText: gLungPage.lblUnitPrice, textAlign: 'Right', allowEditing: false,

                                width: 160, validationRules: { required: true },

                            },

                            {

                                field: 'QUANTITY', headerText: gLungPage.lblQuantity, textAlign: 'Right',

                                width: 160, validationRules: { required: true }, editType: 'numericedit',

                                edit: {

                                    create: function () {

                                        stockElem = document.createElement('input');

                                        return stockElem;

                                    },

                                    read: function () {

                                        return stockObj.value;

                                    },

                                    destroy: function () {

                                        stockObj.destroy();

                                    },

                                    write: function (args) {

                                        stockObj = new ej.inputs.NumericTextBox({

                                            value: args.rowData[args.column.field],

                                            change: function (args) {

                                                var formEle = grid.element.querySelector('form').ej2_instances[0];

                                                var totalCostFieldEle = formEle.getInputElement('TOTAL_PRICE');

                                                var UNIT_PRICEFieldEle = formEle.getInputElement('UNIT_PRICE');

                                                totalCostFieldEle.value = UNIT_PRICEFieldEle.value * stockObj.value;

                                            }

                                        });

                                        stockObj.appendTo(stockElem);

                                    }

                                },

                            },

                            {

                                field: 'TOTAL_PRICE', headerText: gLungPage.lblTotalPrice, textAlign: 'Right', allowEditing: false,

                                width: 160, validationRules: { required: true },

                            },

                            {

                                field: 'NURSING_NOTE', headerText: gLungPage.lblNotes, textAlign: 'Right',

                                width: 210, validationRules: { required: false }

                            }

                        ],

                        actionBegin: actionBegin,

                        actionComplete: actionComplete,

                        dataBound: dataBound,

                        aggregates: [

                        {

                            columns: [{

                                type: 'Count',

                                field: 'WH_ITEM_NAME',

                                footerTemplate: gLungPage.lblCountItem + ': ${Count}'

                            }]

                        }]

                        }


function actionComplete(args) {

    if (args.requestType === 'delete') {

        let toolbarItems = this.toolbarModule.toolbar.items;

        if (args.rows.length < 2) {

            for (let i = 0; i < toolbarItems.length; i++) {

                if (toolbarItems[i].text === 'Delete') {

                    toolbarItems[i].disabled = true;

                }

            }

        }

    }

}


function dataBound(args) {

    let toolbarItems = this.toolbarModule.toolbar.items;

    if (this.getRows().length < 2) {

        for (let i = 0; i < toolbarItems.length; i++) {

            if (toolbarItems[i].text === 'Delete') {

                toolbarItems[i].disabled = true;

            }

        }

    }

}


Image_4779_1695719207074



DM Dineshnarasimman Muthu Syncfusion Team September 28, 2023 02:04 PM UTC

Hi Enas,


We have reviewed the code you have shared and tried to replicate the issue, unfortunately we are unable to replicate the issue. The sample have been attached for your reference.


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


We understand that you are facing this issue in your code. Can you please provide the following information which will be very useful to provide a better solution.


  • Upon reviewing your code we notice that you have set empty data in the childgrid dataSource. Can you please ensure how you have rendered data in the childgrid.
  • Can you please provide other customizations made in the grid.
  • Can you please provide a runnable sample, since we are unaware of the customizations made in the grid.
  • If possible can you please replicate the issue in the sample which we have provide which will be helpful to identify and resolve.



Regards,

Dineshnarasimman M


Loader.
Up arrow icon