【问题标题】:jQuery datatable setting column design and value in success callback成功回调中的jQuery数据表设置列设计和值
【发布时间】:2017-06-02 15:04:32
【问题描述】:

我为我的数据表编写了以下代码,它用我的数据库中的内容填充表格,如下所示:

 if (datatable != null)
            datatable.destroy();

        datatable = $('#tableProducts').DataTable({
            "pageLength": 50,
            "bLengthChange": false,
            "processing": true,
            "serverSide": true,
            "filter": true,
            "orderMulti": false,
            "bSort": false,
            "ajax": {
                "url": "/Bulk/LoadData/",
                "type": "POST",
                "data": {
                    "username": $(".sellerInput").val(),
                    "drange": $(".select2").val()
                },
                success: function (data) {
                }
            },
            "columns": [
                      {
                          "targets": -1,
                          "data": "ImageURL",
                          "name": "Title",
                          "render": function (data, type, row) {
                              return '<td><img src=' + data + '></td>';
                          },
                          "autoWidth": true
                      },
                {
                    "data": "Sales",
                    "name": "QuantitySold",
                    "render": function (data, type, row) {

                        return '<td>' + data + '</td>'
                    },
                },
                 {
                     "data": "CurrentPrice",
                     "name": "CurrenctPrice",
                     "render": function (data, type, row) {

                         return '<td> <b>$ ' + data + '</b></td>'
                     },
                 }
            ]
        });

如果我不指定成功回调,这很好用。如果我这样指定成功回调(也显示在上面的代码中):

"ajax": {
       "url": "/Bulk/LoadData/",
       "type": "POST",
       "data": {
                "username": $(".sellerInput").val(),
                "drange": $(".select2").val()
               },
                success: function (data) {
               // some custom code here and + filling up datatable with data from my DB...
               }
   },

这里的问题是,如果我指定成功回调,然后更新我的 HTML 页面的其他部分,那么数据表不会加载数据库中的数据..

我的问题是,如果我覆盖数据表的成功回调函数以更新我的 HTML 页面的更多部分而不仅仅是数据表本身,我该如何手动指定数据表的数据源?

有人可以帮帮我吗?

【问题讨论】:

    标签: javascript jquery html ajax datatables


    【解决方案1】:

    你可以使用dataSrc代替success来获取响应数据

    试试这个:

    "ajax": {
           "url": "/Bulk/LoadData/",
           "type": "POST",
           "data": function (d){
                    d.username: $(".sellerInput").val(),
                    d.drange: $(".select2").val()
           },
           "dataSrc": function (data) {
                   // some custom code here and + filling up datatable with data from my DB...
                console.log(data);
                return data;
           }
       },
    

    注意:另外你正在尝试发送额外的参数为usernamedrange,所以在DataTable中我们应该使用"data":function(d){作为函数来发送额外的参数

    Official Documentation

    【讨论】:

    • 为什么要返回数据?我想用这些数据更新 HTML。我不能这样做(例如):$("#Title").text(data.Title);或类似的东西?
    • 勾选here操作数据,需要return才能处理dataTable中的数据
    • 哦,好吧,我现在明白你的意思了。但是一些返回的数据用于操作我页面的其他部分,而不仅仅是数据表:D
    • @User987 完全正确 :) 还要检查如何使用数据作为函数发送额外的数据参数
    • 太辛格了,我的朋友!我今天学到了一些有用的东西! =D 好的 .. 所以我已经用这种方式测试过了,但它说它期望; ?
    【解决方案2】:

    来自docs

    成功 - 不能被覆盖,因为它在内部使用 数据表。操作/转换服务器返回的数据 使用 ajax.dataSrc。

    所以简单地使用

    ajax: {
       url: "/Bulk/LoadData/",
       type: "POST",
       data: {
         "username": $(".sellerInput").val(),
         "drange": $(".select2").val()
       },
       dataSrc: function (data) {
         // some custom code here and + filling up datatable with data from my DB...
         return data
       }
    },
    

    改为。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-22
      相关资源
      最近更新 更多