【问题标题】:dojo - data from sql server for datagrid and chartsdojo - 来自 sql server 的数据,用于数据网格和图表
【发布时间】:2012-05-14 18:56:43
【问题描述】:

我刚刚开始熟悉 dojo 并创建小部件,并拥有一个我现在想用数据填充的 Web UI。我的问题只是获得一些关于如何做到这一点的参考或想法。我的数据库都是 sql server 2008,我通常使用 microsoft.net。我认为我可能必须创建一个调用 sql 查询并将结果转换为 json 并将其提供给小部件的服务,无论它是数据网格还是图表。只是不确定如何执行此操作以及是否确实可以执行此操作。任何想法表示赞赏。

编辑:

       store = new dojo.data.ItemFileWriteStore({
            url: "hof-batting.json"
        });

        ngrid = new dojox.grid.DataGrid({
            store: store,
            id: 'ngrid',
            structure: [
                { name: "Search Term", field: "searchterm", width: "10%" },
                { name: "Import Date", field: "importDate", width: "10%" }
            ]
        }, "grid");


        ngrid.startup();

我想将从我的网络服务返回的数据添加到这个数据网格中,并使用相同的原理将数据添加到图表中。

【问题讨论】:

    标签: sql-server-2008 dojo


    【解决方案1】:

    您准确地描述了您需要做什么。

    我们使用 C# 来查询我们的数据库以获取数据,然后将其转换为 json。我们现在使用多种技术进行 json 序列化。我会推荐使用 JSON.NET。这是 .NET MVC 团队将要使用的。我不会使用当前属于 .NET 的 DataContractSerialization。

    http://json.codeplex.com/

    我们有时将 JSON 放在页面上,然后 javascript 将其作为页面变量进行访问。其他时候,我们在 .NET 中调用服务。我们使用 WCF,我们还使用 .ashx 文件为 Web 客户端提供 json 数据。

    json 的结构将是您的 dojo 小部件和 Web 服务器之间的契约。我会使用图表小部件或商店需要的东西来开始定义合同的过程。

    编辑

    WCF 接口

    [OperationContract]
    [WebInvoke(Method="POST", UriTemplate = "/data/{service}/", 
        BodyStyle = WebMessageBodyStyle.WrappedRequest)]
    String RetrieveData(string service, Stream streamdata);
    

    实现返回一个字符串,即 json。这以 json 格式发送到浏览器,但它由 .NET 通过 xml 节点包装。我有一个实用功能可以清理它。

    MyUtil._xmlPrefix = 
        '<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">';
    MyUtil._xmlPostfix = '</string>';
    
    MyUtil.CleanJsonResponse = function(data) {
        // summary: 
        //    a method that cleans a .NET response and converts it 
        //    to a javascript object with the JSON. 
        //    The .NET framework, doesn't easily allow for custom serialization,
        //    so the results are shipped as a string and we need to remove the 
        //    crap that Microsoft adds to the response.
        var d = data;
        if (d.startsWith(MyUtil._xmlPrefix)) {
            d = d.substring(MyUtil._xmlPrefix.length);
        }
        if (d.endsWith(MyUtil._xmlPostfix)) {
            d = d.substring(0, d.length - MyUtil._xmlPostfix.length);
        }
    
        return dojo.fromJson(d);
    };
    
    // utility methods I have added to string
    String.prototype.startsWith = function(str) { 
      return this.slice(0, str.length) == str;
    };
    String.prototype.endsWith = function(str) { 
      return this.slice(-str.length) == str;
    };
    

    【讨论】:

    • 谢谢。您是否知道任何显示尝试这样做的示例或链接?我刚刚让 WCF 服务工作,现在尝试将结果转换为 json。似乎具有挑战性的部分是将 json 输入控件。我有在 xml 中硬编码的 json 数据的示例;所以需要弄清楚如何做到这一点。感谢您的回复。
    • 我应该从 .net 方法调用服务,还是可以从 xml 中作为 javascript 函数执行此操作,以及如何将其添加到数据网格中
    猜你喜欢
    • 2016-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-06
    • 2013-11-23
    • 2016-01-14
    相关资源
    最近更新 更多