Click here to Skip to main content
15,885,869 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to do searching filtering and sorting in web API using datatable js library. But nothing is showing up on load.

What I have tried:

At Layout.CSHTML
@Scripts.Render("~/bundles/lib")
    @RenderSection("scripts", required: false)
    
</body>


in Bundles.Config

bundles.Add(new ScriptBundle("~/bundles/lib").Include(
                        "~/Scripts/jquery-{version}.js",
                        "~/Scripts/bootstrap.js",
                        "~/scripts/bootbox.js",
                        "~/Scripts/respond.js",
                        "~/scripts/datatables/jquery.datatables.js",
                        "~/scripts/datatables/datatables.bootstrap.js"
                      ));


in main Index.html

<script>
       $(document).ready(function () {

           $("#customers").DataTable();



           $("#customers").on("click", ".js-delete", function () {
               var button = $(this);

               bootbox.confirm("Are you Sure you want to delete this customer?",function(result){

                   if(result){
                       $.ajax({

                           url: "/api/customers/" + button.attr("data-customer-id"),
                           method: "DELETE",
                           success: function () {
                               button.parents("tr").remove();



                           }



                       });

                   }
               });


           });


       });


   </script>
Posted
Updated 22-Aug-17 7:09am
Comments
Nathan Minier 1-Feb-17 7:45am    
You're not actually getting any data to populate a table. All you have here is a delete method.
j snooze 1-Feb-17 17:08pm    
Agree with Nathan. Also you need an html object with the ID of "customers" (per your reference in the javascript). I don't see that in the index.html file either.

You Question is incomplete. You have asked for populating the records from WebApi to jQuery DataTable, But writing cde for deleting a row fro the DataTable.
The following code may work for pupulating the data (assuming Col1 and Col2 are two cloumns for the DataTable)


$("#customers").DataTable({
      serverSide: true,
      processing: true,
      columns : [
          { data : 'Col1' },
          { data : 'Col2' }
      ],    
      ajax: {
          url: /api/customers/" + button.attr("data-customer-id"),
          dataSrc : ''
      }
});
 
Share this answer
 
DataTables requires a well-formed table. It must contain <thead> and <tbody> tags, otherwise it throws this error. Also, make sure that you have the same amount of <td> in your project. 
 
Share this answer
 

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