Json Issue

Tags: #<Tag:0x00007efc651dc438>

Hi
We are fetching data from database and inserted into json format.
We need new column for that ( hot.alter(‘insert_col’,1,1); ) this function used.
but I have getten one error like this
Uncaught Error: Cannot create new column. When data source in an object, you can only have as much columns as defined in first data row, data schema or in the ‘columns’ setting.If you want to be able to add new columns, you have to use array datasource.

I need solution for that

Hi Kunal,

You can use updateSettings() on your columns to push a new empty object as a column https://jsfiddle.net/handsoncode/s83bkaq9/

But as defining columns you use ‘data’ for the binding I recommend adding the key as well.

EXAMPLE

Initial dataset

[{
        id: 1,
        name: 'John'
      },
      {
        id: 2,
        name: 'Ana'
      }
]

columns

[{
      data: 'id',
      type: 'numeric',
      width: 50
    },
    {
      data: 'name',
      type: 'text',
      width: 100
    }
]

dataset changes to

 [{
        id: 1,
        name: 'John',
        number: 435
      },
      {
        id: 2,
        name: 'Ana',
        number: 159
      }
 ]

columns have to be changed to

[{
      data: 'id',
      type: 'numeric',
      width: 50
    },
    {
      data: 'name',
      type: 'text',
      width: 100
    }, {
      data: 'number'
    }
  ]

so the required action is to call

mycolumns.push({
      data: 'number'
    });
    hot.updateSettings({
      columns: mycolumns
 })

Demo https://jsfiddle.net/handsoncode/cfh6e3so/