【问题标题】:loadData just won't workloadData 不起作用
【发布时间】:2018-07-04 03:51:43
【问题描述】:

以下是我的完整代码。没有错误并显示Directory is empty

<script>
    $(document).ready(function() {
        $("#table").jsGrid({
            width: "100%",
            height: "auto",
            paging: false,
            autoload: false,
            noDataContent: "Directory is empty",

            controller: {
                loadData: function(filter) {
                    var data = {
                        data: [{
                            nickname: "test",
                            email: "t@gmail.com"
                        }]
                    };
                    console.log(data);
                    return data;
                }
            },
            fields: [{
                name: "nickname",
                type: "text",
                width: 80,
                title: "Name"
            }, {
                name: "email",
                type: "text",
                width: 100,
                title: "Email Address",
                readOnly: false
            }]
        });
    });
</script>

控制台输出如下。格式有什么不对吗?

有没有办法启用更多诊断,例如打印出它实际接收的数据以便进行故障排除?

【问题讨论】:

    标签: jsgrid


    【解决方案1】:

    你需要设置autoload:true

    autoload (default false)

    一个布尔值,指定在渲染网格时是否调用controller.loadData

    您还需要在loadData() mehtod 中返回data.data,因为数据中有数组。

    代码片段

    controller: {
        loadData: function(filter) {
    
            var data = {
                data: [{
                    nickname: "test",
                    email: "t@gmail.com"
                }]
            };
            return data.data; //As per your data array is like this console.log(data.data) so you need to send like this data.data
        }
    },
    

    演示

    $(document).ready(function() {
    
        $("#table").jsGrid({
            width: "100%",
            height: "auto",
            paging: false,
    
            //for loadData method Need to set auto load true
            autoload: true,
    
            noDataContent: "Directory is empty",
    
            controller: {
                loadData: function(filter) {
                
                alert('Table load data method fire because we have set config autoload:true')
                    var data = {
                        data: [{
                            nickname: "test",
                            email: "t@gmail.com"
                        }]
                    };
                    return data.data;//As per your data array is like this console.log(data.data) so you need to send like this data.data
                }
            },
    
            fields: [{
                name: "nickname",
                type: "text",
                width: 80,
                title: "Name"
            }, {
                name: "email",
                type: "text",
                width: 100,
                title: "Email Address",
                readOnly: false
            }]
        });
        
        $("#table1").jsGrid({
            width: "100%",
            height: "auto",
            paging: false,
    
            //for loadData method will not work with auto load false
            autoload: false,
    
            noDataContent: "Directory is empty",
    
            controller: {
                loadData: function(filter) {
                   alert('Table 1 load data method not fire')
                    return []
                }
            },
    
            fields: [{
                name: "nickname",
                type: "text",
                width: 80,
                title: "Name"
            }, {
                name: "email",
                type: "text",
                width: 100,
                title: "Email Address",
                readOnly: false
            }]
        });
    });
    <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/jsgrid/1.5.3/jsgrid.min.css" />
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jsgrid/1.5.3/jsgrid-theme.min.css" />
    <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jsgrid/1.5.3/jsgrid.min.js"></script>
    
    
    <div id="table"></div>
    <br>
    <br>
    <br>
    <div id="table1"></div>

    【讨论】:

    • 谢谢!我的autoload 是真的,但是我在测试不同的排列时将它设置为假。 data.data 成功了。
    • 是的,因为 jsgrid 将接受 Array 的数据通知。所以需要传递一个数据数组。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-30
    • 2015-11-28
    • 2012-07-16
    相关资源
    最近更新 更多