Introduction
For the past few days I have been working on ADOMD and MDX. I encountered a situation where I need to convert my Cell Set to a HTML table and render it to the client-side Grid. So I thought of sharing that with you all.
This article has been selected as article of the day Monday, November 10, 2014 in
http://www.asp.net/community/articles (Convert CellSet to HTML Table, and from HTML to Json, Array)
Background
If you are new to ADOMD, you can refer to the following links:
- Microsoft Analysis Services 2005: Displaying a grid using ADOMD.NET and MDX
- Manipulate and Query OLAP Data Using ADOMD and Multidimensional Expressions
Why
As I have already said, in my current project we are using MDX cubes, so in the server-side we will get only a CellSet. So I have tried very much to convert the CellSet to the JSON for this JQX grid alone (all other Grids in the project use a HTML table as the data source). But I couldn't find any good way for that. So I thought of getting the HTML table from the CellSet as in the other grid at the server side. And I knew how to formulate the Array and JSON from an HTML table. Here I am sharing that information.
Please provide your valuable suggestions for improvement.
Using the Code
We modify the code as per our needs from the preceding specified articles and bind to an HtmlTextWriter. We have created a function called renderHTML() that will accept CellSet as an argument. Here, I will show the code.
- try
- {
- System.Text.StringBuilder result = new System.Text.StringBuilder();
-
- int axes_count = cst.Axes.Count;
- if (axes_count == 0)
- throw new Exception("No data returned for the selection");
-
-
- if (axes_count != 2)
- throw new Exception("The sample code support only queries with two axes");
-
-
- if (!(cst.Axes[0].Positions.Count > 0) && !(cst.Axes[1].Positions.Count > 0))
- throw new Exception("No data returned for the selection");
-
- int cur_row, cur_col, col_count, row_count, col_dim_count, row_dim_count;
- cur_row = cur_col = col_count = row_count = col_dim_count = row_dim_count = 0;
-
-
- col_dim_count = cst.Axes[0].Positions[0].Members.Count;
-
-
- if (cst.Axes[1].Positions[0].Members.Count > 0)
- row_dim_count = cst.Axes[1].Positions[0].Members.Count;
-
-
- row_count = cst.Axes[1].Positions.Count +
- col_dim_count;
- col_count = cst.Axes[0].Positions.Count +
- row_dim_count;
-
-
-
-
-
-
- Table tblgrid = new Table();
- tblgrid.Attributes.Add("Id", "myhtmltab");
- tblgrid.Attributes.Add("class", "display");
-
- Label lbl;
- string previousText = "";
- int colSpan = 1;
- for (cur_row = 0; cur_row < row_count; cur_row++)
- {
-
- TableRow tr = new TableRow();
-
- for (cur_col = 0; cur_col < col_count; cur_col++)
- {
-
- TableCell td = new TableCell();
- TableHeaderCell th = new TableHeaderCell();
- lbl = new Label();
-
-
- if (cur_row < col_dim_count)
- {
-
- if (cur_col < row_dim_count)
- {
-
-
- lbl.Text = " ";
- td.CssClass = "titleAllLockedCell";
-
- }
- else
- {
-
-
-
-
-
- lbl.Text = cst.Axes[0].Positions[cur_col - row_dim_count].Members[cur_row].Caption;
- th.CssClass = "titleTopLockedCell";
-
-
-
- if (lbl.Text == previousText)
- {
- colSpan++;
- }
- else
- {
- colSpan = 1;
- }
- }
- }
- else
- {
-
-
-
- if (cur_col < row_dim_count)
- {
-
-
- lbl.Text = cst.Axes[1].Positions[cur_row -
- col_dim_count].Members[cur_col].Caption.Replace(",", " ");
- td.CssClass = "titleLeftLockedCell";
-
-
- }
- else
- {
-
- lbl.Text = cst[cur_col - row_dim_count, cur_row - col_dim_count].FormattedValue;
-
-
- td.CssClass = "valueCell";
-
- }
-
-
- td.Wrap = true;
- }
- if (((lbl.Text != previousText) || (lbl.Text == " "))
- && (cur_row < col_dim_count))
- {
- tr.TableSection = TableRowSection.TableHeader;
- th.Text = "HEADER";
-
- th.Controls.Add(lbl);
- tr.Cells.Add(th);
- tblgrid.Rows.Add(tr);
- }
- else if ((lbl.Text != previousText) || (lbl.Text == " ") ||
- (lbl.Text == null) || (lbl.Text == ""))
- {
- td.Controls.Add(lbl);
- tr.Cells.Add(td);
- tblgrid.Rows.Add(tr);
- }
- else
- {
- try
- {
- tr.Cells[tr.Cells.Count - 1].ColumnSpan = colSpan;
- }
- catch
- {
- }
- }
- if (cur_row < col_dim_count)
- previousText = lbl.Text;
- }
-
-
-
- }
-
- using (StringWriter writer = new StringWriter())
- {
- HtmlTextWriter htw = new HtmlTextWriter(writer);
-
- tblgrid.RenderControl(htw);
- return htw.InnerWriter.ToString();
- }
- }
- catch (Exception ex)
- {
- throw ex;
- }
Finally, the function will return the output as an HTML table with the id "myhtmltab" where all the th, tr and td tags are rendered.
Now if you want, you can convert the HTML table to an Array, JSON in the client side.
Now what we need to do is just add the dynamic HTML to the DOM. You can do that as follows:
$('#your element id').html(data);
Please read here for more information: Get the HTML contents of the first element in the set of matched elements.
Convert HTML to Array Dynamically in jQuery
Let's say you have an Ajax jQuery function that will return the output as I have shown in the output image.
If you are new to jQuery Ajax function, please read here:
Then in the success of the Ajax function, you can write the code like this to formulate an array.
Next is to get the columns and rows from the dynamic HTML table that you just formulated using CellSet:
- var rows = $("#myhtmltab tbody tr");
-
- var columns = $("#myhtmltab thead th");
Now what we need is an Array where we can populate the data. :)
- var data = [];
- for (var i = 0; i < rows.length; i++) {
- var row = rows[i];
- var datarow = {};
- for (var j = 0; j < columns.length; j++) {
-
- var columnName = $.trim($(columns[j]).text());
-
- var cell = $(row).find('td:eq(' + j + ')');
- datarow[columnName] = $.trim(cell.text());
- }
- data[data.length] = datarow;
- }
Now this is the time to formulate a JSON from the table. :)
Convert Dynamic HTML to JSON Dynamically in jQuery
As we discussed above, here also we are looping through the column and rows. The intent behind this is to formulate a dynamic JSON to assign data to my JQX Grid (You can check this out: Working With JQX Grid With Filtering And Sorting).
- var varDataFields = '[';
- var varDataColumns = '[';
- var typ = 'string';
- var align = 'center';
- var width = '200';
-
- var myColumns = $("#myhtmltab thead th");
- for (var j = 0; j < myColumns.length; j++) {
- var column = myColumns[j];
- var col = $(column).text().trim();
-
-
-
-
- varDataFields = varDataFields +
- ' { \"name\" : \"' + col + '\" , \"type\" : \"' + typ + '\"},';
- varDataColumns = varDataColumns +
- ' { \"text\" : \"' + col + '\" , \"dataField\" : \"' +
- col + '\" , \"align\" : \"' + align + '\" , \"width\" : \"' + width + '\"},';
-
-
- if (j == myColumns.length - 1) {
- varDataFields = varDataFields.slice(0, -1);
- varDataColumns = varDataColumns.slice(0, -1)
- }
- }
- varDataFields = varDataFields + ']';
- varDataColumns = varDataColumns + ']';
- varDataFields = varDataFields.trim();
- varDataColumns = varDataColumns.trim();
-
- var DataFields = $.parseJSON(varDataFields);
- var DataColumns = $.parseJSON(varDataColumns);
So in DataFields, DataColumns, I will get the JSON in the way that I want. This I can directly bind to the JQX Grid. :)
Points of Interest
History
- First version: 27-Oct-2014