【问题标题】:jquery datatable issue no data retrievedjquery数据表问题没有检索到数据
【发布时间】:2015-11-27 23:43:29
【问题描述】:

我使用 jQuery DataTable 通过 C# web 方法填充表格是 AJAX 请求。 我的问题是没有返回任何数据或任何错误。谁能帮帮我?

JS代码:

var TableAjax = function () {

    var initPickers = function () {
        //init date pickers
        $('.date-picker').datepicker({
            rtl: Metronic.isRTL(),
            autoclose: true
        });
     }

    var handleRecords = function () {
    var grid = new Datatable();
    grid.init({
        src: $("#datatable_ajax"),
        onSuccess: function (grid) {
            // execute some code after table records loaded
        },
        onError: function (grid) {
            // execute some code on network or other general error  
        },
        onDataLoad: function (grid) {
            // execute some code on ajax data load
        },
        loadingMessage: 'Loading...',
        DataTable: { // here you can define a typical datatable settings from http://datatables.net/usage/options 
            // Uncomment below line("dom" parameter) to fix the dropdown overflow issue in the datatable cells. The default datatable layout
            // setup uses scrollable div(table-scrollable) with overflow:auto to enable vertical scroll(see: assets/global/scripts/datatable.js). 
            // So when dropdowns used the scrollable div should be removed. 
            //"dom": "<'row'<'col-md-8 col-sm-12'pli><'col-md-4 col-sm-12'<'table-group-actions pull-right'>>r>t<'row'<'col-md-8 col-sm-12'pli><'col-md-4 col-sm-12'>>",

            "bStateSave": true, // save datatable state(pagination, sort, etc) in cookie.

            "lengthMenu": [
                [10, 20, 50, 100, 150, -1],
                [10, 20, 50, 100, 150, "All"] // change per page values here
            ],
            "pageLength": 10, // default record count per page
            "ajax": {
                url: "/WS/WS.asmx/GetCustomers",
                type: 'GET',
                dataType: 'json',
                contentType: 'application/json; charset=utf-8',

            },
            "aoColumns": [
              { "mData": "CustomerAddress" },
              { "mData": "CustomerName" },
              { "mData": "CustomerCode" },
              { "mData": "CustomerCode" },
              { "mData": "CustomerCode" },
              { "mData": "CustomerCode" },
              { "mData": "CustomerCode" }
            ],
            "order": [
                [1, "asc"]
            ]// set first column as a default sort by asc
        }
    });

    // handle group actionsubmit button click
    grid.getTableWrapper().on('click', '.table-group-action-submit', function (e) {
        e.preventDefault();
        var action = $(".table-group-action-input", grid.getTableWrapper());
        if (action.val() != "" && grid.getSelectedRowsCount() > 0) {
            grid.setAjaxParam("CustomerId", "group_action");
            grid.setAjaxParam("CustomerName", action.val());
            grid.setAjaxParam("id", grid.getSelectedRows());
            grid.getDataTable().ajax.reload();
            grid.clearAjaxParams();
        } else if (action.val() == "") {
            Metronic.alert({
                type: 'danger',
                icon: 'warning',
                message: 'Please select an action',
                container: grid.getTableWrapper(),
                place: 'prepend'
            });
        } else if (grid.getSelectedRowsCount() === 0) {
            Metronic.alert({
                type: 'danger',
                icon: 'warning',
                message: 'No record selected',
                container: grid.getTableWrapper(),
                place: 'prepend'
            });
        }
    });
}

return {
    //main function to initiate the module
    init: function () {
        initPickers();
        handleRecords();
    }
};

}();

还有我的网络方法代码:

[WebMethod(EnableSession = true)]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public object GetCustomers()
{
    DataTable dt = ICMS_DB_CLS.GetDataFromDB("Select * from  t_crm_customers");
    List<Customers> lis = new List<Customers>();
    lis = ExtensionList.ToListof<Customers>(dt);
    return new
    {
        data = lis,
        length = lis.Count
    };
}

我的问题没有返回任何数据或任何错误。

【问题讨论】:

    标签: c# jquery asp.net ajax datatable


    【解决方案1】:

    此时你只是返回一个对象。

    试试:

    [WebMethod(EnableSession = true)]
    [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
    public JsonResult GetCustomers()
    {
        DataTable dt = ICMS_DB_CLS.GetDataFromDB("Select * from  t_crm_customers");
        List<Customers> lis = new List<Customers>();
        lis = ExtensionList.ToListof<Customers>(dt);
        var toJson = new
             {
                 data = lis,
                 length = lis.Count
             };
        return Json(toJson, JsonRequestBehaviour.AllowGet);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-14
      相关资源
      最近更新 更多