Jquery 数据表插件 - sAjaxSource

发布于 2024-08-18 17:24:13 字数 313 浏览 5 评论 0原文

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

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

I get my data from the asp.net web service and I was wondering whether there is a way to pass on that data (in json formar straight from the web service) into the DataTables object. I would like to do something like:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

拥抱影子 2024-08-25 17:24:13

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

$(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)
            } );
        }
    } );
} );

Yes you can do that. Is this what you mean?... pass a data to a server?

$(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)
            } );
        }
    } );
} );
以可爱出名 2024-08-25 17:24:13

您可以在这里找到一篇关于在 ASP.NET MVC 中使用 DataTables 的文章 集成 jQuery DataTables使用 ASP.NET MVC

希望这可以帮助你。几乎没有什么可以帮助您(用于访问 DataTables 参数的模型类和用于直接序列化 JSON 的类)。它使代码变得更干净一些。

问候

Here you can find one article about using DataTables with ASP.NET MVC Integrating jQuery DataTables with ASP.NET MVC.

Hope this could help you. There are few thing that can help you (model class for accessing DataTables parameters and class for direct serialization int the JSON). It makes code little bit cleaner.

Regards

梦情居士 2024-08-25 17:24:13

我现在正在做这件事。我正在使用 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 数据中。

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

I'm working on this right now. I'm using ASP.Net MVC 2 Web Application, using the dataTables religiously, and need to show over 1000 records on each table. Server-Side processing was the way to go for me. Here's what we have.

Here's our declaration in the view (/Admin/Unassigned).

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

Here's our controller function (/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);
}

NOTE!!!!!!! When adding a control, you must put your quotes for your control values as \\" because the backslash needs to be in the JSON data.

Hopefully, in the controller you can access your webservice. I'm not too familiar with web programming, so I don't know what you'd need to do. I just thought this would help.

べ繥欢鉨o。 2024-08-25 17:24:13

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


更新

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

It is possible provided your web service satisfies DataTables API conventions.
Take a look at http://datatables.net/usage/server-side


UPDATE

I've used DataTables AjaxSource in my ASP.NET MVC application the way you want - specifying it in the initialization routine. However, my server side was specially designed so that DataTables could speak to it.

难理解 2024-08-25 17:24:13

最后我自己整理了表格。它足以满足我现在相当小的目的。

I ended up composing the table by myself. It suffices for my purposes that are fairly minimal now.

无力看清 2024-08-25 17:24:13

是的。

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

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

Yes.

Create an object that would store the Datatables data in the correct format. I used a structure:

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

We actually created our own Serializer for this, but the concept is the same. Loop through your data source and populate the object, then serialize it and respond:

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))

That should serialize to the exact format you need. If you need to add data in a specific order, then you'll need to modify the loop. Keep in mind that the {Passed In} is the values being passed into a function or directly assigned to those variables from the request.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文