<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ADOMD Cell &#8211; Sibeesh Passion</title>
	<atom:link href="https://mail.sibeeshpassion.com/tag/adomd-cell/feed/" rel="self" type="application/rss+xml" />
	<link>https://mail.sibeeshpassion.com</link>
	<description>My passion towards life</description>
	<lastBuildDate>Wed, 02 Jun 2021 15:18:06 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>/wp-content/uploads/2017/04/Sibeesh_Passion_Logo_Small.png</url>
	<title>ADOMD Cell &#8211; Sibeesh Passion</title>
	<link>https://mail.sibeeshpassion.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Convert CellSet to HTML Table and From HTML to JSON and to Array</title>
		<link>https://mail.sibeeshpassion.com/convert-cellset-to-html-table-and-from-html-to-json-to-array/</link>
					<comments>https://mail.sibeeshpassion.com/convert-cellset-to-html-table-and-from-html-to-json-to-array/#disqus_thread</comments>
		
		<dc:creator><![CDATA[SibeeshVenu]]></dc:creator>
		<pubDate>Wed, 29 Oct 2014 19:19:46 +0000</pubDate>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Json]]></category>
		<category><![CDATA[Microsoft ADOMD]]></category>
		<category><![CDATA[Microsoft Technologies]]></category>
		<category><![CDATA[ADOMD]]></category>
		<category><![CDATA[ADOMD Cell]]></category>
		<category><![CDATA[ADOMD CellSet]]></category>
		<category><![CDATA[CellSet]]></category>
		<category><![CDATA[CellSet JSON]]></category>
		<category><![CDATA[Convert CellSet to JSON]]></category>
		<guid isPermaLink="false">https://sibeeshpassion.com/?p=591</guid>

					<description><![CDATA[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 [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><strong>Introduction</strong></p>
<p>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.</p>
<p>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)</p>
<p><strong>Background</strong></p>
<p>If you are new to ADOMD, you can refer to the following links:</p>
<p><a href="http://www.codeproject.com/Articles/28290/Microsoft-Analysis-Services-Displaying-a-grid" target="_blank" rel="noopener">Microsoft Analysis Services 2005: Displaying a grid using ADOMD.NET and MDX</a><br />
<a href="http://www.microsoft.com/msj/0899/mdx/mdx.aspx" target="_blank" rel="noopener">Manipulate and Query OLAP Data Using ADOMD and Multidimensional Expressions</a></p>
<p><strong>Why</strong></p>
<p>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.</p>
<p>Please provide your valuable suggestions for improvement.</p>
<p><strong>Using the Code</strong></p>
<p>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.<br />
[csharp]<br />
try<br />
            {<br />
                System.Text.StringBuilder result = new System.Text.StringBuilder();<br />
                //check if any axes were returned else throw error.<br />
                int axes_count = cst.Axes.Count;<br />
                if (axes_count == 0)<br />
                    throw new Exception(“No data returned for the selection”);<br />
                //if axes count is not 2<br />
                if (axes_count != 2)<br />
                    throw new Exception(“The sample code support only queries with two axes”);<br />
                //if no position on either row or column throw error<br />
                if (!(cst.Axes[0].Positions.Count &gt; 0) &amp;&amp; !(cst.Axes[1].Positions.Count &gt; 0))<br />
                    throw new Exception(“No data returned for the selection”);<br />
                int cur_row, cur_col, col_count, row_count, col_dim_count, row_dim_count;<br />
                cur_row = cur_col = col_count = row_count = col_dim_count = row_dim_count = 0;<br />
                //Number of dimensions on the column<br />
                col_dim_count = cst.Axes[0].Positions[0].Members.Count;<br />
                //Number of dimensions on the row<br />
                if (cst.Axes[1].Positions[0].Members.Count &gt; 0)<br />
                    row_dim_count = cst.Axes[1].Positions[0].Members.Count;<br />
                //Total rows and columns<br />
                row_count = cst.Axes[1].Positions.Count +<br />
                    col_dim_count;  //number of rows + rows for column headers<br />
                col_count = cst.Axes[0].Positions.Count +<br />
                    row_dim_count;  //number of columns + columns for row headers<br />
                //gridPanel.ClientIDMode = System.Web.UI.ClientIDMode.AutoID;<br />
                //////lets clear any controls under the grid panel<br />
                //gridPanel.Controls.Clear();<br />
                ////Add new server side table control to gridPanel<br />
                Table tblgrid = new Table();<br />
                tblgrid.Attributes.Add(“Id”, “myhtmltab”);<br />
                tblgrid.Attributes.Add(“class”, “display”);<br />
                //We will use label control to add text to the table cell<br />
                Label lbl;<br />
                string previousText = “”;<br />
                int colSpan = 1;<br />
                for (cur_row = 0; cur_row &lt; row_count; cur_row++)<br />
                {<br />
                    //add new row to table<br />
                    TableRow tr = new TableRow();<br />
                    for (cur_col = 0; cur_col &lt; col_count; cur_col++)<br />
                    {<br />
                        //create new cell and instance of label<br />
                        TableCell td = new TableCell();<br />
                        TableHeaderCell th = new TableHeaderCell();<br />
                        lbl = new Label();<br />
                        //check if we are writing to a ROW having column header<br />
                        if (cur_row &lt; col_dim_count)<br />
                        {<br />
                            //check if we are writing to a cell having row header<br />
                            if (cur_col &lt; row_dim_count)<br />
                            {<br />
                                //this should be empty cell — it’s on top left of the grid.<br />
                                //result.Append(” ,”);<br />
                                lbl.Text = ” “;<br />
                                td.CssClass = “titleAllLockedCell”; //this locks<br />
                                    //the cell so it doesn’t scroll upwards nor leftwards<br />
                            }<br />
                            else<br />
                            {<br />
                                //this is a column header cell — use member caption for header<br />
                                //result.Append(cst.Axes[0].Positions[cur_col –<br />
                                //    row_dim_count].Members[cur_row].Caption + “,”);<br />
                                //if (cur_row &lt; 1)<br />
                                //{<br />
                                    lbl.Text = cst.Axes[0].Positions[cur_col – row_dim_count].Members[cur_row].Caption;<br />
                                    th.CssClass = “titleTopLockedCell”; // this lockeders<br />
                                            //the cell so it doesn’t scroll upwards<br />
                                //}<br />
                                if (lbl.Text == previousText)<br />
                                {<br />
                                    colSpan++;<br />
                                }<br />
                                else<br />
                                {<br />
                                    colSpan = 1;<br />
                                }<br />
                            }<br />
                        }<br />
                        else<br />
                        {<br />
                            //We are here.. so we are writing a row having data (not column headers)<br />
                            //check if we are writing to a cell having row header<br />
                            if (cur_col &lt; row_dim_count)<br />
                            {<br />
                                //this is a row header cell — use member caption for header<br />
                                lbl.Text = cst.Axes[1].Positions[cur_row –<br />
                                col_dim_count].Members[cur_col].Caption.Replace(“,”, ” “);<br />
                                td.CssClass = “titleLeftLockedCell”; // this lockeders<br />
                                    //the cell so it doesn’t scroll leftwards<br />
                            }<br />
                            else<br />
                            {<br />
                                //this is data cell.. so we write the Formatted value of the cell.<br />
                                lbl.Text = cst[cur_col – row_dim_count, cur_row – col_dim_count].FormattedValue;<br />
                                //td.InnerText = cst[cur_col – row_dim_count,<br />
                                //cur_row – col_dim_count].FormattedValue;<br />
                                td.CssClass = “valueCell”; //this  right<br />
                                        //aligns the values in the column<br />
                            }<br />
                            //turn the wrapping off for row header and data cells.<br />
                            td.Wrap = true;<br />
                        }<br />
                        if (((lbl.Text != previousText) || (lbl.Text == ” “))<br />
                            &amp;&amp; (cur_row &lt; col_dim_count))<br />
                        {<br />
                            tr.TableSection = TableRowSection.TableHeader;<br />
                            th.Text = “HEADER”;<br />
                            th.Controls.Add(lbl);<br />
                            tr.Cells.Add(th);<br />
                            tblgrid.Rows.Add(tr);<br />
                        }<br />
                        else if ((lbl.Text != previousText) || (lbl.Text == ” “) ||<br />
                            (lbl.Text == null) || (lbl.Text == “”))<br />
                        {<br />
                            td.Controls.Add(lbl);<br />
                            tr.Cells.Add(td);<br />
                            tblgrid.Rows.Add(tr);<br />
                        }<br />
                        else<br />
                        {<br />
                            try<br />
                            {<br />
                                tr.Cells[tr.Cells.Count – 1].ColumnSpan = colSpan;<br />
                            }<br />
                            catch<br />
                            {<br />
                            }<br />
                        }<br />
                        if (cur_row &lt; col_dim_count)<br />
                            previousText = lbl.Text;<br />
                    }<br />
                    //result.AppendLine();<br />
                }<br />
                using (StringWriter writer = new StringWriter())<br />
                {<br />
                    HtmlTextWriter htw = new HtmlTextWriter(writer);<br />
                    tblgrid.RenderControl(htw);<br />
                    return htw.InnerWriter.ToString();<br />
                }<br />
            }<br />
            catch (Exception ex)<br />
            {<br />
                throw ex;<br />
            }<br />
[/csharp]</p>
<p>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.</p>
<p>Now if you want, you can convert the HTML table to an Array, JSON in the client side.</p>
<p>Now what we need to do is just add the dynamic HTML to the DOM. You can do that as follows:<br />
[js]<br />
$(‘#your element id’).html(data);<br />
[/js]</p>
<p>Please read here for more information: <a href="http://api.jquery.com/html/" target="_blank" rel="noopener">Get the HTML contents of the first element in the set of matched elements.</a></p>
<p><strong>Convert HTML to Array Dynamically in jQuery</strong></p>
<p>Let’s say you have an Ajax jQuery function that will return the output as I have shown in the output image.</p>
<p>If you are new to jQuery Ajax function, please read here:</p>
<p><a href="http://www.codeproject.com/Tips/688228/Bind-Dropdownlist-in-ASP-NET-using-jQuery-AJAX" target="_blank" rel="noopener">Bind Dropdownlist in ASP.NET using jQuery AJAX</a><br />
<a href="http://www.codeproject.com/Articles/224827/Jquery-Ajax-Calling-functions" target="_blank" rel="noopener">Jquery Ajax Calling Functions</a><br />
<a href="http://www.codeproject.com/Articles/631438/Code-Builder-for-jQuery-AJAX-Calling-Web-Services" target="_blank" rel="noopener">Code Builder for jQuery AJAX (Calling Web Services)</a></p>
<p>Then in the success of the Ajax function, you can write the code like this to formulate an array.</p>
<p>Next is to get the columns and rows from the dynamic HTML table that you just formulated using CellSet:<br />
[js]<br />
var rows = $(“#myhtmltab tbody tr”); //Select Rows , looping through every tr<br />
var columns = $(“#myhtmltab thead th”); //Select columns , looping through every th<br />
[/js]<br />
Now what we need is an Array where we can populate the data. 🙂<br />
[js]<br />
var data = [];<br />
for (var i = 0; i &lt; rows.length; i++) {<br />
    var row = rows[i];<br />
    var datarow = {};<br />
    for (var j = 0; j &lt; columns.length; j++) {<br />
        // get column’s title.<br />
        var columnName = $.trim($(columns[j]).text());<br />
        // select cell.<br />
        var cell = $(row).find(‘td:eq(‘ + j + ‘)’);<br />
        datarow[columnName] = $.trim(cell.text());<br />
    }<br />
    data[data.length] = datarow;<br />
}<br />
[/js]</p>
<p>Now this is the time to formulate a JSON from the table. 🙂</p>
<p><strong>Convert Dynamic HTML to JSON Dynamically in jQuery</strong></p>
<p>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: <a href="http://sibeeshpassion.com/Working-With-JQX-Grid-With-Filtering-And-Sorting/" target="_blank" rel="noopener">Working With JQX Grid With Filtering And Sorting</a>).<br />
[js]<br />
var varDataFields = ‘[‘;<br />
var varDataColumns = ‘[‘;<br />
var typ = ‘string’;<br />
var align = ‘center’;<br />
var width = ‘200’;<br />
var myColumns = $(“#myhtmltab thead th”);<br />
for (var j = 0; j &lt; myColumns.length; j++) {<br />
    var column = myColumns[j];<br />
    var col = $(column).text().trim();<br />
    //col = col.replace(‘&lt;span&gt;’, ”);<br />
    //col = col.replace(‘&lt;/span&gt;’, ”);<br />
    //var col = $(columns).find(‘th:get(‘ + j + ‘).find(‘ &lt; span &gt; ‘).text()’);<br />
    //if (!col == ”) {<br />
    varDataFields = varDataFields +<br />
    ‘ { \”name\” : \”‘ + col + ‘\” , \”type\” : \”‘ + typ + ‘\”},’;<br />
    varDataColumns = varDataColumns +<br />
    ‘ { \”text\” : \”‘ + col + ‘\” , \”dataField\” :  \”‘ +<br />
    col + ‘\” , \”align\” :  \”‘ + align + ‘\” , \”width\” : \”‘ + width + ‘\”},’;<br />
    //}<br />
    if (j == myColumns.length – 1) {<br />
        varDataFields = varDataFields.slice(0, -1);<br />
        varDataColumns = varDataColumns.slice(0, -1)<br />
    }<br />
}<br />
varDataFields = varDataFields + ‘]’;<br />
varDataColumns = varDataColumns + ‘]’;<br />
varDataFields = varDataFields.trim();<br />
varDataColumns = varDataColumns.trim();<br />
var DataFields = $.parseJSON(varDataFields);<br />
var DataColumns = $.parseJSON(varDataColumns);<br />
[/js]</p>
<p>So in DataFields, DataColumns, I will get the JSON in the way that I want. This I can directly bind to the JQX Grid. 🙂</p>
<p><strong>Points of Interest</strong></p>
<p>ADOMD, MDX</p>
<p><strong>History</strong></p>
<p>First version: 27-Oct-2014</p>
<p>Kindest Regards<br />
Sibeesh Venu</p>
]]></content:encoded>
					
					<wfw:commentRss>https://mail.sibeeshpassion.com/convert-cellset-to-html-table-and-from-html-to-json-to-array/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
