如何从SQL Server绑定jsGrid

时间:2017-05-01 04:42:06

标签: c# jsgrid

我使用jsGrid网格jQuery插件)以表格格式显示数据,下面是我的代码

<script>
    $(function() {

        $("#jsGrid").jsGrid({
            height: "70%",
            width: "100%",
            filtering: true,
            editing: true,
            inserting: true,
            sorting: true,
            paging: true,
            autoload: true,
            pageSize: 15,
            pageButtonCount: 5,
            deleteConfirm: "Do you really want to delete the client?",
            controller: db,
            fields: [
                { name: "Name", type: "text", width: 150 },
                { name: "Age", type: "number", width: 50 },
                { name: "Address", type: "text", width: 200 },
                { name: "Country", type: "select", items: db.countries, valueField: "Id", textField: "Name" },
                { name: "Married", type: "checkbox", title: "Is Married", sorting: false },
                { type: "control" }
            ]
        });

    });
</script>

从服务或文件中获取数据db.js如下

db.countries = [
    { Name: "", Id: 0 },
    { Name: "United States", Id: 1 },
    { Name: "Canada", Id: 2 },
    { Name: "United Kingdom", Id: 3 },
    { Name: "France", Id: 4 },
    { Name: "Brazil", Id: 5 },
    { Name: "China", Id: 6 },
    { Name: "Russia", Id: 7 }
];

db.clients = [
    {
        "Name": "Otto Clay",
        "Age": 61,
        "Country": 6,
        "Address": "Ap #897-1459 Quam Avenue",
        "Married": false
    },
    {
        "Name": "Connor Johnston",
        "Age": 73,
        "Country": 7,
        "Address": "Ap #370-4647 Dis Av.",
        "Married": false
    },
    {
        "Name": "Lacey Hess",
        "Age": 29,
        "Country": 7,
        "Address": "Ap #365-8835 Integer St.",
        "Married": false
    }];

我想从MS SQL Server等数据源传入数据,我们该怎么做?

1 个答案:

答案 0 :(得分:1)

您的网格是否与db.js中的上述静态数据一起使用?如果是这样,那么它不是关于jsGrid而是关于SQL和Web服务的更多信息。基本上,您需要创建一个Web服务来查询SQL数据库并返回jsGrid的数据数组。

对Web服务的调用可以在loadData控制器中。 documentation显示了一个简单的示例。下面是一个使用promises的类似示例,其中/api/data是对Web服务的Ajax调用,必须以静态文件返回的形式返回数据:

 ...
 controller: {
   loadData: function(filter) { 
               return $.getJSON("/api/data/"
                ).done(function(results) {
                    console.log(results);
               }).fail(function(err) {
                     alert(err));
                  }); 
     },
 },
 ...

您可能希望从现在开始简单并避免分页,以测试获取数据是否有效。分页需要更复杂的查询,还需要返回结果中的记录总数。来自documentation

  

dataResult 取决于pageLoading。当pageLoading为false(默认情况下)时,数据结果是一个简单的javascript对象数组。如果pageLoading为真,则数据结果应具有以下结构:

{
   data          // array of items
   itemsCount    // total items amount in storage
}