JavaScript和DataTable

时间:2014-02-24 10:57:33

标签: c# javascript jquery

我想从JavaScript中检索dataTable中的对象值。

我的对象返回的是我的Web服务中的ResponseStatistic_1

[OperationContract]
    [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json)]
    ResponseStatistic_1 Statistic_1();

这是我的ResponseStatistic_1.cs

public class ResponseStatistic_1 : IBaseClientEntity
{
    public ResponseStatistic_1()
    {

    }

    public ResponseStatistic_1(Statistic_1 [] items) : this()
    {
        this.Items = items;
    }

    public Statistic_1[] Items
    {
        get;
        set;
    }
}

这是我的Statistic_1.cs

public class Statistic_1
{
    private string _geografisch_zone;
    private decimal[] _sum; 
    private int _yearStart;
    private int _yearEnd;

    public Statistic_1()
    {
     ...
    }

    public string Geografisch_zone
    {...
    }

    public decimal[] Sum
    {
     ...
    }

    public int YearStart
    {
     ...
    }

    public int YearEnd
    {
     ...
    }
}

这是我在JavasCript中的代码:

function getStatistic1() {

var response;
var allstat1 = [];

$.ajax({
    type: 'GET',
    url: 'http://localhost:52768/Service1/Statistic_1',
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    success: function (msg) {
     response = msg.d;
     for (var i = 0; i < response.length; i++) {

         allstat1[i] =**???**

     }

         fillData(allstat1);**???**

    },
    error: function (e) {
        alert("error loading statistic 1");
    }
})
}

function fillData(data) {

$('#table_campaigns').dataTable({
    **???**
});
}

我们应该这样做吗?

1 个答案:

答案 0 :(得分:0)

解决方案是:

allyearstat1[0] = [response.Items[0].PropertyName, response.Items[0].PropertyName];

Ans填写dataTables:

$('#table_statistic_1').dataTable({

    'aaData': data,
    'aoColumns': [
        { "sTitle": ""},
        { "sTitle": allyearstat3[0][0] },
        { "sTitle": allyearstat1[0][1] }
    ],

});