刷新 Ajax 成功的数据表

2024-04-05

我正在使用数据表和 jquery 对话框。总的来说,我有 3 个表格和 3 个数据表。 我的脚本运行良好,但我遇到的问题是在 ajax 保存成功时更新正确的数据表(它甚至不必是正确的对应表,它可以更新 3 个表单保存中任何一个的所有 3 个表。 )

任何指导将不胜感激。

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

<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>

初始化数据表

$(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();
            });

});

要更新表,只需调用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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

刷新 Ajax 成功的数据表 的相关文章

随机推荐

  • 当我使用 getView() 获取 ViewPager 中 Fragment 的根视图时,它返回 null

    我有一个包含两个片段的活动 这些片段由 ViewPager 呈现给用户 因此 我为 ViewPager 的适配器创建了一个扩展 FragmentStatePagerAdapter 的类 MainFragsAdapter 类 并重写了它的一些
  • 无法在 mac 10.10 上安装 or-tools

    我正在尝试在 mac 10 10 上安装 Google 的 or tools https code google com p or tools wiki OrToolsWithPyPi https code google com p or
  • 关于可用性和“美观”的争论

    作为一名 UI 人员 编码和设计用户界面 我经常发现自己处于一种奇怪的境地 与程序员和其他 外行 争论用户界面的质量 我发现争论颜色 图标或布局之类的事情有点困难 而且似乎没有事实的对错 但是 尽管没有令人信服的论据 我仍然知道 即感觉 有
  • 根据相邻列的值替换多列中的值

    Create a data frame gt df lt data frame a rnorm 7 b rnorm 7 c rnorm 7 threshold rnorm 7 gt df lt round abs df 2 gt gt df
  • 确定两个数字在四舍五入到 n 个有效小数位后是否几乎相等的函数

    我被要求测试第三方提供的库 该库已知准确n重要数字 任何不太重要的错误都可以安全地忽略 我想写一个函数来帮助我比较结果 def nearlyequal a b sigfig 5 该函数的目的是确定两个浮点数 a 和 b 是否近似相等 如果
  • Groovy:如何在字符串中包含反斜杠而不转义?

    我想在我的 groovy 程序中使用以下字符串文字 而不必转义反斜杠 C dev username 这是我到目前为止所尝试过的 字符串 单引号 和 GString 双引号 def aString C dev username def aGS
  • 在 MATLAB 中加载多个图像

    这是所需的工作流程 我想将 100 张图像加载到 MATLAB 工作区 在图像上运行一堆我的代码 将我的输出 我的代码返回的输出是一个整数数组 保存在一个新数组中 最后 我应该有一个数据结构来存储图像 1 100 的代码输出 我该怎么做呢
  • Javascript:如何在处理程序中保留对请求发起者的引用?

    我通常不是一个 Javascript 人 但我一直在潜心阅读道格拉斯 克罗克福德的书 http oreilly com catalog 9780596517748 并编写一些琐碎 有用的花絮作为 Chrome 扩展和Node js http
  • boost::asio::io_service 在 win_mutex 锁中崩溃

    我一直遇到 boost asio 问题 其中使用全局 io service 实例创建的计时器和 或套接字在构造过程中崩溃 发生崩溃的系统如下 Windows 7的 适用于 Windows 桌面的 Visual Studio 2013 Exp
  • python读取文件utf-8解码问题

    我在读取包含 UTF8 和 ASCII 字符的文件时遇到问题 问题是我使用seek仅读取数据的某些部分 但我不知道我是否在UTF8的 中间 读取 osx 蟒蛇3 6 6 简而言之 我的问题可以用以下代码来演示 write some utf
  • 如何使用 Jquery 创建 .txt 文件?

    好的 这是我的域 example com index html 我想在该域中创建一个 txt 文件 结果 example com file txt 包含以下信息 js saveButton on click function e e pre
  • Python-Sphinx:从超类“继承”方法文档

    Edit 截至目前 Sphinx 1 4 9 似乎没有办法告诉 Sphinx 做我想做的事情 参见issue https github com sphinx doc sphinx issues 3140在 GitHub 上 这接受的答案 h
  • .NET GUI 中仍然使用本机 Windows 控件吗?

    当您使用 WinForms 或 WPF 创建 GUI 时显示的内容仍然基于本机控件 例如通用控制 http msdn microsoft com en us library windows desktop bb773169 28v vs 8
  • Android 接收器可以进行多种操作?

    简单的问题 我可以注册一个吗BroadcastReceiver多个 Intent 操作 这是我正在考虑的
  • linkedin 上的 Open Graph 图片问题

    我的项目现在遇到有关 linkedin 共享的问题 图像 缩略图 未显示 我的问题是 网址更改也会影响吗 例如 我添加了这样的 og 标签 https domain name com wp content uploads 2016 10 2
  • 如何更改 Angular 中 mat-dialog 的 Z 索引

    我的应用程序使用多个 mat dialogs 有时可能会同时显示 2 个 这会导致问题 因为第二个永远不会正确显示 而且它的模式使应用程序变得无用 经过更多研究后 我似乎可以通过调整 mat dialog 的 z index cdk ove
  • 如何获取通过SpriteKit编辑器创建的项目的SKSpriteNode?

    我使用 SpriteKit 使用 Objective C 在 XCode 中创建了一个相当简单的 此时是实验性的 游戏 我知道如何手动创建 SKSpriteNode 对象并将其添加到 SKScene 但我有点尝试做相反的事情 我在 XCod
  • Spark.ml 回归计算的模型与 scikit-learn 不同

    我在 scikit learn 和 Spark ml 中设置一个非常简单的逻辑回归问题 结果有所不同 他们学习的模型不同 但我不明白为什么 数据相同 模型类型是相同 正则化相同 毫无疑问 我错过了一侧或另一侧的一些设置 哪个设置 我应该如何
  • Visual Studio C# - 未找到 SQLite.Interop.dll

    我目前正在尝试使用 Visual Studio 创建一个与 SQLite 一起使用的 C 应用程序 我使用 NuGet 为我的程序安装了 SQLite 解决方案资源管理器中出现了三个引用 System Data SQLite System
  • 刷新 Ajax 成功的数据表

    我正在使用数据表和 jquery 对话框 总的来说 我有 3 个表格和 3 个数据表 我的脚本运行良好 但我遇到的问题是在 ajax 保存成功时更新正确的数据表 它甚至不必是正确的对应表 它可以更新 3 个表单保存中任何一个的所有 3 个表