Click here to Skip to main content
15,877,103 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am not getting the checkbox checked row from jquery table data, i want to get the data for checked rows and then i want to store the checked rows data into object and thn i want to pass to controller / action method as object.
can some one please help me

What I have tried:

$(function () {
$(document).on("click", "#btnAddtoMailing", function () {
debugger;
var getselectedvalues = $('#dataTable input:checked').parents("tr")
var row = $('#dataTable input:checked').closest('tr').data();

alert(row);

//var count = $('#dataTable').DataTable().rows('.selected').count();

//var checked_rows = $('#dataTable').DataTable().rows('.selected').data();
//alert(count);
//console.log(row);

$.ajax({
type: "POST",

url: '/Home/SelectedData',

data: '{obj: ' + JSON.stringify(row) + '}',

dataType: "json",

success: function (data) {
;
alert("success");


},

error: function (xhr, ajaxOptions, thrownError) {
alert('Failed to retrieve data.');
}
});
})
});
Posted
Updated 6-Apr-23 22:30pm

1 solution

for jQuery data table select, you should follow this instruction.

If You want to add function handler inside of jQuery data table action button, you can make the handler function outside of datatable component.

first, this is the default of grid code with select data action button.
JavaScript
$(document).ready(function() {
    var events = $('#events');
    var table = $('#example').DataTable( {
        dom: 'Bfrtip',
        select: true,
        buttons: [
            {
                text: 'Get selected data',
                action: function () {
                    var dataselected= table.rows( { selected: true } );

                    console.log("dataselected:", dataselected);

                }
            }
        ]
    } );
} );


Check at console browser and paste here what is console.log printed, at comments of this thread.

then replace this block:
JavaScript
console.log("dataselected:", dataselected);


with your custom function. i.e. PostSelectedData(dataselected);
JavaScript
var PostSelectedData = function(dataselected) {
    $.ajax({
        type: "POST",
        url: '/Home/SelectedData',
        data: '{obj: ' + JSON.stringify(row) + '}',
        dataType: "json",
        success: function(data) {
            alert("success");
            console.log("Success: ", data)
        },

        error: function(xhr, ajaxOptions, thrownError) {
            alert('Failed to retrieve data.');
            console.log("Failed:", xhr, ajaxOptions, thrownError)
        }
    });
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900