Dynamic columns based on JSON being fed

1,004 views
Skip to first unread message

Eddie Shipman

unread,
Oct 22, 2013, 4:56:13 PM10/22/13
to slic...@googlegroups.com
We have JSON formatted like this:
[
   {
      "FILENAME":"/DownloadFile2.aspx?File=Update_Letter_20130831E.PDF",
      "DOC_NAME":"Update_Letter",
      "PRINT_DATE":"20130907",
      "ISSUE":"",
      "E_CODE":"20130831E",
      "CE_ST":"",
      "DESIG":"CR",
      "P_CODE":"LHE",
      "C_NAME":"LHE",
      "RECEIPT_DATE":"",
      "REC_RID":"",
      "RUN_DATE":""
   },
   {
      "FILENAME":"/DownloadFile2.aspx?File=Certificate6174451.PDF",
      "DOC_NAME":"Certificate",
      "PRINT_DATE":"20130906",
      "ISSUE":"",
      "E_CODE":"20130831E",
      "CE_ST":"NC",
      "DESIG":"",
      "P_CODE":"",
      "C_NAME":"LHE",
      "RECEIPT_DATE":"",
      "REC_RID":"",
      "RUN_DATE":""
   }
]

Can I configure SlickGrid to read the "columns" dynamically from the JSON? The JSON being fed may have different "columns" based on the data retrieved.

Steve Fazekas

unread,
Oct 22, 2013, 9:03:15 PM10/22/13
to slic...@googlegroups.com
Yes you can in a way...
You would need to loop thru the keys in the first row of your data and then dynamically create the id, field, and if applicable name attributes for each column.  If you want to use DataView, you need to create Ids dynamically and add them to each data row. 

You would then need to modify your slickgrid JS file for the "m" variable when it looks for formatters and editors to eval() it if it is a m.formatter - that is if you wish to set editors and formatters for certain types of columns.  

Steve Fazekas
--
You received this message because you are subscribed to the Google Groups "SlickGrid" group.
To unsubscribe from this group and stop receiving emails from it, send an email to slickgrid+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Eddie Shipman

unread,
Oct 23, 2013, 10:00:12 AM10/23/13
to slic...@googlegroups.com
After looking at how the data is coming over in our ASP.Net MVC3 application, it will be very easy to do this because it actually comes over with the column names in a Header array and the data in a Rows array.
I was transforming the data because I thought it was necessary to do that but I think I should be able to get by without transforming it now.

Thanks.

bill...@gmail.com

unread,
Oct 23, 2013, 10:01:15 AM10/23/13
to slic...@googlegroups.com

Hi Eddie!

 

I do what you’re doing all the time, to display things like arbitrary Database Query results;  I never know the names or number of columns to be displayed until the results are returned to my JavaScript.

 

However…the JSON response format that you’re using is not very efficient for this kind of thing. 

 

Are you stuck with (or married to) that JSON response format? 

 

If not, and you have control over the format of the response that the server hands you, I can show you a MUCH better way to handle this.  It’s far more efficient for the server to generate, far more efficient to transmit over the network, and far more efficient to stuff into a SlickGrid.  I routinely display result sets with a hundred thousand rows or more, and they work perfectly and very quickly.

 

Bill Rebey

--

bill...@gmail.com

unread,
Oct 23, 2013, 11:42:49 AM10/23/13
to slic...@googlegroups.com

Ha!  That’s exactly what I do, and what I was going to tell you to do!  ;-)  If you want a quick jump-start, here’s a couple snippets that demonstrate how I do it (these aren’t complete examples;  these are snippets from a larger body of code;  I think everything you need to get up and running is here, though):

 

The data arrives in a JSON object something like this:

{

                "ColumnHeadings": ["MsgID", "LastName", "FirstName"],

                "RowData": [      ["1", "Smith", "Bob"],

                                                ["2", "Jones", "Sally"] ]

}

 

All that’s needed to get the data on-screen is:

 

                var gridQMsgs;

                var gridQMsgsColumns = [];                                          

                var gridQMsgsOptions = {...whatever...};

                var gridQMsgsDataView = new Slick.Data.DataView(); ;

                var gridQMsgsData = [];

 

// This tracks index in the data array that's associated with each column

                // It’ll be used to populate the “field:xxx” field for each column definition

                var iCnt = 0;       

                //push all the column definitions.

                jQuery.each(jsonResp.ColumnHeadings, function () {

                                var colSpec = { id: this, name: this, field: iCnt, sortable: true};

                                gridQMsgsColumns.push(colSpec)

                                iCnt += 1;             // Next column index

                                return (true); // returning false halts iteration

                });

 

                //Create the SlickGrid in the specified Div

                gridQMsgs = new Slick.Grid("#DivToHoldGrid", gridQMsgsDataView, gridQMsgsColumns, gridQMsgsOptions);

 

                // Create reference to data for convenience

                gridQMsgsData = jsonResp["RowData"];

 

                // Get the data on-screen/populate the DataView

                gridQMsgsDataView.setItems(gridQMsgsData, 0);

Steve Fazekas

unread,
Oct 23, 2013, 12:10:34 PM10/23/13
to slic...@googlegroups.com
Nice work!  Nothing I would really do different except if you needed to format or edit anything you may run into JS issues when setting those up.  If you do, refer to my Slickgrid JS eval() update if you need to add quotes around your editor / formatter declarations. 

Steve Fazekas

Eddie Shipman

unread,
Oct 23, 2013, 12:49:47 PM10/23/13
to slic...@googlegroups.com
Bill, that's very similar to how our data is coming from our WCF service. It comes over like this:

{"Header":["WITH ALL COLUMN NAMES HERE"],
"Rows":[["WITH ALL COLUMN DATA HERE"]]
}

We currently pass the Header and Rows like this using a ViewBag:

    // gets the data form the service
  // Archive class is the model that the WCF sends data
  Archive docs = mps.GetArchiveArray(LogonTicket, ParticipantID, 1, 1000000, "D"); 
  string[][] Rows = docs.Rows;
  string[] Header = docs.Header;

            ViewBag.Rows = Rows;
            ViewBag.Header = Header;

The only thing is, we don't have unique id's for each row. I may have to add them. 
Now, I can convert to JSON using simple code like this, BTW, converting using Json serializer doesn't add the column headers to each data item.:

    string tbl = "[";

    for (int i = 0; i <= Rows.Count()-1; i++)
    {
        tbl = String.Concat(tbl, "{");
        for (int x = 0; x <= docs.Header.Count()-1; x++)
        {
            tbl = String.Concat(tbl, "\"", docs.Header[x], "\":\"", Rows[i][x], "\",");
        }
        tbl = String.Concat(tbl.TrimEnd(','), "},");
    }
    tbl = String.Concat(tbl.TrimEnd(','), "]");

Thanks for the tip, it'll make things a lot easier. 
Reply all
Reply to author
Forward
0 new messages