File size: 2,763 Bytes
d893556
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e41fb9b
d893556
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5049f57
d893556
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>fetch api & Datatable</title>
    
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.2.0/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdn.datatables.net/1.13.1/css/dataTables.bootstrap5.min.css">

    <style>
        section{
            position: relative;
            width: 100%;
            padding: 0px 8% 20px;
        }
        header h1{
            color: #8e44ad;
            font-size: 2em;
            margin:30px 0px
        }

    </style>
</head>
<body>
    
    <section>
        <header>
            <h1>Data table</h1>
        </header>

        <!-- table area -->
        <table id="example" class="table table-striped" style="width:100%">
            <thead>
                <tr>
                    <th>Zone </th>
                    <th>Sr No</th>
                    <th>Name</th>
                    <th>Fathers Name</th>
                    <th>Mobile</th>
                    <th>Name_E</th>
                    <th>Father's Name E</th>
                </tr>
            </thead>
            <tbody>

            </tbody>
            
        </table>
    </section>


    <!-- js library -->
    
    <script src="https://code.jquery.com/jquery-3.5.1.js"></script>
    <script src="https://cdn.datatables.net/1.13.1/js/jquery.dataTables.min.js"></script>
    <script src="https://cdn.datatables.net/1.13.1/js/dataTables.bootstrap5.min.js"></script>


    <script>
        


        fetch('http://basantagarwal.com/csvjson_short.json')
            .then(res=>res.json())
            .then(data=>{
                console.log('data',data);

                displayProducts(data);
            })

        async function displayProducts(products) {
            let html = '';
            await products.forEach((product, index, array) => {
                html += '<tr>';
                html += `<td>
                            ${product.Zone}
                         </td>
                        <td>${product.SrNo}</td>
                        <td>${product.NAME}</td>
                        <td>${product.FATHERS_NAME}</td>
                        <td>${product.Mobile}</td>
                        <td>${product.NAME_E}</td>
                        <td>${product.FATHERSN_E}</td>`;
                html += '</tr>';
            })
            document.querySelector('tbody').innerHTML = await html;
            // 
            $(document).ready(function () {
                $('#example').DataTable();
            });
        }
    </script>
</body>
</html>