skip to Main Content

I have a dynamic jQuery data table. For the final column, I have the option of deleting rows. In order for this to execute, I need to pass the itemId value to the function specified within the button onClick attribute.

This is how I have currently done it but no luck:

defaultContent: "<button class='btn btn-danger' onclick='StockSearchManagment.DeleteStock(" + data.itemId + ")'>Delete</button>"

This is my full JavaScript function:

 GetInfo: function (tble) {
$(document).ready(function () {
    $.ajax({
        url: '/Home/GetList',
        dataType: "json",
        method: 'post',
        success: function (data) {
            tble.DataTable().destroy();
            tble.DataTable({
                data: data.html
                ,
                "columns": [
                    { data: "itemId" },
                    { data: "name" },
                    { data: "description" },
                    {
                        data: null,
                        defaultContent: "<button class='btn btn-danger' onclick='StockSearchManagment.DeleteStock(" + data.ItemId + ")'>Delete</button>"
                    }
                ]
            });
        },
        error: function (err) {
            alert(err);
        }
    });
});

}

2

Answers


  1. Chosen as BEST ANSWER

    I refered to this stack overflow question: Passing more than one value into a button in the server side Datatable Jquery using MVC

    It's now working with render instead of defaultContext:

    "render": function (data, type, full) {
    
     return '<button class="btn btn-danger" onclick="StockSearchManagment.DeleteStock(' + full.itemId + ')">Delete</button>';
    
    }
    

  2. you can tag the button you generate with a "data" tag and read that from the onlick event.

    the tag would look like:

    <button … data-id="">

    the onlick event would do this: var itemid = $(this).attr(‘data-id’).val();

    and then you’d have the value to use.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search