Topic: How to get async table to update?

VAdev free asked 3 years ago


Hello - I am new MDBootstrap and I am trying to learn how to use your Datatables (I have seen your examples).

I am interested in learning how to use the async table specifically since I would like my table to update four columns dynamically based on filters that I am collecting from Dropdowns.

I would like the table to get its data by calling a PHP endpoint that will return data back in JSON and I am not understanding how to use the asyncTable.update() method (based on the example that you have shown).

For the sake of simplicity, let's assume the PHP endpoint returns JSON that looks like this:

[ { "a": "a string", "b": "another string", "c": "another string", "d": "another string" }, { "a": "a string", "b": "another string", "c": "another string", "d": "another string" }]

Based on your example, how do I modify the code (below) to call my own endpoint? I do not understand the javascript syntax in the update() method of the example code (listed below):

const columns = [
  { label: 'A', field: 'a' },
  { label: 'B', field: 'b' },
  { label: 'C', field: 'c' },
  { label: 'D', field: 'd' },
];

const asyncTable = new mdb.Datatable(
  document.getElementById('datatable'),
  {
    columns,
  }
);

//var url = 'MY URL';
var url = 'https://jsonplaceholder.typicode.com/users';
fetch(url)
  .then((response) => response.json())
  .then((data) => {
    asyncTable.update(
      {
        rows: data.map((user) => ({
          ...user,
          address: `${user.address.city}, ${user.address.street}`,
          company: user.company.name,
        })),
      },
      { loading: false }
    );
  });
});

By the way, I am using Pro 3.4.0.

Thank you in advance!


VAdev free answered 3 years ago


Thank you, Michal - that did it!


Michał Duszak staff answered 3 years ago


Hello,

you have to change link in fetch for your endpoint's URL

  fetch('https://custom-api.com/rows')

The update() method takes two parameters: data and options. Upon changing the URL you have to modify the data parameter to correspond to your data. In your example it will look like:

   fetch('https://your-url.com/rows')
  .then((response) => response.json())
  .then((data) => {
    asyncTable.update(
      {
        rows: data.map((row) => ({
          a: row.a,
          b: row.b,
          c: row.c,
          d: row.d
        })),
      },
      { loading: false }
    );
  });

this example might look cleaner for you: https://mdbootstrap.com/snippets/standard/m-duszak/3000204#js-tab-view

Keep Coding,

Michal Duszak



Please insert min. 20 characters.

FREE CONSULTATION

Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.

Status

Resolved

Specification of the issue

  • ForumUser: Free
  • Premium support: No
  • Technology: MDB Standard
  • MDB Version: 3.2.0
  • Device: Desktop
  • Browser: Safari or Chrome
  • OS: Ubuntu
  • Provided sample code: No
  • Provided link: No