1

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 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

He can also get data like: $(this).data('id')
0

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>';

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.