【问题标题】:kendoui grid with dynamic columns具有动态列的 kendoui 网格
【发布时间】:2016-10-18 13:42:46
【问题描述】:

我正在尝试使用动态列生成网格

为了实现这一点,我制作了一些生成的 asp.net 服务:列、模型和数据(以 json 格式)。 这些方法由 ajax 调用(不是异步方式,立即获取数据)

网格似乎没有显示列(从“Search.Grid.GenerateColumns”动态提供),但根据数据/模型显示列 这种行为的进一步证明是,如果我将 format = "{0:MM/dd/yyyy}" 添加到日期列(在列集合中),则网格中的日期未格式化

Search.Grid.Init = function ()
{
    var _rootUrl = "http://.................."

    //====================build the datasource==================
    var mainGridDataSource = new kendo.data.DataSource({
        transport: {
            read: {
                type: "POST",
                url: _rootUrl + 'Repository.aspx/GetArchiveData',   
                contentType: "application/json; charset=utf-8",
                dataType: "json"
            },
            parameterMap: function (data, operation) {
                if (operation === "read" ) {
                    return JSON.stringify({ archivio: Search.SelectedArchive.Value });
                }
                return data;
            }
        },
        schema: {
            data: "d.data",
            total: "d.total",
            model: Search.Grid.GenerateModel(Search.SelectedArchive.Value)  //sync call
        }
    });


    //====================configure the grid==================
    $("#mainGrid").kendoGrid({
        dataSource: mainGridDataSource,
        columns: Search.Grid.GenerateColumns(Search.SelectedArchive.Value),  //sync call
        autoGenerateColumns: false,
        filterable: {  
            mode: "row"
        }
    });

} 


//build the columns collection  server side
Search.Grid.GenerateColumns = function (archivio) {

    $.ajax({
        type: 'POST',
        url: _rootUrl + 'Repository.aspx/GetArchiveColumns',
        data: JSON.stringify({ archivio: archivio }),
        success: function(data) {
            return data.d;
        },
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: false
    });
}
//output (the grid columns)
{"d":[{"field":"Id","title":"Id","type":"number"},{"field":"NumeroDocumento","title":"NumeroDocumento","type":"string"},{"field":"DataDocumento","title":"DataDocumento","type":"date"},{"field":"NumeroDocumentoUsoInterno","title":"NumeroDocumentoUsoInterno","type":"string"},{"field":"NumeroOrdine","title":"NumeroOrdine","type":"string"}]}

//build the model collection server side
Search.Grid.GenerateModel = function (archivio) {

    $.ajax({
        type: 'POST',
        url: _rootUrl + 'Repository.aspx/GetArchiveModel',
        data: JSON.stringify({ archivio: archivio }),
        success: function (data) {
            return data.d;
        },
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: false
    });
}
//output (the datasource model)
{"d":{"id":"Id","fields":{"Id":{"type":"number"},"NumeroDocumento":{"type":"string"},"DataDocumento":{"type":"date"},"NumeroDocumentoUsoInterno":{"type":"string"},"NumeroOrdine":{"type":"string"}}}}

【问题讨论】:

  • 只是一种预感,但您可能希望调用网格元数据,然后在成功检索后创建您的网格。我认为您现在看到的只是时间问题。
  • 所以你说的是:首先加载元数据(列和模型),然后才加载数据!这是正确的吗?我在做什么不正确的顺序?是这样吗?帮助我理解,因为我不会说英语
  • 我会发布一个更详细的答案

标签: javascript kendo-ui kendo-grid dynamic-columns


【解决方案1】:

我认为您需要拨打电话以获取元数据,并且在成功接收之前不要尝试创建网格 - 大致如下:

// make the ajax call for metadata - don't create 
// the grid until this call is successful
$.ajax({
    url: "http...",
    // other configurations
    success: function (e) {
        // create the grid        
        createGrid(e);                        
    },
    error: function (e) {
        console.log(e);        
    }
});

createGrid(metaData){

    // create the grid using the metadata

}

【讨论】:

  • 我认为在 ajax 调用中使用“async: false”就足够了,无论如何我会先尝试移动加载,然后在之后创建网格
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-09-16
  • 2020-01-08
  • 2023-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-18
相关资源
最近更新 更多