Hide/Disable the Destroy butons on Add/Edit in all rows

8 posts, 0 answers
  1. Andrey
    Andrey avatar
    49 posts
    Member since:
    Jul 2014

    Posted 20 Jun 2019 Link to this post

    Hello,

    I have a ui grid with code to hide the destroy button if only one record left in the grid:

    ...

    columns.Command(command => { command.Destroy().Visible("isLastRecordEmail").Text("Remove"); }).Width(200);

    ...

                                                function isLastRecordEmail(e) {
                                                    return $('\#GridEmails').data('kendoGrid').dataSource.data().length > 1;
                                                }

    But if I click the Add button then a new records appears and destroy button will appear for last record to allow to delete the very last record.

    My question is, how to hide the destroy button in this case?

    I did try to add code:

     function isLastRecordEmail(e) {
          if ($('\#GridEmails').EditIndexes.Count > 0) return false;

    but EditIndexes is undefined.

    Could you please help?

    Thanks!

  2. Andrey
    Andrey avatar
    49 posts
    Member since:
    Jul 2014

    Posted 20 Jun 2019 Link to this post

    I've attache two images to help understanding my problem.
  3. Eyup
    Admin
    Eyup avatar
    4046 posts

    Posted 24 Jun 2019 Link to this post

    Hello Andrey,

    You can resolve the issue by adding the following modification:
    function isLastRecordEmail(e) {
        var grid = $('\#grid').data('kendoGrid');
        var items = grid.items();
        var count = items.length;
     
        if (count === 2 && items[0].uid === items[1].uid) {
            count--;
        }
        return count > 1;
    }

    I hope this will prove helpful.

    Regards,
    Eyup
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
  4. Andrey
    Andrey avatar
    49 posts
    Member since:
    Jul 2014

    Posted 24 Jun 2019 in reply to Eyup Link to this post

    Perfect! Thank you so much!
  5. Andrey
    Andrey avatar
    49 posts
    Member since:
    Jul 2014

    Posted 24 Jun 2019 Link to this post

    Hi Eyup,

    but now the destroy button will be hidden for two records as well. I've attached the image.

  6. Andrey
    Andrey avatar
    49 posts
    Member since:
    Jul 2014

    Posted 24 Jun 2019 in reply to Andrey Link to this post

    The items[0].uid and items[1].uid are undefined in the line below. I am using version 2019.1.220 of Kendo ui mvc. FYI.

    if (count === 2 && items[0].uid === items[1].uid)

  7. Eyup
    Admin
    Eyup avatar
    4046 posts

    Posted 25 Jun 2019 Link to this post

    Hello Andrey,

    In this case try the following modification to your original approach:
    function isLastRecordEmail(e) {
         return $('\#GridEmails').data('kendoGrid').items().length > 1;
    }

    That should do the trick.

    Regards,
    Eyup
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
  8. Andrey
    Andrey avatar
    49 posts
    Member since:
    Jul 2014

    Posted 25 Jun 2019 in reply to Eyup Link to this post

    Works fine! Thank you so much!
Back to Top