Create Json string From datatable selected Selected row

Create Json string From datatable selected Selected row

richapathakrichapathak Posts: 6Questions: 4Answers: 0

Hi,

I need to Convert data of selected row of datatable in Json form.
I am able to get the Row data on button click but i don't know how to convert it in json.

I have data in this form. This is data of two Rows.

1,Nancy,Davolio,Sales Representative,507 - 20th Ave. E.Apt. 2A,12/08/1948,
2,Andrew,Fuller,Vice President, Sales,908 W. Capital Way,02/19/1952

This is my code

[Jquery]

$(document).ready(function () {
$.ajax({
type: "POST",
url: "DatatableGrid.aspx/GetData",
contentType: "application/json; charset=utf-8",
data: "{'startIndex' :" + "0" + ",'maximumRows' :" + "100" + "}",
dataType: "json",
success: function (response) {
LoadData(response.d, "#grid");
},
failure: function (errMsg) {
$('#errorMessage').text(errMsg); //errorMessage is id of the div
},
error: function (errMsg) {
alert("Error!!");
}

        });
    });
    function LoadData(result, grid) {
        var dtData = [];
        if (grid == "#grid") {
            $.each(result, function () {
                var date = this.BirthDate;
                var newdate = ConvertJsonDateString(date);
                this.BirthDate = newdate;
                dtData.push([
                              this.EmployeeID,
                              this.FirstName,
                              this.LastName,
                              this.Title,
                              this.Address,
                              this.BirthDate
        ]);

            });
            var table; var data;
            table = $(grid).dataTable({  //grid is the id of the table
                'aaData': dtData
            });

            table.$('tr').click(function () {
                $(this).toggleClass('selected');
            });

        }
    }

function GetSelectedRow() {
var table = $('#grid').Datatbale();
var data = table.rows(['.selected']).data().toArray();
alert(data);
}

[HTML]

Emplee ID First Name Last Name Title Address BirthDate
Loading....

Thanks
Richa

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,934Questions: 1Answers: 10,155 Site admin
    Answer ✓
    var data = table.rows(['.selected']).data().toArray();
    var json = JSON.stingify( data );
    

    should be all that is needed. If that doesn't work, please follow the forum rules and link to a test case showing the issue.

    Allan

  • richapathakrichapathak Posts: 6Questions: 4Answers: 0

    Thank u allan.. You are Just amazing..

This discussion has been closed.