简体   繁体   中英

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, 在此处输入图片说明

waiting for replies

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" }
    ]
}); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM