This is a migrated thread and some comments may be shown as answers.

Using a Kendo UI Window as a confirmation prompt in a Grid...

11 Answers 1276 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Neil
Top achievements
Rank 1
Neil asked on 02 Oct 2012, 10:01 AM
Hi,

I have searched the documentation and the forums but have not managed to find a topic on this issue. I would like to know if it is possible to define a template to use for the delete command prompt in a Grid control.

I'm using MVC and I currently have grids on certain pages that make use of the 'Delete' command button. My problem is that when using the delete command the command prompt is the default ugly browser one which is inconsistent with the theme of my application (I am aware that I can hide the prompt altogether but I'd like to keep it and if I use a custom command button instead then I lose the icon on the button which I don't want either).

On other forms I have been able to make use of the Kendo UI Window as a prompt which is working fine and I would like to be able to use this same style prompt in the Grid too. 

Is this possible?

Thanks,

Neil.

11 Answers, 1 is accepted

Sort by
0
Nohinn
Top achievements
Rank 1
answered on 02 Oct 2012, 11:03 AM
It is possible, I will assume we are working with inline edit mode, if not you will have to adapt it.
First on your editable property of kendoGrid:
$('#grid').kendoGrid({
    editable: {
        mode: "inline",
        confirmation: false
    },
    ....
});

In the commands column you should set this:
{ command: ["edit", {name: "custom", text: "Delete"}], title: " ", width: "210px" }

In your css (to make it look like the original delete button):
.k-grid-custom {
  margin-top: -.2em;
  margin-bottom: -.2em;
}
 
.k-grid-custom > span {
  margin: 0 3px 0 -3px;
  vertical-align: text-top;
  background-image: url('Default/sprite.png');
  background-position: -32px -16px;
  display: inline-block;
  width: 16px;
  height: 16px;
  overflow: hidden;
  background-color: transparent;
  background-repeat: no-repeat;
  font-size: 0;
  line-height: 0;
  text-indent: -3333px;
  text-align: center;
}

And then with javascript too using the grid dataBound callback, bind the click on this custom button:
dataBound: function() {
  $('#grid').find('.k-grid-custom').click(function() {
    $('<div id="confirmWindow">Are you sure you want to delete this record?<p><input type="button" id="btnYes" value="Yes" /><input type="button" id="btnNo" value="No" /></p></div>').appendTo(document.body);
    $('#confirmWindow').data('row', $(this).parents('tr:first'));                           
    $('#confirmWindow').kendoWindow({
      modal: true,
      close: function() {
        setTimeout(function(){
          $('#confirmWindow').kendoWindow('destroy');
        }, 200);
      }
    });
                           
    $('#confirmWindow').find('#btnNo').click(function(){
      $('#confirmWindow').kendoWindow('close');
    });
    $('#confirmWindow').find('#btnYes').click(function() {
      var g = $("#grid").data('kendoGrid');
      g.removeRow($('#confirmWindow').data('row')[0]);
      $('#confirmWindow').kendoWindow('close');
    });
                           
  });
}


Here you have a sample with all these changes: http://jsbin.com/uwecuh/1 
0
Neil
Top achievements
Rank 1
answered on 02 Oct 2012, 02:48 PM
Hi Nohinn,

That's perfect thanks! I had to adjust it for my MVC wrapper, but it works exactly as I need it to.

Much appreciated,

Neil.

Edit: One minor point though the CSS for 
.k-grid-custom {}

actually renders as .k-grid-Custom {} 

Note the uppercase 'C' in custom :)
0
Nohinn
Top achievements
Rank 1
answered on 02 Oct 2012, 03:08 PM
It is rendering as k-grid-Custom because you must have set the command name to "Custom", the name you set there will be added to "k-grid-".
Anyway I'm glad it helped you :)
0
Neil
Top achievements
Rank 1
answered on 02 Oct 2012, 03:10 PM
Ah that explains it...

Thanks again,

Neil.
0
M
Top achievements
Rank 1
answered on 30 Oct 2012, 08:40 AM
Hi

I've just stumbled across this and it's just what I'm after.

I've adapted your example to use a Twitter Bootstrap modal windows and it's displays perfectly.

Do you have any idea how to actually delete the row (from the remote datasource) rather than just remove it from the DOM?

Thanks
0
M
Top achievements
Rank 1
answered on 30 Oct 2012, 09:31 AM
This is the code I have currently. It's the actual removal of the record from the dataSource that I can't get working:
dataBound: function() {
            $('#grid').find('.k-grid-custom').click(function() {
                $('<div id="confirmWindow" class="modal hide fade" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"><div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button><h3 id="myModalLabel">Delete Assignment?</h3></div><div class="modal-body"><p>Are you sure you want to delete this record?</p></div><div class="modal-footer"><button class="btn" data-dismiss="modal" aria-hidden="true">Close</button><button id="btnYes" class="btn btn-warning">Delete</button></div></div>').appendTo(document.body);
             
                $('#confirmWindow').data('row', $(this).parents('tr:first'));
 
                $('#confirmWindow').modal({
                    show: true }
                );
 
                $('#confirmWindow').find('#btnYes').click(function() {
                    var g = $("#grid").data('kendoGrid');
                    var i = $('#confirmWindow').data('row')[0];
                    //var di = g.dataItem($('#confirmWindow').data('row')[0]);
                    g.removeRow(i);
                    //g.dataSource.remove(di);
                    $('#confirmWindow').modal('hide');
                });
            });
        },

I don't seem to be able to retrieve the dataItem to call dataSource.remove. Chrome's console gives: Uncaught TypeError: Cannot read property 'uid' of undefined console

because g.dataItem($('#confirmWindow').data('row')[0])
is returning undefined

I think I'm close, can somebody please help me get past the final hurdle.

Thanks
0
M
Top achievements
Rank 1
answered on 30 Oct 2012, 10:42 AM
Ahhh!

I was being stupid... I'm trying to add this to a detail grid, but was getting rows from the parent grid.

Fixed it now by assigning a variable to the detail grid using 
var detail = detailRow.find(".assignments").kendoGrid({...

This simplifies the custom delete routine too:
// detail grid based on Detail Template example in demos
var
detail = detailRow.find(".assignments").kendoGrid({
    dataBound: function() {
        detail.find('.k-grid-custom').click(function() {
            // rowIndex of clicked row
            var rowIndex = $(this).closest('tr').prevAll().length;
     
            $('<div id="confirmWindow" class="modal hide fade" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"><div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button><h3 id="myModalLabel">Delete Assignment?</h3></div><div class="modal-body"><p>Are you sure you want to delete this record?</p></div><div class="modal-footer"><button class="btn" data-dismiss="modal" aria-hidden="true">Close</button><button id="btnYes" class="btn btn-warning">Delete</button></div></div>').appendTo(document.body);
 
            $('#confirmWindow').modal({
                show: true }
            );
 
            $('#confirmWindow').find('#btnYes').click(function(e) {
                var g = detail.data('kendoGrid');
                var item = g.tbody.find('>tr:eq('+rowIndex+')');
                g.removeRow(item);
                $('#confirmWindow').modal('hide');
            });
        });
    },
    editable: {
        mode: 'inline',
        confirmation: false
    }
});
 
// most of grid definition removed

Hope this is useful to somebody.
0
T.
Top achievements
Rank 1
answered on 10 Apr 2013, 07:38 AM
Hello Nohinn,

I tested your code on http://jsbin.com/uwecuh/1 but there is one problem I found.
The Delete button does not trigger the click-event if the button Edit and then Cancel is clicked before,
so the confirmation-window does not come up.

Here is how to rebuild / test the problem:

1. Go to http://jsbin.com/uwecuh/1#
2. Click the Delete button, (e.g. within 1st row), the window comes up - so no problem at all, close the window again, by clicking No
3. Click the Edit button (of 1st row)
4. Click the Cancel button (of 1st row)
5. Click the Delete button (of 1st row)
6. The confirmation-window does not come up anymore!?!
7. Click the Delete button of any other row, the window comes up again
8. Click the Delete button of 1st row, the window does not come up!?!

So it seems to be an issue if Edit  and Cancel  is clicked and then the confirmation-window does not come up anymore
Is this a bug or a feature?

How to solve this problem?

Thanks for any help!




0
Alexander Valchev
Telerik team
answered on 15 Apr 2013, 07:57 AM
Hi Timon,

The provided example uses outdated version of Kendo UI. To fix the issue, please use perform the following steps:
  1. Updated to the latest official release (2013.1.319).
  2. Attach the event handler of the destroy button via columns.command.click property
    {name: "custom", text: "Delete", click: deleteClick }
  3. Build and show the confirm dialog:
    function deleteClick(e) {
      var row = $(e.target).closest("tr");
     
      $('<div id="confirmWindow">Are you sure you want to delete this record?<p><input type="button" id="btnYes" value="Yes" /><input type="button" id="btnNo" value="No" /></p></div>').appendTo(document.body);
       
      $('#confirmWindow').kendoWindow({
        modal: true,
        close: function() {
          setTimeout(function(){
            $('#confirmWindow').kendoWindow('destroy');
          }, 200);
        }
      });
       
      $('#confirmWindow').find('#btnNo').click(function(){
        $('#confirmWindow').kendoWindow('close');
      });
       
      $('#confirmWindow').find('#btnYes').click($.proxy(function() {
        var grid = $("#grid").data('kendoGrid');
        grid.removeRow(this);
        $('#confirmWindow').kendoWindow('close');
      }, row));
       
    }
For your convenience I updated your jsBin sample: http://jsbin.com/uwecuh/15/edit

Kind regards,
Alexander Valchev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
T.
Top achievements
Rank 1
answered on 15 Apr 2013, 09:35 AM
Hi Alexander Valchev,

thanks that helped a lot!

But two more questions:

1: Why the window's close function is not called directly but using a setTimout with delay of 200ms?

2. What is the $.proxy(...) used for in the #btnYes click funktion?


Thanks again!
0
Alexander Valchev
Telerik team
answered on 15 Apr 2013, 12:04 PM
Hello Timon,

Regarding your questions:
  1. This was part of the original implementation. I am not sure what is the purpose of 200ms timeout. Actually it is not necessary to create and destroy the window widget every time, you can just open and close it.
  2. $.proxy is used to change the context of a JavaScript function. I used it to pass a reference to the row element inside click event handler.
    $.proxy(function() {
      var grid = $("#grid").data('kendoGrid');
      grid.removeRow(this); //'this' represents 'row'
      $('#confirmWindow').kendoWindow('close');
    }, row)

For more information please check jQuery API reference:

Regards,
Alexander Valchev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Tags
Grid
Asked by
Neil
Top achievements
Rank 1
Answers by
Nohinn
Top achievements
Rank 1
Neil
Top achievements
Rank 1
M
Top achievements
Rank 1
T.
Top achievements
Rank 1
Alexander Valchev
Telerik team
Share this question
or