Tips for automatically setting tabulator columns for unknown JSON data

I am a novice developer and have come across a Json object whose contents I am not familiar with, which I need to incorporate into Tabulator.

In order to do so, I must provide details for each column. For example:

var JSONData =[{A:12,B:3,C:13},{A:5,B:23,C:3},{A:1,B:30,C:103}]

var tabulator1 = new Tabulator("#table", {
    data:JSONData,
    columns:[
       {title:"A", field:"A", sorter:"string",align:"right", editor:true},
       {title:"B", field:"B", sorter:"string",align:"right", editor:true},
       {title:"C", field:"C", sorter:"string",align:"right", editor:true},
     ],
});

While this approach works for known JSON Data, what if

var JSONData =[UNKNOWN LIST OF JSON DATA ]

I can determine the column headers using Object.keys(JSONData[0]));. I could then default to defining the columns as follows:

{title:"A", field:"A", sorter:"string",align:"right", editor:true},

However, how can I dynamically loop through the unknown JSONData and add it to columns:[]?

At the very least, I will need to include title and field data for Tabulator to function properly.

Another possible solution is to utilize flask-jinja2 to iterate through the data on the backend, but I prefer to minimize dependency on server resources as much as possible.

Answer №1

This solution has proven successful for my needs. It is important to note that this method will only work if all the elements within the array contain an equal number of keys.

const jsonData = [{A:12,B:3,C:13},{A:5,B:23,C:3},{A:1,B:30,C:103}]

const tabulator1 = new Tabulator("#table", {
  data: jsonData,
  columns: Object.keys(jsonData[0]).map(obj => {
    return {
      title: obj,
      field: obj,
      sorter: "string",
      align: "right",
      editor: true
    };
  })
});

Answer №2

A recent update (now at version 5.4) of Tabulator introduces a handy feature known as autoColumns, designed to effortlessly generate column names automatically. For more details on how this feature works, check out:

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

Execute Cheerio selector once the page has finished loading

Hey there! I'm trying to extract the URL value of an iframe on this website: I've checked the view page source but couldn't find the iframe. Looks like it's loaded after the page is fully loaded through JavaScript. Could it be that ...

Security Measures for Parsing Arrays using eval() Function

When I receive a string of an array containing objects via an http request, I use eval() to parse it. Since I am expecting an array object after parsing, how can I secure this eval() process beyond using if (Array.isArray(parsedObj)) ... Is there a bette ...

How to instantly return progress bar to zero in bootstrap without any animations

I am currently working on a task that involves multiple actions, and I have implemented a bootstrap progress bar to visually represent the progress of each action. However, after completion of an action, the progress bar is reset to zero using the followi ...

Verify if Angular 2 route parameters have a legitimate value

Within an angular2 component, I have the following code: ngOnInit() { this.route.params .switchMap((params: Params) => this.elementsService.getElement(+params['id'])) .subscribe(element => { this.elementToEd ...

Issue with Newtonsoft JSON deserialization

Upon making a REST API call, I encountered the following JSON file: { { id: "13", id_user: "2", login: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="46272c2b272a062034232322292b29202223306825292b" ...

Having trouble with the onClick function in React?

Here is my simple react code: Main.js: var ReactDom = require('react-dom'); var Main = React.createClass({ render: function(){ return( <div> <a onClick={alert("hello world")} >hello</a> </ ...

Avoiding caching of GET requests in Angular 2 for Internet Explorer 11

My rest endpoint successfully returns a list when calling GET, and I can also use POST to add new items or DELETE to remove them. This functionality is working perfectly in Firefox and Chrome, with the additional note that POST and DELETE also work in IE ...

Angular JS is up and running

My journey into the world of Angular JS has just begun. While I can put together an Angular module, I find myself with a myriad of questions regarding the inner workings of Angular. How exactly does $scope function? I know that a root scope is created ...

Running a Custom Tab Component in a React Application

I am currently facing an issue with my React app that has multiple tabs. When I click on a specific tab, I want only that tab to render, but currently all tabs are rendering when I click on one. I have used console.log to confirm that all tabs are indeed r ...

Seeking the value of a tab text using Selenium's web driver

Greetings! Currently, I am exploring the integration of selenium webdriver with mocha and node js in order to automate testing for a Single Page Application (SPA). My objective is simple - to locate a specific tab and perform a click action on it. The HTML ...

Issue with Chart JS: Firefox is reporting that the 'Chart' is not defined

As a newcomer to Chart JS, I am currently utilizing it in Angular JS version 1.5.3 with Chart JS version 2.1.4. Below is the code snippet I am working with: var myChart = new Chart(ctx, { type: 'line', data: { labels: dates, datasets: [{ ...

Provide users with the option to select a specific destination for saving their file

In the midst of my spring MVC project, I find myself in need of implementing a file path chooser for users. The goal is to allow users to select a specific location where they can save their files, such as C:\testlocation\sublocation... Despite r ...

Tips for redirecting a page in React by forcing a route

Attempting to implement the guidance from this Stack Overflow solution on how to "go back home" after closing a Modal... import React, { Suspense, useState } from 'react'; import { BrowserRouter, Route, Switch, useHistory } from "react-route ...

Updating API calls with form submission in React.js

Currently working on a weather application and attempting to update my API call upon submitting a form. This project marks my initial attempt at developing my own program, and I've encountered an obstacle. The plan is for the user to input a city, cli ...

What sets index.js apart from page.js in next.js?

https://nextjs.org/docs/pages/building-your-application/routing/pages-and-layouts While reading through the next.js documentation, I came across an interesting point. The documentation mentions that index.js serves as the root of the directory. This mean ...

Dividing a fixed string state according to an increment state and showcasing the state in a React component

I'm facing an issue with my code structure, which can be found here: CodeSandbox The problem lies within the useGenText() hook that generates words every nth second in a read-only format, meaning it cannot be modified. Additionally, there is the useL ...

verified firebase/firestore requests

I've been struggling with the process of sending authenticated firebase/firestore calls. I set up firestore in my web app using Next.js as shown below: import { initializeApp } from "firebase/app"; import { getFirestore } from 'firebase ...

The onclick function is malfunctioning when attempting to use the Windows Phone app in Visual Studio 2015

web development <div class="align_center"> <div class="btn EmployeeloginBtn" **onclick="new Employee().connect()**>CONNECT</div> </div> Employee.js: var Employee = function() { var self = this; self.connect = fu ...

What is the best way to extract data from a table and transmit it to the server using AngularJS?

I'm attempting to extract all the content from a table in HTML. Is it possible to retrieve all rows from one side and send them in a post request to the server? I've been struggling to figure out how to do this. Do I need to bind each row using n ...

Having trouble with Angular minification not properly updating templates?

As a newcomer to angular development, I am currently working on creating components for AEM 6.2 using angular and utilizing gulp to minify the js files for all the components. In my gulpfile, I have set up the following configuration: var uglify = require ...