Javascript 如何创建具有动态列结构的数据表?
我需要构造一个在MVC项目中没有预定义列的数据表。列的数量应在DB字段中现有数据的数量之后定义 我的模型中有一个名为Low Tier ID的字段,它是来自另一个表(Low Tier table)的FK。我需要为数据库中注册的每个低层ID创建列。除此之外,列不能将低层ID作为标题,它们需要将低层名称显示为标题,我需要通过FK获取字段1 将显示的数据将来自数量字段(基本小时表、视图主表中的字段)。每个cholumn应该能够向列的低层ID显示相应的数量 到目前为止,我一直试图在下面以这种方式创建列,但仍然没有放置字段应该显示的数据,甚至没有正确的标题 JAVASCRIPTJavascript 如何创建具有动态列结构的数据表?,javascript,c#,html,model-view-controller,datatables,Javascript,C#,Html,Model View Controller,Datatables,我需要构造一个在MVC项目中没有预定义列的数据表。列的数量应在DB字段中现有数据的数量之后定义 我的模型中有一个名为Low Tier ID的字段,它是来自另一个表(Low Tier table)的FK。我需要为数据库中注册的每个低层ID创建列。除此之外,列不能将低层ID作为标题,它们需要将低层名称显示为标题,我需要通过FK获取字段1 将显示的数据将来自数量字段(基本小时表、视图主表中的字段)。每个cholumn应该能够向列的低层ID显示相应的数量 到目前为止,我一直试图在下面以这种方式创建列,但
var colsBaseLT = [];
function CreateColumnsBH() {
$.ajax({
url: urlLTList, //refers to the LT List method from the Controller
dataType: 'json',
type: 'POST',
aynsc: false,
success: function (result) {
//the result comeback an array of Low Tier Ids
//The function below iterate an Array and try to create a columns for each element on it
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, //refers to the GetBaseHour method from the 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();
}
var colsBaseLT=[];
函数CreateColumnsBH(){
$.ajax({
url:urlLTList,//引用控制器中的LT List方法
数据类型:“json”,
键入:“POST”,
aynsc:错,
成功:功能(结果){
//结果返回一个低层ID数组
//下面的函数迭代数组,并尝试为数组中的每个元素创建列
var ltLength=result.length;
对于(变量i=0;i
控制器(C#)
public JsonResult GetBaseHour()
{
//从基本小时表(视图的主表)获取数据
返回Json(new{Grid=new BaseHourBusiness().GetList(Util.AuxiliaryMethods.BMPerRequestInstance)});
}
public JsonResult LTList()
{
var lowTierList=new LowTierBusiness().GetList(Util.AuxiliaryMethods.BMPerRequestInstance)。选择(
s=>新
{
s、 名字
}).ToList();
//在上面的联接中,我尝试获取低层表的低层名称
返回Json(lowTierList);
}
您的问题是什么?您将尝试在视图中显示一个表,其中包含基于某个整数值的任意n个列数?如果n>20或更糟的是,n>50,会发生什么情况。我不想尝试在网页中滚动以查看表中的20多个列。问题是如何使用未定义列数的数据表(此数量根据“低层ID”的数量定义)以及如何显示这些列的相应数据。DataTable必须将每个字段显示为一列,即使使用滚动,因为DB不会有很多该字段的寄存器。当您获得不同的列列表时,可能需要重新创建DataTable,我没有看到用于更改列的API,只是显示或隐藏。但是我如何为这些未定义列的字段(“数量”)分配正确的数据呢?
public JsonResult GetBaseHour()
{
//get the data from the Base Hour table, the main table of the 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();
//in the Join above I try to get the LowTierNames of the Low Tier table
return Json(lowTierList);
}