简体   繁体   English

如何在Ajax调用中将oData(从DataTable)发送为“数据”?

[英]How can I send oData (from DataTable) as “data” in my Ajax Call?

I have an Ajax Table based on DataTable plug-in, which keep search parameters in localStorage. 我有一个基于DataTable插件的Ajax表,该插件将搜索参数保留在localStorage中。 All good by now. 现在一切都很好。

a) console.debug( oData ) returns: a) console.debug( oData )返回:

Object
    ...
    time : 1488468181954
    name : "John Doe"
    ...

b) And here is my script : b)这是我的脚本

var ShowUsers = function () {

    var Users = function () {

        var grid = new Datatable();

        grid.init({
            src: $("#datatable_ajax")

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

                 // save custom filters to the state
                "fnStateSaveParams": function ( oSettings, sValue ) {
                    $("#datatable_ajax tr.filter .form-control").each(function() {
                        sValue[$(this).attr('name')] = $(this).val();
                    });

                    return sValue;
                },

                // read and populate the filter inputs
                "fnStateLoadParams": function ( oSettings, oData ) {
                    //Load custom filters
                    $("#datatable_ajax tr.filter .form-control").each(function() {
                        var element = $(this);
                        if (oData[element.attr('name')]) {
                            element.val( oData[element.attr('name')] );
                        }
                    });

                    return true;
                },

                "ajax": {
                    "url": "/admin/users", // ajax source
                    "data": {
                        name: oData.user_name // here is the problem
                    },
                    "headers": {
                        'X-CSRF-TOKEN': '{{ csrf_token() }}'
                    }
                }
            }
        });
    }

    return {
        init: function () {
            Users();
        }
    };
}();

jQuery(document).ready(function() {
    ShowUsers.init();
});

The problem appear when I try to use oData as a variable in the Ajax Call. 当我尝试在Ajax调用中使用oData作为变量时,会出现问题。

"ajax": {
    ...
    "data": {
        name: oData.user_name // return the Undefined error
    },
    ...
}

I was trying many solutions, the best so far it was to load Ajax in fnStateLoaded , like this: 我正在尝试许多解决方案,到目前为止,最好的办法是将ajax加载到fnStateLoaded中 ,如下所示:

"fnStateLoaded": function (oSettings, oData) {
  // Ajax here
},

... but it is useless because I need Ajax to remain "ajax": {} not $.ajax( {}); ...但这没用,因为我需要Ajax保持“ ajax”:{}而不是$ .ajax({}); .

That being said, any advice on how to get oData and use it? 话虽如此,关于如何获取和使用oData的任何建议?

PS: I tried to get data right from the inputs, but at that time fnStateLoadParams did not manage to pass the data to the Inputs, so $("input[name=user_name]").val(); PS:我试图从输入中正确获取数据,但当时fnStateLoadParams未能将数据传递到输入,因此$(“ input [name = user_name]”)。val(); returns undefined as well. 也返回undefined

Thank you all in advance. 谢谢大家。

David 大卫

You can either assign oData to a global variable or use state.loaded() API method to retrieve last loaded state in Ajax call. 您可以将oData分配给全局变量,也可以使用state.loaded() API方法检索Ajax调用中的最后一个加载状态。

For example: 例如:

"ajax": {
    "url": "/admin/users", // ajax source
    "data": function(data, settings){
       var api = new $.fn.dataTable.Api( settings );

       var state = api.state.loaded();
       if(state.hasOwnProperty('user_name')){
          data.name = state.user_name;
       }
    },
    "headers": {
       'X-CSRF-TOKEN': '{{ csrf_token() }}'
    }
}

I found a clean and easy solution for this problem. 我找到了解决此问题的简便方法。

grid.getDataTable().ajax.params();

After I obtained the oData I pass it to the Ajax like this: 获得oData之后,将其传递给Ajax,如下所示:

var Users = function () {
    ...
}

oData = grid.getDataTable().ajax.params(); // get oData
grid.setAjaxParam("name", oData.name); // pass the parameter
grid.getDataTable().ajax.reload(); // reload the table

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将ajax调用发送到服务器并在数据库中插入数据? - How can i send an ajax call to the server and inserting data in my database? 我如何在Ajax发布呼叫中发送不同类型的数据 - How can I send different types of data in ajax post call 在我的情况下,如何在 AJAX 回调 function 之外访问从 ajax 调用返回的数据? - How can I access data returned from ajax call outside AJAX callback function in my case? 如何从 JavaScript ajax 调用和打印来自 Django 模板中视图的返回数据 - How can I send localstorage data from JavaScript ajax call and print return data from views in Django template 我如何从控制器向我的ajax发送一个http? - how can i send a http to my ajax from the controller? 我如何在ajax数据中发送字典 - how can i send a dict in data of ajax 如何从 UI 中的数据表中删除行,尽管我可以使用 Ajax 调用从服务器成功删除 - how can I delete the row from datatable in UI,though i could successfully delete from server using Ajax call Ajax 调用 - 我需要一个将表数据发送到我的 PHP 查询的 Ajax 调用 - Ajax Call - I need a Ajax call that will send table data to my PHP query 如何使用 Ajax 在数据表数组上显示数据? - How can i display on a datatable array data using Ajax? 如何通过进行ajax调用将html表单数据发送到node.js服务器? - how can i send html form data to node.js server by making ajax call?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM