Adding header column in between existing header columns

While populating data-grid after getting an API response.

I have a problem statement where I need to add a new data-grid header in between the existing data-grid header while populating column data.
As in my case, I want to add a new header name as 'Order Date' in-between existing 'Order ID' & 'Order Status' while adding my column data.

Code :

datagrid.addColumn('Order ID', datagrid.DATA_TYPE_DOUBLE);
datagrid.addColumn('Order Status', datagrid.DATA_TYPE_STRING);

 

for(var i = 0; i < json_order_length; i++){
datagrid.addCell(jsonobj[i].id);
datagrid.addCell(jsonobj[i].status);
if(i === 3)
{
// it adds the column at last after Order Status
// but I want it between existing 'Order ID' & 'Order Status'  header.

datagrid.addColumn('Order Date', datagrid.DATA_TYPE_DATETIME);
datagrid.addCell(jsonobj[i].date_created);
}
}

Please also refer to the image uploaded for more details.

Comments

  • GrantSmith
    GrantSmith Indiana 🥷

    Hi @user062862 

     

    If you want the order date to be before status you need to call datagrid.addColumn('Order Date'...) before the Order Status. Because you're adding Status column before the date column is what you're getting your current result.

     

    Could you just add all 3 columns to start and then just add a null value (this is all an assumption as I don't know the structure of your JSON) when i = 3?

     

    Something like this (untested - back of napkin code here):

    datagrid.addColumn('Order ID', datagrid.DATA_TYPE_DOUBLE);
    datagrid.addColumn('Order Date', datagrid.DATA_TYPE_DATETIME);
    datagrid.addColumn('Order Status', datagrid.DATA_TYPE_STRING);
     
    for(var i = 0; i < json_order_length; i++){
    datagrid.addCell(jsonobj[i].id);
    if(i === 3)
    {
    datagrid.addCell(jsonobj[i].date_created);
    }
    else
    {
    datagrid.addCell()
    }
    datagrid.addCell(jsonobj[i].status);
    }

     

    My javascript is a bit rusty. You may need to do datagrid.addCell(null) instead of datagrid.addCell()

    **Was this post helpful? Click Agree or Like below**
    **Did this solve your problem? Accept it as a solution!**
  • I can't pre-decide the column headers before writing the column data as the condition I gave here for easier understanding i.e. *i===3* but in reality it is something like *order has 3 products then add three new columns in-between "Product-1", "Product-2", "Product-3" * which is dynamic means a 1st person can have 3 products in orders & 2nd person can have 20 products in orders.

    So while iterating through the order data we want to add headers existing between two headers.

  • jaeW_at_Onyx
    jaeW_at_Onyx Budapest / Portland, OR 🔴

    this may lead to problems down the road.

     

    if you design a dataflow that injects columns based on the number of orders (prod_1, prod_2, prod_3) then you can potentially get a dataset with 100 extra columns.

     

    from day to day, the number of columns might change based on the data in your dataset.  as a result any downstream ETL that expects column (ex. prod_21) will break.

     

    in this context your're probably off inserting ROWS instead of columnns.

    Jae Wilson
    Check out my 🎥 Domo Training YouTube Channel 👨‍💻

    **Say "Thanks" by clicking the ❤️ in the post that helped you.
    **Please mark the post that solves your problem by clicking on "Accept as Solution"
  • jaeW_at_Onyx
    jaeW_at_Onyx Budapest / Portland, OR 🔴

    also... don't split hairs which order columns are added to your dataset.  neither analyzer nor ETL tools really care about column order.

     

    if you want to reorganize them for display purposes, just use a DataSetView (talk to your CSM) to reoganize them.

    Jae Wilson
    Check out my 🎥 Domo Training YouTube Channel 👨‍💻

    **Say "Thanks" by clicking the ❤️ in the post that helped you.
    **Please mark the post that solves your problem by clicking on "Accept as Solution"
This discussion has been closed.