Jquery DataTables插件 - sAjaxSource

时间:2010-01-25 17:43:43

标签: jquery web-services datatables

我从asp.net Web服务获取数据,我想知道是否有办法将数据(在json formar中直接从Web服务传递)传递到DataTables对象。我想做点什么:

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

6 个答案:

答案 0 :(得分: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)
            } );
        }
    } );
} );

答案 1 :(得分:4)

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

希望这可以帮到你。有一些东西可以帮助你(用于访问DataTables参数的模型类和用于JSON中的直接序列化的类)。它使代码更清晰。

此致

答案 2 :(得分:1)

我正在努力解决这个问题。我正在使用ASP.Net MVC 2 Web应用程序,宗教地使用dataTables,并且需要在每个表上显示超过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数据中。

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

答案 3 :(得分:0)

如果您的Web服务满足DataTables API约定,则可以使用。 看看http://datatables.net/usage/server-side


<强>更新

我在ASP.NET MVC应用程序中以您希望的方式使用了DataTables AjaxSource - 在初始化例程中指定它。但是,我的服务器端是专门设计的,因此DataTables可以对它说话。

答案 4 :(得分:0)

我最后自己创作了这张桌子。这对我的目的而言已经足够了。

答案 5 :(得分: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

我们实际上为此创建了自己的Serializer,但概念是一样的。循环遍历数据源并填充对象,然后对其进行序列化并做出响应:

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}是传递给函数的值,或直接分配给请求中的那些变量。