jsGrid 调用 Controller .Net Core - 单元格未加载数据

jsGrid calling Controller .Net Core - Cells not loading data

使用 jsGrid / .net core 2.2 / MVC 我在 ajax 调用中成功返回数据并记录到控制台,但我无法弄清楚为什么没有填充这些字段?

我的观点

<div id="jsGrid"></div>

@section scripts {
<script>
    $(function () {

        $("#jsGrid").jsGrid({
            height: "50%",
            width: "100%",

            filtering: true,
            inserting: true,
            editing: true,
            sorting: true,
            paging: true,
            autoload: true,

            pageSize: 10,
            pageButtonCount: 5,

            //deleteConfirm: "Do you really want to delete client?",

            controller: {
                loadData: function () {
                    var d = $.Deferred();

                    $.ajax({
                        type: 'GET',
                        url: "/operator/GridJson",
                        dataType: "json",
                        success: function (data) {
                            d.resolve(data);
                            console.log('success ', data);
                        },
                        error: function (e) {
                            alert("error: " + e.responseText);
                        }
                    });

                    return d.promise();
                }                    
            },

            fields: [
                { name: "OperatorId", type: "number", width: 50 },
                { name: "FirstName", type: "text", width: 150 },
                { name: "LastName", type: "text", width: 150 },
                { name: "LicenseNumber", type: "number", width: 50, filtering: false },
                { name: "RFIDNumber", type: "number", width: 50, filtering: false },
                { name: "DateCreated", type: "date", width: 50, filtering: false },
                { name: "LastUpdated", type: "date", width: 50, filtering: false },
                { name: "CompanyId", type: "number", width: 50 }
                //    { type: "control" }
            ]
        });
        console.log("test");

    });
</script>
}

我的控制器方法

[HttpGet]
public string GridJson()
{
    List<OperatorDetail> operators = service.GetOperators();

    string json = JsonConvert.SerializeObject(operators);

    return json;

}

控制台日志

(5) [{…}, {…}, {…}, {…}, {…}]
0: {OperatorId: 1, FirstName: "driver1", LastName: "driver1", LicenseNumber: 12345, RFIDNumber: 12345, …}
1: {OperatorId: 2, FirstName: "driver2", LastName: "driver2", LicenseNumber: 23456, RFIDNumber: 23456, …}
2: {OperatorId: 3, FirstName: "driver3", LastName: "driver3", LicenseNumber: 34567, RFIDNumber: 34567, …}
3: {OperatorId: 4, FirstName: "driver4", LastName: "driver4", LicenseNumber: 45678, RFIDNumber: 45678, …}
4: {OperatorId: 5, FirstName: "driver5", LastName: "driver5", LicenseNumber: 56789, RFIDNumber: 56789, …}
length: 5
__proto__: Array(0)

原因是您的 loadData() 异步函数 returns 在您得到响应之前。

要解决此问题,请将 loadData 函数更改为 return 承诺。

loadData: function(filter){
    return $.ajax({
        type:"GET",
        url: "/operator/GridJson",
        data:filter,
        dataType:"JSON",
    });
}

[编辑]

实际上,如果您使用 F12 开发人员工具检查源代码,您会发现字段已经呈现。事实证明,我们还需要 更改 height: "100%",因为它会使这些字段不可见。

这是工作示例的完整列表:

$("#jsGrid").jsGrid({
    width: "100%",
    height: "100%",
    height: "400px",
    inserting: true,
    editing: true,
    sorting: true,
    paging: true,
    autoload: true,

    pageSize: 10,
    pageButtonCount: 5,

    //deleteConfirm: "Do you really want to delete client?",

    controller: {
        loadData: function(filter){
            return $.ajax({
                type:"GET",
                url: "/book/index",
                data:filter,
                dataType:"JSON",
            });
        }                  
    },

    fields: [
        { name: "OperatorId", type: "number", width: 50 },
        { name: "FirstName", type: "text", width: 150 },
        { name: "LastName", type: "text", width: 150 },
        { name: "LicenseNumber", type: "number", width: 50, filtering: false },
        { name: "RFIDNumber", type: "number", width: 50, filtering: false },
        { name: "DateCreated", type: "date", width: 50, filtering: false },
        { name: "LastUpdated", type: "date", width: 50, filtering: false },
        { name: "CompanyId", type: "number", width: 50 }
        //    { type: "control" }
    ]
});

演示:

我最近遇到了同样的问题,我花了一段时间才找到其他人向我指出。 itminus 有很大一部分解决方案,但由于启用了分页,您需要 return 特定 JSON 对象中的数据。

{ 
  data: [{your list here}],
  itemsCount: {int}
}

它几乎不在文档中,因为它是内联的并且不是很明显。 (加粗我的。)

loadData is a function returning an array of data or jQuery promise that will be resolved with an array of data (when pageLoading is true instead of object the structure { data: [items], itemsCount: [total items count] } should be returned). Accepts filter parameter including current filter options and paging parameters when

http://js-grid.com/docs/#controller