在Ajax成功后刷新数据表格。

8

我正在使用datatables和jQuery对话框。总体上,我有3个表单和3个datatables。 我的脚本运行得很好,但我遇到的问题是在ajax保存成功后更新正确的datatable(它甚至不必是正确对应的表格,它可以在任何一个3个表单保存中更新所有3个表格)。

任何指导将不胜感激。

带有按钮的页面,用于在对话框中显示datatable/表单

<div style="float:left;">
<button class="menubutton" id="view_academic">Academic</button>
<button class="menubutton" id="view_business">Business/Suppport</button>
<button class="menubutton" id="line_managers">Managers/Divisions</button>
<br/>
<br/>
</div>
<div style="float:right;">
<a href="line_managers_form.php" class="menubutton" id="add_line_managers">Add Managers/Divisions</a>
<a href="academic_form.php" class="menubutton" id="add_academic">Add Academic</a>
<a href="business_form.php" class="menubutton" id="add_business">Add Business/Suppport</a>
<br/>
<br/>
</div>
<div style="clear:both"></div>


<div id="academic_list">
<h2>Academic Entitlements</h2>
<table class="dataTable" id="academic_table" cellpadding="2" cellspacing="2" width="100%">
<thead>
<tr>
<th>Year</th> 
<th>Employee</th>  
<th>Division</th>
<th>Contract</th>
<th>Entitlement</th>
<th>Line Manager</th>
</tr> 
</thead>
<tbody>
    <tr>
        <td colspan="4" class="dataTables_empty">Loading data from server</td>
    </tr>
</tbody>
</table>
</div>

<div id="business_list" class="the_options" style="display:none;">
<h2>Business & Manual Entitlements</h2>
<table class="dataTable" id="business_table" cellpadding="2" cellspacing="2" width="100%">
<thead>
<tr>
<th>Year</th> 
<th>Employee</th>  
<th>FT/PT</th>
<th>Weekly Hours</th>
<th>Division</th>
<th>Commencement</th>
<th>Entitlement</th>
<th>Line Manager</th>
</tr> 
</thead>
<tbody>
    <tr>
        <td colspan="4" class="dataTables_empty">Loading data from server</td>
    </tr>
</tbody>
</table>
</div>

</div>

<div id="line_managers_list" class="the_options" style="display:none;">
<h2>Line Managers & Divisions</h2>
<table class="dataTable" id="line_managers_table" cellpadding="2" cellspacing="2" width="100%">
<thead>
<tr>
<th>Division</th>
<th>Name</th>
<th>Line Manager</th>
</tr> 
</thead>
<tbody>
    <tr>
        <td colspan="4" class="dataTables_empty">Loading data from server</td>
    </tr>
</tbody>
</table>
</div>

初始化Datatables

$(function() {
    // Implements the dataTables plugin on the HTML table
    var $acTable= $("#academic_table").dataTable( {
        "oLanguage": {
            "sSearch": "Filter:"
        },
        "bProcessing": true,
        "bServerSide": true,
        "sAjaxSource": "scripts/academic_serverside.php",
        "iDisplayLength": 10,       
        "bJQueryUI": false,
        "sPaginationType": "scrolling",
        "sDom": '<"top"iflp<"clear">>rt>',
        "sScrollX": "100%",
        "sScrollXInner": "100%",
        "bScrollCollapse": true
        });     

});
$(function() {
    // Implements the dataTables plugin on the HTML table
    var $buTable= $("#business_table").dataTable( {
        "oLanguage": {
            "sSearch": "Filter:"
        },
        "bProcessing": true,
        "bServerSide": true,
        "sAjaxSource": "scripts/business_serverside.php",
        "iDisplayLength": 10,       
        "bJQueryUI": false,
        "sPaginationType": "scrolling",
        "sDom": '<"top"iflp<"clear">>rt>',
        "sScrollX": "100%",
        "sScrollXInner": "100%",
        "bScrollCollapse": true
        });     

});
$(function() {
    // Implements the dataTables plugin on the HTML table
    var $lmTable= $("#line_managers_table").dataTable( {
        "oLanguage": {
            "sSearch": "Filter:"
        },
        "bProcessing": true,
        "bServerSide": true,
        "sAjaxSource": "scripts/line_managers_serverside.php",
        "iDisplayLength": 10,       
        "bJQueryUI": false,
        "sPaginationType": "scrolling",
        "sDom": '<"top"iflp<"clear">>rt>'
        });     

});

$(document).ready(function() {
$(".the_options").hide();
});

对话框/数据表格的显示/隐藏/打开/关闭以及AJAX保存表单:

$(document).ready(dialogForms);
function dialogForms() {
  $('a.menubutton').click(function() {
    var a = $(this);
    $.get(a.attr('href'),function(resp){
      var dialog = $('<div>').attr('id','formDialog').html($(resp).find('form:first').parent('div').html());
      $('body').append(dialog);
      dialog.find(':submit').hide();
      dialog.dialog({
        title: a.attr('title') ? a.attr('title') : '',
        modal: true,
        buttons: {
          'Save': function() {
                submitFormWithAjax($(this).find('form'));
                $(this).dialog('close');
                $lmTable.fnDraw('');
                },
          'Cancel': function() {$(this).dialog('close');}
        },
        close: function() {$(this).remove();},
        width: 600,
        height: 500
      });
    }, 'html');
    return false;
  });
}

function submitFormWithAjax(form) {
  form = $(form);
  $.ajax({
    url: form.attr('action'),
    data: form.serialize(),
    type: (form.attr('method')),
    dataType: 'script',
    success: function(data){
    $(this).dialog('close');
    $lmTable.fnDraw('');
   }
  });
  return false;
}

$(function() {

        $("#add_academic")
            .button()
            .click(function() {
                $("#academic-form").dialog( "open" );
            });
        $("#add_line_managers")
            .button()
            .click(function() {
                $("#line-managers-form").dialog( "open" );
            });
        $("#add_business")
            .button()
            .click(function() {
                $("#business-form").dialog( "open" );
            });
        $("#view_academic")
            .button()
            .click(function() {
                $('#academic_list').show();
                $('#business_list').hide();
                $('#line_managers_list').hide();
            });
        $("#view_business")
            .button()
            .click(function() {
                $('#academic_list').hide();
                $('#business_list').show();
                $('#line_managers_list').hide();
            });
        $("#line_managers")
            .button()
            .click(function() {
                $('#academic_list').hide();
                $('#business_list').hide();
                $('#line_managers_list').show();
            });

});
1个回答

12

要更新表格,只需在其上调用fnDraw()。由于您没有使用全局变量,因此必须先检索该表

var $lmTable = $("#line_managers_table").dataTable( { bRetrieve : true } );
$lmTable.fnDraw();

编辑 - 如果要仅显示正确的表格,您可以执行以下操作:

function dialogForms() {
  $('a.menubutton').click(function() {
    var id = this.id;// Save the id of the clicked button
    var a = $(this);
    $.get(a.attr('href'),function(resp){
      var dialog = $('<div>').attr('id','formDialog').html($(resp).find('form:first').parent('div').html());
      $('body').append(dialog);
      dialog.find(':submit').hide();
      dialog.dialog({
        title: a.attr('title') ? a.attr('title') : '',
        modal: true,
        buttons: {
          'Save': function() {
                submitFormWithAjax($(this).find('form'), id);// Pass the id to the function 

function submitFormWithAjax(form, id) {
  form = $(form);
  var table_id;
  // Choose the table to display depending on the id, i made some guesses but adjust this
  switch(id){
    case 'view_academic': table_id = '#academic_table';
    break;
    case 'view_business': table_id = '#business_table';
    break;
    case 'line_managers': table_id = '#line_managers_list';
    break;
  }
  $.ajax({
    url: form.attr('action'),
    data: form.serialize(),
    type: (form.attr('method')),
    dataType: 'script',
    success: function(data){
        $(this).dialog('close');
        // Refresh table
        var oTableToUpdate =  $(table_id).dataTable( { bRetrieve : true } );
        $oTableToUpdate .fnDraw();
        // Hide all tables
        $('table').hide();
        // Show the refreshed
        $(table_id).show();

   }
  });
  return false;
}

@编码得很好,实现起来应该不会太难,但是我甚至在您的标记中没有看到表单 :) 如果您在 jsfiddle 上提供一个示例,我可以帮忙。请参阅此处有关如何在 jsfiddle 中模拟 ajax 调用的说明:http://newcodeandroll.blogspot.it/2012/01/how-to-receive-response-from-ajax-calls.html - Nicola Peluchetti
它们只是由我的问题中的按钮发起的简单POST表单。它们在对话框中打开,并通过mysql INSERT提交给php脚本。使用submitFormWithAjax($(this).find('form'));提交。 - Codded
谢谢你提供的解决方案,看起来很有前途,尽管它没有按照预期工作。它已经停止了表格更新并且没有显示正确的表格。有什么想法吗? - Codded
我能和你私聊吗?我觉得我知道问题出在哪里,但不确定如何解决。 - Codded
@Codded 这很奇怪。你是否通过 console.log 或 alert 来检查 fnDraw() 是否真的被调用了? - Nicola Peluchetti
显示剩余7条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接