Link to home
Start Free TrialLog in
Avatar of ITsolutionWizard
ITsolutionWizardFlag for United States of America

asked on

datatable json c# mvc


Hi Experts, do you know any code sample I can use for datatables?

I have a API Json response and try to use datatables showing on the webpage using Ajax javascript.

But I can't make it working. Can you help? Thanks


plug in: https://datatables.net/

my api: http://apitest.cc.com/api/CreditAccount/Data?FormatType=json

my api response:

 [{
   "CompanyID": 32125,
   "CompanyName": "Technology Inc 32125",
   "CreditLimit": 500.00,
   "Balance": 0.00,
   "LatestPayment": "2017-02-13T17:30:39.92",
   "EarliestOpenInvoice": null,
   "Invoices": 1,
   "OpenOrders": 0,
   "OverDueInvoices": 0
}, 
{
   "CompanyID": 108394,
   "CompanyName": "Company Inc/V Group 108394",
   "CreditLimit": 5000.00,
   "Balance": 0.00,
   "LatestPayment": "2021-06-01T14:18:32.03",
   "EarliestOpenInvoice": null,
   "Invoices": 2,
   "OpenOrders": 0,
   "OverDueInvoices": 0
}]

Open in new window


ASKER CERTIFIED SOLUTION
Avatar of lenamtl
lenamtl
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

or try this method

<table id="example" class="display" style="width:100%">
        <thead>
            <tr>
                <th>Name</th>
                <th>Position</th>
                <th>Office</th>
                <th>Extn.</th>
                <th>Start date</th>
                <th>Salary</th>
            </tr>
        </thead>
        <tfoot>
            <tr>
                <th>Name</th>
                <th>Position</th>
                <th>Office</th>
                <th>Extn.</th>
                <th>Start date</th>
                <th>Salary</th>
            </tr>
        </tfoot>
    </table>

Open in new window

$(document).ready(function () {
    $('#example').DataTable({
        ajax: 'data/objects_subarrays.txt',
        columns: [
            { data: 'name[, ]' },
            { data: 'hr.0' },
            { data: 'office' },
            { data: 'extn' },
            { data: 'hr.2' },
            { data: 'hr.1' },
        ],
    });
});

Open in new window

{
  "data": [
    {
      "name": [
        "Nixon",
        "Tiger"
      ],
      "hr": [
        "System Architect",
        "$320,800",
        "2011/04/25"
      ],
      "office": "Edinburgh",
      "extn": "5421"
    },
    {
      "name": [
        "Mooney",
        "Timothy"
      ],
      "hr": [
        "Office Manager",
        "$136,200",
        "2008/12/11"
      ],
      "office": "London",
      "extn": "7580"
    },
    {
      "name": [
        "Bradshaw",
        "Jackson"
      ],
      "hr": [
        "Director",
        "$645,750",
        "2008/09/26"
      ],
      "office": "New York",
      "extn": "1042"
    },
    {
      "name": [
        "Snider",
        "Donna"
      ],
      "hr": [
        "Customer Support",
        "$112,000",
        "2011/01/25"
      ],
      "office": "New York",
      "extn": "4226"
    }
  ]
}

Open in new window


Avatar of ITsolutionWizard

ASKER

can you use my Json response as example?

https://datatables.net/manual/server-side

My codes are reference from above.

and I have no idea why it does not work. It responses nothing


<script src="https://code.jquery.com/jquery-3.5.1.js"></script>
<script src="https://cdn.datatables.net/1.12.1/js/jquery.dataTables.min.js"></script><link href="https://cdn.datatables.net/1.12.1/css/jquery.dataTables.min.css" rel="stylesheet" type="text/css" />
    <script>
       // $(document).ready(function () {
       //     $('#example').DataTable();
       // }); 
        $(document).ready(function () {
            $('#example').DataTable({
                processing: true,
                serverSide: true,                
                columns: [
                    { data: 'CompanyID' },
                    { data: 'CompanyName' },
                    { data: 'CreditLimit' },
                    { data: 'Balance' },
                    { data: 'LatestOpenInvoice' },
                    { data: 'Invoices' },
                ],
                ajax:
                {
                    url: 'http://apitest.cc.com/api/CreditAccount/Data?FormatType=json',
                    type: 'POST'
                }
            });
        });
       
    </script>

Open in new window


Hi,

The problems are

you don't need to use their server side method, so set this to false

processing: false,                 
serverSide: false,  

Open in new window

You need to set the html code for the table

Your data  / url is not working 


I have set an example https://jsfiddle.net/lenamtl/w8k0cqoL/2/

As @lenamtl said:

  1. Your endpoint does not return data.
  2. Those two settings should be false. 


Here's a forked of @lenamtl's fiddle, using your data: https://jsfiddle.net/azarc3/p5crsv3n/6/

As such, @lenamtl should get credit for the solution.

1. My endpoint is working. I just can't put the link in public

2. I already updated to false for 2 settings. still not working


@lenamtl  I replace the ajax link to http://localhost:57071/test.txt, this returns all json format 

and it is not working


Is your columns configuration is correct?

  • Your code references a column LatestOpenInvoice.
  • In the example data you provided the corresponding column is EarliestOpenInvoice.


 [{
   "CompanyID": 32125,
   "CompanyName": "Technology Inc 32125",
   "CreditLimit": 500.00,
   "Balance": 0.00,
   "LatestPayment": "2017-02-13T17:30:39.92",
   "EarliestOpenInvoice": null, <==== THIS ONE
   "Invoices": 1,
   "OpenOrders": 0,
   "OverDueInvoices": 0
}, 
{
   "CompanyID": 108394,
   "CompanyName": "Company Inc/V Group 108394",
   "CreditLimit": 5000.00,
   "Balance": 0.00,
   "LatestPayment": "2021-06-01T14:18:32.03",
   "EarliestOpenInvoice": null, <==== THIS ONE
   "Invoices": 2,
   "OpenOrders": 0,
   "OverDueInvoices": 0
}]

Open in new window


Hi,


We don't see all your code so it's hard to figure out why it is not working.

Do you have any error into the browser console?

Try first with static data like in the example I provided.
or you can check this example https://datatables.net/examples/data_sources/js_array.html

Does static data work with your code or not?


If this is not working make sure the number of column is corresponding, 
If they are not the same you will see an error into the browser console.

Make sure you add the HTM code for the table and use the correct table ID.

Datatables will send error if you don't follow the requirement.


Make sure your json file format is ok 

Validate your json https://jsonlint.com/


Check format accepted by Datatables 

https://datatables.net/examples/ajax/


You can also check this method to allow the use of JSON data from any server.

https://datatables.net/examples/server_side/jsonp.html


If the data you want to display will be static the easiest way would be dataset like in my example.

If the data will change often then a txt or json file will be a good idea or use DB to store the values.

Do you still need help with this?