2013-06-05 28 views
0

对话形式AJAX的DataTable fnAddData我有一个数据表中定义的文件准备好如下jQuery的距离不工作

$(document).ready(function() { 
var oTable = $('#systemgoals').dataTable({}); 

我有形式的对话,并具有以下功能

buttons: { 
     "Add System Goal": function() { 
      var formfilled = true; 
      $("form#add_systemgoal :text, form#add_systemgoal :file, form#add_systemgoal :checkbox, form#add_systemgoal select, form#add_systemgoal textarea").each(function() { 
        if($(this).val() === "") 

         formfilled = false; 
       }); 
       if(formfilled === true){ 
        $('form#add_systemgoal .error').remove(); 
      var formdata = $('form#add_systemgoal').serialize(); 
      $.ajaxSetup({async: false}); 
      $.ajax({  
       type: "POST", 
       url: '/admin/systemgoals/systemgoalupdate?format=html', 
       data: formdata, 
       success: function (data) { 
        var obj = $.parseJSON(data); 
        if(obj.success){ 
         $(oTable).dataTable().fnAddData([ 
                   obj.inserted.goal_id, 
                   obj.inserted.value, 
                   obj.inserted.status, 
                   obj.inserted.points_per_action, 
                   obj.inserted.created, 
                   obj.inserted.due, 
                   obj.inserted.expires ]);               

         }      
        } 
      }); 
       } 

的一个按钮ajax是好的形式帖子正确的值回应,但fnAddData返回错误

ReferenceError:oTable未定义

任何意见赞赏

谢谢

回答

0

你没有设置你的oTable作为一个全局变量就是为什么oTable你的对话框上没有定义,如果你想将它定义在你的脚本做这样的事情:

var oTable; 
$(document).ready(function() { 
oTable = $('#systemgoals').dataTable({}); 

并在您的对话框

oTable.fnAddData([ 
                   obj.inserted.goal_id, 
                   obj.inserted.value, 
                   obj.inserted.status, 
                   obj.inserted.points_per_action, 
                   obj.inserted.created, 
                   obj.inserted.due, 
                   obj.inserted.expires ]);               

         }  

,或只是简单地做

var oTable = $('#systemgoals').dataTable().fnAddData... 

最好的问候