Apply jQuery DataTable Plugin in HTML Table using AngularJS and WebService in ASP.Net

Last Reply 3 months ago By dharmendr

Posted 3 months ago

How to apply jquery datatable plugin in Angularjs with web service to bind table.

You are viewing reply posted by: dharmendr 3 months ago.
Posted 3 months ago

Hi rani,

Check this example. Now please take its reference and correct your code.

Database

For this example I have used of Northwind database that you can download using the link given below.

Download Northwind Database

HTML

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
    <link rel="stylesheet" href="https://cdn.datatables.net/1.10.12/css/dataTables.bootstrap.min.css" />
    <script type="text/javascript" src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
    <script type="text/javascript" src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
    <script type="text/javascript" src="https://cdn.datatables.net/1.10.12/js/dataTables.bootstrap.min.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.8/angular.min.js"></script>
    <script type="text/javascript">
        var app = angular.module('MyApp', []);
        app.controller('MyController', function ($scope, $http) {
            $http.post("WebService.asmx/GetCustomers", { 'Content-Type': 'application/json' })
            .then(function (response) {
                $scope.Customers = response.data.d;
                angular.element(document).ready(function () {
                    dTable = $('#tblCustomers')
                    dTable.DataTable();
                });
            }, function error(response) { });
        });
    </script>
</head>
<body>
    <div ng-app="MyApp" ng-controller="MyController">
        <table id="tblCustomers" class="table table-bordered bordered table-striped table-condensed datatable">
            <thead>
                <tr class="info">
                    <th>Customer Id</th>
                    <th>Name</th>
                    <th>City</th>
                    <th>Country</th>
                </tr>
            </thead>
            <tbody>
                <tr ng-repeat="customer in Customers">
                    <td>{{customer.CustomerId}}</td>
                    <td>{{customer.Name}}</td>
                    <td>{{customer.City}}</td>
                    <td>{{customer.Country}}</td>
                </tr>
            </tbody>
        </table>
    </div>
</body>
</html>

Webservice

C#

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Web.Services;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService
{
    [WebMethod()]
    public List<Customer> GetCustomers()
    {
        List<Customer> customers = new List<Customer>();
        using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
        {
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.Connection = con;
                con.Open();
                cmd.CommandText = "SELECT CustomerId,ContactName,City,Country FROM Customers ORDER BY ContactName ASC";
                SqlDataReader rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {
                    Customer customer = new Customer()
                    {
                        CustomerId = rdr["CustomerId"].ToString(),
                        Name = rdr["ContactName"].ToString(),
                        City = rdr["City"].ToString(),
                        Country = rdr["Country"].ToString()
                    };
                    customers.Add(customer);
                }
                con.Close();
            }
        }

        return customers;
    }

    public class Customer
    {
        public string CustomerId { get; set; }
        public string Name { get; set; }
        public string City { get; set; }
        public string Country { get; set; }
    }
}

VB.Net

Imports System.Web
Imports System.Collections.Generic
Imports System.Configuration
Imports System.Data.SqlClient
Imports System.Web.Services

' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
<System.Web.Script.Services.ScriptService()> _
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class WebService
    Inherits System.Web.Services.WebService
    <WebMethod()> _
    Public Function GetCustomers() As List(Of Customer)
        Dim customers As List(Of Customer) = New List(Of Customer)()
        Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
            Using cmd As SqlCommand = New SqlCommand()
                cmd.Connection = con
                con.Open()
                cmd.CommandText = "SELECT CustomerId,ContactName,City,Country FROM Customers ORDER BY ContactName ASC"
                Dim rdr As SqlDataReader = cmd.ExecuteReader()
                While rdr.Read()
                    Dim customer As Customer = New Customer With {
                        .CustomerId = rdr("CustomerId").ToString(),
                        .Name = rdr("ContactName").ToString(),
                        .City = rdr("City").ToString(),
                        .Country = rdr("Country").ToString()
                    }
                    customers.Add(customer)
                End While
                con.Close()
            End Using
        End Using

        Return customers
    End Function

    Public Class Customer
        Public Property CustomerId As String
        Public Property Name As String
        Public Property City As String
        Public Property Country As String
    End Class
End Class

Screenshot