如何使用Ajax JQuery在Handsontable中加载数据?

时间:2014-10-17 11:14:12

标签: jquery .net handsontable

我正在使用以下代码使用Jquery和AJAX从ASP.Net加载数据。但是当它加载时,Handsontable只加载一个带有数组数据的单元格。

我尝试过不同的运气方式。请帮我从服务器加载数据。

function showGetResult() {

            $.ajax({
                type: "POST",
                url: "Input.aspx/LoadTable",
                contentType: "application/json; charset=utf-8",
                dataType: "json",

                success: function (res) {
                    alert(res); 

                    $("#exampleGrid").handsontable({
                        data: res,
                        rowHeaders: false,


                        stretchH: 'all',
                        minSpareRows: 0,
                        contextMenu: true
                    });

                },
                error: function (xhr, status) {
                    alert("An error occurred: " + status);
                }
            });
        }


<WebMethod()> <ScriptMethod()> _
    Public Shared Function LoadTable() As String
        Dim dsCosts As New DataSet
        dsCosts = DataAccess.LoadTables
        Dim serializer As New JavaScriptSerializer()
        Dim dt As New DataTable()
        '.... Get datatable 
        Dim data As String = GetJson(dt)
        Return data
    End Function

Private Shared Function GetJson(dt As DataTable) As String
        Dim serializer As New JavaScriptSerializer()
        Dim rows As New List(Of Dictionary(Of String, Object))()
        Dim row As Dictionary(Of String, Object) = Nothing
        Dim rowCount As Integer = 0

        For Each dr As DataRow In dt.Rows
            row = New Dictionary(Of String, Object)()
            For Each col As DataColumn In dt.Columns
                row.Add(col.ColumnName.Trim(), dr(col))
            Next
            '   row.Add("RowNumber", "R||" & rowCount)
            rows.Add(row)
            Dim temp As String = serializer.Serialize(rows)
            rowCount += 1
        Next
        Return serializer.Serialize(rows)
    End Function

1 个答案:

答案 0 :(得分:0)

挣扎之后,我得到了答案。希望它能帮助别人

在将数据绑定到Handsontable

之前,您必须解析数据
$.ajax({
                type: "POST",
                url: "Test.aspx/LoadTable",
                contentType: "application/json; charset=utf-8",
                dataType: "json",

                success: function (res) {

                    part = res.d;
                    var data = JSON.parse(part);
                    $('#dataTable').handsontable('loadData', data);
                },
                    error: function (xhr, status) {
                        alert("An error occurred: " + status);
                    }

                    });

                });