Quantcast
Channel: DataTables 1.10 — DataTables forums
Viewing all articles
Browse latest Browse all 2364

How to create table dynamically with dynamic column names & rows too.

$
0
0

controller -

        public JsonResult GetTableSchema(string Tablename)
        {
            // List<TABLESCHEMADATAMODEL> objTableSchemaDm = new MasterBL().GetTableSchema(Tablename);
            //List<MasterConfiguration.TableSchema> objTableVm = new MasterConverter().ConvertTableSchema(objTableSchemaDm);
            DataTable objTableSchemaDm = new MasterBL().GetTableSchema(Tablename);

            string JSONString = string.Empty;
            JSONString = JsonConvert.SerializeObject(objTableSchemaDm);

            return Json(JSONString);
        }

Ajax Call -

    $("#ddltable").change(function () {
            debugger;
            var Tablenameselect = $("#ddltable").find("option:selected").text();
            var columns = [];
            //alert(Tablenameselect);
            $.ajax({
            type: "POST",
                url: "@Url.Action("GetTableSchema", "Master")",
                data: { Tablename: Tablenameselect },
                dataType: "json",

                success: function (data) {
                    debugger;

                    console.log(data);
                    columnNames = Object.keys(data[0]);
                    console.log(columnNames);
                    for (var i in columnNames) {
                        columns.push({
                            data: columnNames[i],
                            title: columnNames[i]
                        });
                    }

                    $("#divGridSales").css({ display: "block" });
                    $('#tblViewPartDetails').DataTable({
                        "processing": true, // for show progress bar
                        "serverSide": false, // for process server side
                        "filter": true, // this is for disable filter (search box)
                        "orderMulti": false, // for disable multiple column at once
                        "bDestroy": true,   //for reinitialize the datatable
                        "data": data,
                        "columns": columns,
                        "responsive": true
                    });
                    },
                error: function (data) {
                    alert("Error while fetching Data");
                }
            });

        });

e.g. from API code as below

        public DataTable GetTableSchema(string pTablename)
        {
            try
            {
                DataSet dsTable = _sql.ExecuteDataSet(null, "Select * from " + Tablename).ResultSet;

                dsTable.Tables[0].TableName = "TableSchema";
                return dsTable.Tables["TableSchema"];
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

**here table name will be any table it is not fixed, so as per table schema Datatable has to create dynamic table with column names & rows.
**

Note: Here by using above code I am getting blank Datatable


Viewing all articles
Browse latest Browse all 2364

Trending Articles