【问题标题】:Is there a way to get aocolumns from the server in datatable editable?有没有办法从可编辑的数据表中的服务器获取 aocolumns?
【发布时间】:2015-07-18 22:18:00
【问题描述】:

因此,我正在尝试为用户请求的任何表格制作一个通用页面。为此,我试图从服务器获取所有数据,并且在客户端没有任何硬编码。

 $(document).ready(function(){

     /* Add/remove class to a row when clicked on */
    $('#table1 tr').on('click', function() {
        $(this).toggleClass('row_selected');
    } );


     var which_table=window.location.pathname;
     var which_table_data=which_table.substring(0, which_table.length-1)+'/data';
     var table_name=which_table.substring(14, which_table.length-1);
     $('#table1').dataTable({
            "bProcessing": true,
            "bServerSide": true,
            "bjQueryUI": true,
            "sAjaxSource": which_table_data,
            "bPaginate": true,
            "sPaginationType": "full_numbers",
            "bjQueryUI": true,
            "sScrollX":"100%",
            "aoColumnDefs": [{
                "targets" : [0],
                "visible" : false,
                "searchable" : false
            }]
    }).makeEditable({
         "sUpdateURL": "../update/" + table_name,
         "sAddURL": "../add/" + table_name,
         "sDeleteURL": "../delete/" + table_name,
         "aoColumns": $.ajax({
                      dataType: "json",
                      url: '/get_aoColumns',
                      data: function (table_name) {
                          return {
                              q: table_name
                          };
                      },

                      results: function (data) {
                            alert(data);
                        }

                    });

    });


 });

所以在这篇基于var which_table=window.location.pathname; 的文章中,我尝试从我成功的服务器中获取相应表的数据。但现在我什至想从服务器获取 aoColumns 数组。我的问题是我可以在同一个请求中发送数据以及 aoData、secho 和其他必填字段。我认为这可能无法正确呈现数据表,因为 aoColumns 不是所需 json 的一部分。如何获取任何表的 aoColumns,这样即使验证也成为服务器端,我不必为每个表设计一页。

下面这部分不起作用,因为我不知道正确的做法是什么

"aoColumns": $.ajax({
                      dataType: "json",
                      url: '/get_aoColumns',

已编辑:-

我尝试了@garryp 的方法..

这是我从服务器获取的数据

[{"cssclass": "required", "type": "textarea"}, {"sUpdateURL": "../update/my_table", "cssclass": "required", "type": "textarea", "loadtype": "POST", "submit": "OK"}, {"loadurl": "../data/", "sUpdateURL": "../update/my_table", "loadtype": "POST", "type": "select", "submit": "OK"}]

这是我的代码

 $(document).ready(function(){

     /* Add/remove class to a row when clicked on */
    $('#table1 tr').on('click', function() {
        $(this).toggleClass('row_selected');
    } );



     var which_table=window.location.pathname;
     var which_table_data=which_table.substring(0, which_table.length-1)+'/data';
     var table_name=which_table.substring(14, which_table.length-1);
     if(table_name.indexOf('Welog')> -1) {
         $('#table1').dataTable({
             "bProcessing": true,
             "bServerSide": true,
             "bjQueryUI": true,
             "sAjaxSource": which_table_data,
             "bPaginate": true,
             "sPaginationType": "full_numbers",
             "bjQueryUI": true,
             "sScrollX": "100%"
             });
           $('#formAddNewRow').hide();


        }
     else {
         $.ajax({
             url: '../get_aodata/' + table_name,
             async: false,
             success: function (data) {
                 alert(data);
                 $('#table1').dataTable({
                     "bProcessing": true,
                     "bServerSide": true,
                     "bjQueryUI": true,
                     "sAjaxSource": which_table_data,
                     "bPaginate": true,
                     "sPaginationType": "full_numbers",
                     "bjQueryUI": true,
                     "sScrollX": "100%",
                     "aoColumnDefs": [{
                         "targets": [0],
                         "visible": false,
                         "searchable": false
                     }]
                 }).makeEditable({
                     "sUpdateURL": "../update/" + table_name,
                     "sAddURL": "../add/" + table_name,
                     "sDeleteURL": "../delete/" + table_name,
                     "sAddDeleteToolbarSelector": "#table1_length",
                     "aoColumns" : data

             /*"aoColumns" : [
                         {
                             "cssclass": "required",
                             "type":"textarea"
                         },
                         {
                             "cssclass": "required",
                             "type":"textarea",
                             "submit"  : "OK",
                             "sUpdateURL": "../update/"+table_name,
                             "loadtype" : "POST"
                         },
                         {
                             "loadurl" : "../data/",
                             "type" : "select",
                             "submit": "OK",
                             "sUpdateURL": "../update/"+table_name,
                             "loadtype" : "POST"
                         }
                     ]*/

                 });
             }
         });
     }

 });

因此,如果您看到这段代码中注释掉的 aoColumns 与从服务器获得的输出完全相同,但从服务器获得的输出在 makeditable 函数中根本没有提到t work and the one commented out if uncommented does work. The one got from the server if used using aoColumns : data just behaves the same way as if aoColumns parameter wasnt。这是否意味着数据未达到该参数,因为我在控制台中没有收到任何错误。

解决方案:-

success : function(data){
  var data_str= JSON.parse(data);
});

好的。我不得不使用 parse 将 json 字符串转换为 JSobject,然后它终于起作用了。

【问题讨论】:

    标签: jquery ajax datatables jquery-datatables-editor


    【解决方案1】:

    它不起作用,因为您在这里将$.ajax(...) 的返回值分配给aoColumns(当您实际上需要将列数组分配给“aoColumns”时):

    }).makeEditable({
    
         ...
    
         "aoColumns": $.ajax({
    

    相反,您需要做的是首先调用 AJAX。然后,在 jQuery success 函数中设置你的数据表。

    $.ajax({
        url: '/get_aoColumns',
        ...
        success : function(data) {
            // ToDo: put all your datatable code in here.
            // and assign `data` to "aoColumns"
    
                /** data table code **/
    
            }).makeEditable({
            "aoColumns": data
    
                /** rest of data table code **/
        }
    

    我已尝试将除重要部分之外的所有内容都保留下来,以明确关键点,但这应该有助于您了解哪里出错了。

    如果这没有意义,我已经在这里设置了一个带有(未经测试的)代码示例的 JS Fiddle:

    http://jsfiddle.net/GarryPas/got4fxhb/1/

    【讨论】:

    • 明白了要点。让我试试。我也在研究同样的问题。
    • 好的,我也添加了一个 JS fiddle,以防出现混乱。
    • 嗨,对不起,我没有关注这个帖子。我注意到你几个小时前编辑了。这是否意味着您的代码现在可以工作了?
    【解决方案2】:

    假设/get_aoColumns 正确返回所有内容,看起来您想先获取该信息,然后在成功处理程序中创建数据表。在您上面的代码中,看起来 dataTables 声明可以在 ajax 请求有机会完成之前完成,那么这个怎么样:

    $(document).ready(function () {
        /* Add/remove class to a row when clicked on */
        $('#table1 tr').on('click', function () {
            $(this).toggleClass('row_selected');
        });
        var which_table = window.location.pathname;
        var which_table_data = which_table.substring(0, which_table.length - 1) + '/data';
        var table_name = which_table.substring(14, which_table.length - 1);
    
        //wrap the ajax request to get aoColumns outside of the initializaer
        $.get('/get_aoColumns', {q: table_name}, function (aoColumns) {
            $('#table1').dataTable({
                "bProcessing": true,
                "bServerSide": true,
                "bjQueryUI": true,
                "sAjaxSource": which_table_data,
                "bPaginate": true,
                "sPaginationType": "full_numbers",
                "sScrollX": "100%",
                "aoColumnDefs": [{
                        "targets": [0],
                        "visible": false,
                        "searchable": false
                    }]
            }).makeEditable({
                "sUpdateURL": "../update/" + table_name,
                "sAddURL": "../add/" + table_name,
                "sDeleteURL": "../delete/" + table_name,
                "aoColumns": aoColumns //the data retrieved from the request to get_aoColumns
            });
        });
    });
    

    【讨论】:

    • 哈,谢谢,是的,我想这不是你,我在问“沉默的匿名投票者”。这对你有什么帮助吗?我猜没有
    • 没有。这种方法不起作用。你能看到我更新的问题并提出问题所在吗? @chiliNUT
    猜你喜欢
    • 1970-01-01
    • 2021-06-04
    • 2021-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-31
    • 2023-02-22
    • 1970-01-01
    相关资源
    最近更新 更多