Search code examples
javascriptjqueryslickgrid

How do I create a delete button on every row in slickgrid with confirmation?


As the title said it, how do I do it?, I am using this button created by jiri:

How do i create a delete button on every row using the SlickGrid plugin?

when I add an if(confirmation(msg)) inside the function it repeats me the msg ALOT maybe its because i refresh-ajax the table with each modification.

ask me if you need more info, I am still noob here in stackoverflow :P (also if there is someway to "kill" the function)

here is the button, iam using(link) i added the idBorrada to check whetever the id was already deleted and dont try to delete it twice, also here is a confirm, but when i touch cancel it asks me again.

$('.del').live('click', function(){ var me = $(this), id = me.attr('id'); //assuming you have used a dataView to create your grid //also assuming that its variable name is called 'dataView' //use the following code to get the item to be deleted from it if(idBorrada != id && confirm("¿Seguro desea eleminarlo?")){ dataView.deleteItem(id); Wicket.Ajax.ajax({"u":"${url}","c":"${gridId}","ep":{'borrar':JSON.stringify(id, null, 2)}}); //This is possible because in the formatter we have assigned the row id itself as the button id; //now assuming your grid is called 'grid' //TODO grid.invalidate(); idBorrada= id; } else{ }; });

and i call the entire function again. hope that help, sorry for the grammar its not my native language


Solution

  • Follow these steps,

    1. Add a delete link for each row with of the columns object as follows,
    var columns = 
       { id: "Type", name: "Application Type", field: "ApplicationType", width: 100, cssClass: "cell-title", editor: Slick.Editors.Text, validator: requiredFieldValidator, sortable: true },
       { id: "delete", name: "Action", width: 40, cssClass: "cell-title", formatter: Slick.Formatters.Link }
    ];
    
    1. Add a Link Formatter inside slick.formatters.js as follows,
    "Formatters": {
       "PercentComplete": PercentCompleteFormatter,
       "YesNo": YesNoFormatter,
       "Link": LinkFormatter
    }
    
    function LinkFormatter(row, cell, value, columnDef, dataContext) {
       return "<a style='color:#4996D0; text-decoration:none;cursor:pointer' onclick='DeleteData(" + dataContext.Id + ", " + row + ")'>Delete</a>";
    }
    
    1. Add the following delete function in javascript
    function DeleteData(id, rowId) {
       var result = confirm("Are you sure you want to permenantly delete this record!");
       if (result == true) {
           if (id) {
               $.ajax({
                   type: "POST",
                   url: "DeleteURL",
                   data: { id: id },
                   dataType: "text",
                   success: function () {
                   },
                   error: function () {
                   }
               });
           }
           dataView.deleteItem(id);
           dataView.refresh();}
    }