Click here to Skip to main content
16,004,806 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Friends,

I have bind values in jquery datatable. without any button in datatable executes well. when i add button in datatable didn't show values at pageload.

What I have tried:

function BindGridview() {
       $.ajax({
           type: "POST",
           contentType: "application/json; charset=utf-8",
           url: "Employee.aspx/BindGridview",
           data: "{}",
           dataType: "json",
           success: function (data) {
        $('#MyTable').DataTable({
                   "aaData": JSON.parse(data.d),
                   "columns": [{ "data": "SNo", sortable: false },
                               { "data": "Id", "visible": false, sortable: false },
                               { "data": "EmployeeId", sortable: false },
                               { "data": "EmployeeName" }, { "data": "DOJ" },
                               { "data": "Contact", sortable: false },
                               { "data": "AltContact", sortable: false },
                               {
                                 mRender: function (data, type, row) {
return '<a href="" class="table-edit" data-id="Id" onclick="Edit(' + row.Id + ')">EDIT</a> / <a href="" class="editor_remove" onclick="Delete(' + row.Id + ')">Delete</a>'
                       }
                   }
                   ]
               });
           },
           error: function (data) {
               var r = data.responseText;
               var errorMessage = r.Message;
               alert(errorMessage);
           }
       });
   }
Posted
Updated 10-Aug-17 19:01pm

1 solution

refer this example and correct the errors, make sure you have defined extra two th in the header of the table.

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="http://localhost:45673/dt/buttons.dataTables.min.css" rel="stylesheet" />
    <link href="http://localhost:45673/dt/jquery.dataTables.min.css" rel="stylesheet" />
    <script src="http://localhost:45673/dt/jquery-1.12.4.js"></script>
    <script src="http://localhost:45673/dt/jquery.dataTables.min.js"></script>
    <script src="http://localhost:45673/dt/dataTables.buttons.min.js"></script>
</head>
<body>
    <table id="MyTable" style="width:400px">
        <thead>
            <tr>
                <th>Slno</th>
                <th>Contact</th>
                <th> </th>
                <th> </th>
            </tr>
        </thead>
        <tbody></tbody>
    </table>
    <script>
        var input = [{ Id: 1, Contact: 'aa' },
        { Id: 2, Contact: 'bb' },
        { Id: 3, Contact: 'cc' }]

        $('#MyTable').DataTable({
            "aaData": input,
            "columns": [{ "data": "Id", sortable: false },

                        { "data": "Contact", sortable: false },
                        {
                            mRender: function (data, type, row) {
                                return '<a href="#" class="table-edit" data-id="Id" onclick="Edit(' + row.Id + ')">EDIT</a> / <a href="#" class="editor_remove" onclick="Delete(' + row.Id + ')">Delete</a>'
                            }
                        }
            ]
        });

    </script> 
</body>
</html>
 
Share this answer
 
v2
Comments
Vivek.anand34 11-Aug-17 7:07am    
No this is also not come in page load
Karthik_Mahalingam 11-Aug-17 7:49am    
meaning?
Vivek.anand34 11-Aug-17 7:14am    
I gave that function like this its right..
$(document).ready(function () {
BindGridview();
});
Karthik_Mahalingam 11-Aug-17 7:49am    
post your code
Vivek.anand34 11-Aug-17 23:25pm    
without that button code executes well in pageload. with button didn't in pageload.

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