Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to create DataTable filled by JSON when number of columns is unknown
    primarykey
    data
    text
    <p>I have a table in my web-application, which is created like this:</p> <pre><code>&lt;table id="mygrid"&gt; &lt;thead&gt; &lt;th&gt;Col1&lt;/th&gt; &lt;th&gt;Col2&lt;/th&gt; &lt;th&gt;Col3&lt;/th&gt; &lt;/thead&gt; &lt;/table&gt; &lt;script type="text/javascript"&gt; $(document).ready(function() { window.oTable = $("#mygrid").dataTable({ "bServerSide": true, "bSort": false, "sAjaxSource": "@Url.Action("MyFunction", "Events")", "fnServerParams": function(aoData) { aoData.push({ name: "arg1", value: "@Model.arg1" }); }, "aoColumns": [ { "mDataProp": "Column1" }, { "mDataProp": "Column2" }, { "mDataProp": "Column3" } ], "bJQueryUI": true, "sPaginationType": "full_numbers", "bProcessing": false }); </code></pre> <p>I fill it with function that returns JSON result, like this:</p> <pre><code>ActionResult MyFunction(string arg1, ...) { List&lt;string&gt; someList = ...; object array = eventsList.Select(str =&gt; new { Column1 = str + "1", Column2 = str + "2", Column3 = str + "3" }); var result = new { sEcho = ..., iTotalRecords = ..., iTotalDisplayRecords = ..., aaData = array }; return Json(result, JsonRequestBehavior.AllowGet); } </code></pre> <p>Now I want to generate table dynamically, so I don't know the number of columns in design time. For example:</p> <pre><code>&lt;table id="mygrid"&gt;&lt;thead&gt; @{ foreach (string col in colNames) &lt;th&gt;@col&lt;/th&gt; } &lt;/thead&gt;&lt;/table&gt; </code></pre> <p>Could you please advise how should I change my Javascript and C# code to fill the table in similar way? Is it possible? I can generate <code>"mDataProp"</code> lines in Javascript code like I generate columns, but how can I create JSON result in C# code?</p> <p><strong>Added:</strong></p> <p>I have solved the problem with controller. As I discovered, the List of Dictionaries is serialized to exactly the same JSON as the list of anonymous objects, so I wrote this:</p> <pre><code>for (int i = 0; i &lt; n; ++i) { list.Add(new Dictionary&lt;string, string&gt;()); foreach (int colName in colNames) events[i][colName] = cellValue; } var result = new { ..., aaData = list }; return Json(result, JsonRequestBehavior.AllowGet); </code></pre> <p>Now I have new question. I cannot generate "aoColumns" array using C# loop like I generated tags in HTML code. I tried like this:</p> <pre><code>"aoColumns": [ @{ for (int i = 0; i &lt; n; ++i) { string colName = "Column" + i.ToString(); { "mDataProp": "@colName" }, } } ], </code></pre> <p>but it does not work. How can I do it?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload