5
Hello, I need to create a Datatable in my MVC project that will not have predefined columns. The amount of columns should be defined from the amount of data existing in a BD field.
I have a field in my Model called Low Tier Id, it is a FK of another table (Low Tier table). I need to create columns for each Low Tier Id I have registered in the bank. Also, the columns cannot have the Low Tier Id as the title, but the Low Tier Name field, which I must pull through the FK.
The data that will appear in the fields will be from the Quantity field (this field is from the Base Hour table, the main one of the View). Each column should show the respective Quantity to the Low Tier Id column.
Until then, I was trying to create the columns this way, but still without passing the data that the fields should show and the titles of the correct columns:
JAVASCRIPT
var colsBaseLT = [];
function CreateColumnsBH() {
$.ajax({
url: urlLTList, //referencia o método LT List da Controller
dataType: 'json',
type: 'POST',
aynsc: false,
success: function (result) {
//a result trás um array de Low Tier Ids
//A função abaixo percorre o Array e tenta criar uma coluna para cada elemento dele
var ltLength = result.length;
for (var i = 0; i < ltLength; i++) {
colsBaseLT.push({ data: null, orderable: false, className: 'col-sm-2 text-center', title: "titulo", defaultContent: '' });
}
}
});
function CreateTableBH() {
$.ajax({
url: urlCreateTableBH, //referencia o método GetBaseHour da Controller
type: 'POST',
success: function (result) {
dataTableBH = DataTablesAjaxScrollHeight('#tableBaseHour', colsBaseLT, result.Grid, null, "350px");
}
});
$(document).ready(function () {
CreateColumnsBH();
DataTablesAjaxPagination("#tableBaseHour", colsBaseLT, null, null, 12, 'BaseHour');
CreateTableBH();
}
CONTROLLER (C#)
public JsonResult GetBaseHour()
{
//pega os dados da tabela Base Hour, a principal da View
return Json(new { Grid = new BaseHourBusiness().GetList<BaseHour>(Util.AuxiliaryMethods.BMPerRequestInstance)});
}
public JsonResult LTList()
{
var lowTierList = new LowTierBusiness().GetList<LowTier>(Util.AuxiliaryMethods.BMPerRequestInstance).Select(
s => new
{
s.Name
}).ToList();
//no Join acima eu tento pegar os LowTierNames presentes na tabela Low Tier
return Json(lowTierList);
}
EDIT: To better illustrate the Datatable structure I want to create, I leave the following image:
The names in the columns are the Low Tier Names, fields that come from the Low Tier table. The BD table of this View is called Base Hour and it has a FK of the Low Tier table which is the Low Tier Id field.
I need to pull the Low Tier Names by the Low Tier Ids so that the columns can acquire these names, and of course build the columns according to the amount of existing Low Tier ID.
The numbers, which are the table fields, come from the Quantity field. They should be displayed according to the corresponding Low Tier ID column.
I couldn’t understand the structure you’re trying to build
– Leandro Angelo
I edited the question to better illustrate!
– Guilherme Lima
Do you want to build Datatable from the front end? That’s it?
– Diego Rafael Souza
Yes, Datatable is built in Javascript (the View(HTML) only references this construction by tag). The Controller (C#) is only used to take the data that will be used and displayed.
– Guilherme Lima
Try the following: via json refer and bring only the fields you want to mount on the thead table, mount the table using javascript and after completion call your functions to insert content.
– Wilson Rosa Gomes