Karthik Tv
Karthik Tv

Reputation: 35

Can not add rows using datatable.js Ajax

I tried to create table using datatable.js ajax. I'm getting data from webmethod , but, result is not adding into the tables.

JS Method

 function getMyData() {
            alert('d');
            $.ajax({
                type: "POST",
                url: "AssignHistory.aspx/getModemAssign ",
                data: "{}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: buildMyDatatable,
                error:
                    function (msg) {
                        alert(msg.status + " " + msg.statusText);
                    }
            });

            function buildMyDatatable(result) { 
                var data = JSON.stringify(result.d);
                $('#gvAssgin').dataTable({
                    retrieve: true,
                    JSON:data,
                    columns: [
                            { data: "ModemId" },
                            { data: "ModemName" }
                    ]
                }); 
            }
        } 

HTML Code

   <table id="gvAssgin">
      <thead>
        <tr>
             <th>
                   Modem ID 
             </th>
             <th>
                   Modem Name 
             </th>
         </tr>
   </thead>                                 

Result I'm getting is, enter image description here

waiting for replies

Upvotes: 1

Views: 58

Answers (1)

Gyrocode.com
Gyrocode.com

Reputation: 58880

Correct option of JavaScript sourced data is data. Also there is no need to generate JSON again with JSON.stringify(result.d), just pass the array to jQuery DataTables.

See the corrected code below:

$('#gvAssgin').dataTable({
    data: result.d,
    columns: [
       { data: "ModemId" },
       { data: "ModemName" }
    ]
}); 

Upvotes: 1

Related Questions