Can you provide instructions on utilizing the async .update() function for DataTables within MDBootstrap?

I am just starting to explore MDBootstrap and I'm currently focused on grasping how to utilize the DataTables. While I've browsed through the examples on their website regarding Async table updates, I've found it a bit challenging to adapt it to my specific scenario.

My main goal is to understand the async table update technique in detail, as I want my table to be able to dynamically update four columns based on filters obtained from Dropdowns.

Essentially, I aim for the table to fetch its data by making a call to a PHP endpoint that responds with JSON data. However, I'm struggling to comprehend how to implement the asyncTable.update() method as demonstrated in their example.

To simplify, let's assume the JSON returned by the PHP endpoint looks something 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" }]

Given the code snippet from the MDBootstrap site (provided below), how can I adjust the code to connect to my own endpoint? I'm having trouble with the JavaScript syntax within the .update() method in the example:

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 OWN PHP ENDPOINT 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 }
    );
  });
});

I would greatly appreciate guidance on incorporating this method with my own endpoint rather than the example provided. Thank you.

Best regards

Answer №1

Make sure to update the link in the fetch function with your endpoint's URL

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

When using the update() method, remember that it requires two parameters: data and options. Adjust the data parameter to match your specific data once you have changed the URL. In your scenario, it should be like this:

   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 }
    );
  });

If you prefer a cleaner example, you can check out this link:

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the process for implementing a version folder for my @types/definition?

Is there a way to access the typings for react-router in my project? My package.json file currently has this dependency: { "@types/react-router": "^4.0.3" } However, it seems to be using the standard index.d.ts file from DefinitelyTyped library which i ...

Exploring the process of extracting an array from JSON data retrieved from PHP

I am currently working with an index.html file that makes a call to main.php, which successfully returns a JSON object. The structure of the returned data from main.php looks like this: { "products": [ { "id": "1", "txt ...

How can you set a value to a radio button with Angular 4?

How can I set the JSON value for the radio buttons (Unlimited, Custom) below? I tried using [(ngModel)]="user.accessSchedule.fullAccess", but it's not working. <ol-radio-group [selected]="unlimitedAccessSchedule" id="accessSchedule" [(ngModel)]=" ...

Disappearing Cloned Form Fields in jQuery

Hey there! I'm trying to duplicate a section of a form using the code below. But for some reason, the copied fields are only visible for a split-second before they disappear. Can anyone spot any errors that might be causing this strange behavior? jQu ...

What is the best way to retrieve and utilize this JSON information with D3?

Understanding how to load JSON in D3 is crucial for working with data visualization. The process involves using the following code snippet without encountering any errors: d3.json("sample_data/unique_items.json", function(json) { // do something }); Af ...

Issue with Dropdown Menu functionality while using multiple dropdown options

I am currently experimenting with dropdown menus from W3Schools and have encountered an issue when trying to implement two dropdown options. The code works fine for a single dropdown, but when I try to add a second one using GetElementsByClassName instead ...

Convert a hexadecimal JSON object into its corresponding raw internal binary string in Python

How can I convert a value of a "json-loaded" object into a raw binary string in Python? For example, how can I convert "0A" to "1010"? Here is what I currently do: {"hex":"0A01145af1ab"} I read this line from a file and then load it using the JSON libra ...

What is the best way to display multiple VR scenes on a single webpage?

Currently, I am attempting to display several 360 photos utilizing the a-frame library through implementing a-scene. However, I am encountering an issue where only one scene is being rendered on my page. Are there any alternative approaches to address this ...

The process of decoding JSON data results in reaching the end

Having trouble decoding JSON into a struct. I've confirmed that the JSON response is valid by checking it in the browser. However, I keep receiving an EOF error for some reason. I even used json-to-go to double-check my structs. The issue seems to be ...

Autonomous JQuery Modules

Is it possible to separate the functions in JQuery and use only the ones needed by splitting the file with PHP? By doing this, I aim to improve page speed, save bandwidth, and have more control over the functions used through the backend. Can the functio ...

Error Message: Unable to retrieve property "country" as the variable this.props is not defined

Currently, I am developing a react application However, when running this function using infinite scroll, an error is encountered An Unhandled Rejection (TypeError) occurs: Unable to access the "country" property, since this.props is undefined async pa ...

The behavior of having two submit buttons within the $document.ready(function) in Jquery

In my code, I have implemented the behavior of two buttons, button1 and button2, within a $(document).ready(function). Whenever either button is clicked, an alert() function should be triggered. However, it seems that only button2 is functioning properly w ...

How can I reduce the delay in Angular 5 to a minimum?

The following code represents a simple request function: getItem() { return this.http .post<ItemModel>(`${this.apiUrl}/${methodUrl}`, {}) .subscribe(response => { ... }); } Depending on the duration of the ...

ReactJS: What is the best way to rearrange elements within an array stored in an object's property?

I am attempting to swap the positions of two elements within an array, which is nested inside an object as a property. However, when I click on the second "^" button in my CodeSandbox example below, I encounter the error message TypeError: arr.container.ma ...

Convert all keys from underscore to camelCase for JSON objects using Circe

Source { "fName" : "foo", "lName" : "bar", "father" : { "fName" : "baz", "lName" : "bazz", } } Desired Output { "fName" : "foo", "lName" : "bar", "father" : { "fName" : "baz", "lName" : "bazz", } ...

The function passed to the modal is not functioning correctly when stored within the state

Is it possible to create a unique modal that can be utilized for multiple actions on the same page? To achieve this, I have implemented a state to store the title, description, and submit function for modal click and modal open state. In addition, I want t ...

Tips for aligning a Font-Awesome icon vertically centered with a line of text in Bootstrap 4

My goal is to vertically center a Font Awesome icon next to a small block of text. To better illustrate, here is a reference image of what I am aiming for: This is the visual I aspire to achieve The section of my website that I am currently fine-tuning ca ...

The submission of the form is prevented upon updating the inner HTML

I am in the process of creating a website that will incorporate search, add, update, and delete functionalities all on a single webpage without any modals. The main focus of this webpage is facility maintenance. Adding facilities works smoothly; however, i ...

I am experiencing a 404 error when attempting to import a local JS file in Angular

After creating a new project with "ng new xxx", all you need to do is add one line of code in index.html: <!doctype html> <html lang="en> <head> <meta charset="utf-8> <title>Bbb</title> <base href="/&g ...

Guide on organizing json object by various elements with distinct sorting patterns

Below is a JSON Object that needs to be sorted based on different conditions: var data1 = [ {id: "1", name: "b", lastname: "y", marks: "10"}, {id: "1", name: "a", lastname: "x", marks: "20"}, {id: "2", name: "a", lastname: "x", marks: "30" ...