4

我从 asp.net Web 服务获取数据,我想知道是否有办法将这些数据(直接从 Web 服务以 json 格式)传递到 DataTables 对象中。我想做类似的事情:

$(document).ready(function() {
    $('#example').dataTable( {
        "bProcessing": true,
        "bServerSide": true,
        "sAjaxSource": "http://localhost/WebService/GetData",
    } );
} );
4

6 回答 6

6

是的,你可以这么做。这是你的意思吗?...将数据传递给服务器?

$(document).ready(function() {
    $('#example').dataTable( {
        "bProcessing": true,
        "bServerSide": true,
        "sAjaxSource": "../examples_support/server_processing.php",
        "fnServerData": function ( sSource, aoData, fnCallback ) {
            /* Add some extra data to the sender */
            aoData.push( { "name": "more_data", "value": "my_value" } ); // this line will give additional data to the server...
            $.getJSON( sSource, aoData, function (json) { 
                /* Do whatever additional processing you want on the callback, then tell DataTables */
                fnCallback(json)
            } );
        }
    } );
} );
于 2010-01-25T17:54:10.483 回答
4

在这里你可以找到一篇关于使用 DataTables with ASP.NET MVC Integrating jQuery DataTables with ASP.NET MVC的文章。

希望这可以帮助你。很少有东西可以帮助您(用于访问 DataTables 参数的模型类和用于在 JSON 中直接序列化的类)。它使代码更简洁。

问候

于 2011-02-27T09:50:17.383 回答
1

我现在正在做这件事。我正在使用 ASP.Net MVC 2 Web 应用程序,虔诚地使用数据表,并且需要在每个表上显示超过 1000 条记录。服务器端处理是我要走的路。这就是我们所拥有的。

这是我们在视图中的声明(/Admin/Unassigned)。

<script type="text/javascript" charset="utf-8">
    $(document).ready(function() {
    $('#adminUnassignedTable').dataTable({
            "bProcessing": true,
            "bServerSide": true,
            "sAjaxSource": "/Admin/UnassignedTable"
        });
    });
</script>

这是我们的控制器函数(/Admin/UnassignedTable)。

public void UnassignedTable()
{
    statusID = (int)PTA.Helpers.Constants.Queue;
    locationID = (int)PTA.Helpers.Constants.Reviewer;
    _AdminList = PTA.Models.IPBRepository.GetIPBsByStatus(Convert.ToInt32(statusID), Convert.ToInt32(locationID)); //_AdminList is an IEnumerable<IPB>, IPB being our class
    IEnumerable<IPB> _NewList;
    int nDisplayStart = Convert.ToInt32(HttpContext.Request.QueryString.Get("iDisplayStart");
    int nDisplayLength = Convert.ToInt32(HttpContext.Request.QueryString.Get("iDisplayLength");
    _NewList = _AdminList.Skip<IPB>(nDisplayStart).Take<IPB>(nDisplayLength).ToList<IPB>();
    string strOutput = "{";
    strOutput += "\"sEcho\":" + HttpContext.Request.QueryString.Get("sEcho") + ", ";
    strOutput += "\"iTotalRecords\":" + _AdminList.Count().ToString() + ", ";
    strOutput += "\"iTotalDisplayRecords\":" + _AdminList.Count().ToString() + ", ";
    strOutput += "\"aaData\":[";
    foreach (IPB ipb in _NewList)
    {
        strOutput += "[ ";
        strOutput += "\"" + ipb.IPBName + "\",";
        strOutput += "\"" + ipb.PubDate + "\",";
        strOutput += "\"" + ipb.Change + "\",";
        strOutput += "\"" + ipb.ChangeDate + "\",";
        strOutput += "\"" + ipb.TotalParts + "\",";
        strOutput += "\"" + ipb.TotalPartsReports + "\",";
        strOutput += "\"" + ipb.ALC + "\",";
        strOutput += "\"" + ipb.DateAdded + "\",";
        strOutput += "\"" + "" + "\","; //Need to add drop down control, that's why it's blank.
        strOutput += "\"" + "" + "\","; //Need to add drop down control, that's why it's blank.
        strOutput += "\"" + "" + "\","; //Need to add drop down control, that's why it's blank.
        strOutput += "\"" + "" + "\""; //Need to add drop down control, that's why it's blank.
        strOutput += "]";
        if (ipb != _NewList.Last())
        {
            strOutput += ", ";
        }
    }
    strOutput += "]}";
    Response.Write(strOutput);
}

笔记!!!!!!!添加控件时,您必须将控件值的引号作为 \\" 因为反斜杠需要在 JSON 数据中。

希望在控制器中您可以访问您的网络服务。我对网络编程不太熟悉,所以我不知道你需要做什么。我只是认为这会有所帮助。

于 2010-08-13T13:35:14.253 回答
0

如果您的 Web 服务满足 DataTables API 约定,这是可能的。看看http://datatables.net/usage/server-side


更新

我已经按照您想要的方式在我的 ASP.NET MVC 应用程序中使用了 DataTables AjaxSource - 在初始化例程中指定它。但是,我的服务器端是专门设计的,以便 DataTables 可以与之对话。

于 2010-01-25T18:00:18.113 回答
0

我最终自己组成了这张桌子。它足以满足我现在相当少的目的。

于 2011-02-28T18:29:56.720 回答
0

是的。

创建一个以正确格式存储 Datatables 数据的对象。我使用了一个结构:

Public Structure DatatablesObject
    Public sEcho As Integer
    Public iTotalRecords As Integer
    Public iTotalDisplayRecords As Integer
    Public aaData As List(Of List(Of String))
End Structure

我们实际上为此创建了自己的序列化器,但概念是相同的。遍历您的数据源并填充对象,然后对其进行序列化并响应:

Dim ser As New JavascriptSerializer
Dim obj As New DatatablesObject With {.sEcho = {Passed In}, .iTotalRecords = {Passed In}, .iTotalDisplayRecords = {Passed In}}
            Dim container As New List(Of List(Of String))
            If value IsNot Nothing Then
                For Each dr As DataRow In value.Rows
                    Dim list As New List(Of String)
                        For Each dc As DataColumn In dr.Table.Columns
                            list.Add(If(dr(dc.ColumnName) Is DBNull.Value, String.Empty, "" & dr(dc.ColumnName) & ""))
                        Next
                    container.Add(list)
                Next
            End If
            obj.aaData = container
            Response.Cache.SetCacheability(HttpCacheability.NoCache)
            Response.ContentType = "application/json"
            Response.Write(ser.Serialize(obj))

那应该序列化为您需要的确切格式。如果您需要按特定顺序添加数据,则需要修改循环。请记住,{Passed In} 是传递给函数或从请求中直接分配给这些变量的值。

于 2012-10-25T21:09:59.293 回答