调用ajax成功后加载数据表的函数

时间:2015-07-09 13:34:31

标签: jquery ajax datatables

这是我第一次使用Jquery和Ajax,所以我对此非常新。我编写了一个Jquery函数,它捕获用户的数据,并在操作结果运行大量linq查询时将其发送给Actionresult,并将Data返回到成功区域(数据)。我遇到麻烦的地方是我需要编写一个抓取(数据)并将其加载到数据表中的函数。我不确定那会是什么样子。我希望有人可以帮助我在数据成功后如何调用数据。我也得到了一个错误(indata){在错误上添加一个}的括号下但我已经做了。

<script>

var ttpath = "../Scripts/copy_csv_xls_pdf.swf";
$(function () {
    $("form").submit(function(e) {

        e.preventDefault();

        var formData = new FormData($(this)[0]);
        //var sheet = $('#SheetName').serialize();
        //var filepath = $('#FileUpload').serialize();

        $.ajax({
            "url": '@Url.Action("GetAttData", "Admin")',
            "data": formData,
            "type": 'POST',
            xhr: function() { 
                var myxhr = $.ajaxSettings.xhr();
                if(myxhr.upload){

                }
                return myxhr;
            },
            cache: false,
            contentType: false,
            processData: false,
            'success': function (data) {
                alert("success")
                loadDataTables(data);
            },
            'error': function () {
                alert("failed")
            },

            function:loadDataTables (inData){

            $('#tstarTable').DataTable({
                "dom": 'T<"clear">lfrtip',
                "tableTools": {
                    "sSwfPath": ttpath
                },
                ajax: {                
                    "type": 'GET',
                },

                columns: [
                    {
                        title: "ATT IP",
                        data: "ATTIP"
                    },


                    {
                        title: "ICCID",
                        data: "ATTICCID"
                    },


                    {
                        title: "STATUS",
                        data: "ATTSTATUS"

                    },

                    {
                        title: "Session",
                        data: "ATTSession"
                    },

                    {
                        title: "Activated",
                        data: "ATTActivated"
                    },

                    {
                        title: "Trackstar IP",
                        data: "IP"
                    },
                    {
                        title: "Vehicle Name",
                        data: "VehicleName"
                    },

                    {
                        title: "Vehicle Group",
                        data: "VehicleGroupName"
                    },

                    {
                        title: "Phone Number",
                        data: "PhoneNumber"
                    },

                    {
                        title: "M5 Match",
                        data: "Unit_No"
                    },

                     {
                         title: "Using Depart",
                         data: "Using_Dept"
                     },

                      {
                          title: "Using Dept Desc",
                          data: "Using_Dept_Desc"
                      },

                       {
                           title: "Status",
                           data: "Status"
                       },

                        {
                            title: "Change Date",
                            data: "Change_DT"
                        }
                ]
            });

    });

});
</script>

1 个答案:

答案 0 :(得分:0)

您的代码中存在一些错误,其结构应如下所示:

var ttpath = "../Scripts/copy_csv_xls_pdf.swf";
$(function () {

   function loadDataTables (inData){             
      $('#tstarTable').DataTable({
         "dom": 'T<"clear">lfrtip',
         "tableTools": {
            "sSwfPath": ttpath
         },
         ajax: {                
            "type": 'GET'
         },          
         columns: [
            {
               title: "ATT IP",
               data: "ATTIP"
            },
            {
               title: "ICCID",
               data: "ATTICCID"
            },
            {
               title: "STATUS",
               data: "ATTSTATUS"
            },
            {
               title: "Session",
               data: "ATTSession"
            },
            {
               title: "Activated",
               data: "ATTActivated"
            },
            {
               title: "Trackstar IP",
               data: "IP"
            },
            {
               title: "Vehicle Name",
               data: "VehicleName"
            },
            {
               title: "Vehicle Group",
               data: "VehicleGroupName"
            },
            {
               title: "Phone Number",
               data: "PhoneNumber"
            },
            {
               title: "M5 Match",
               data: "Unit_No"
            },
            {
               title: "Using Depart",
               data: "Using_Dept"
            },
            {
               title: "Using Dept Desc",
               data: "Using_Dept_Desc"
            },
            {
               title: "Status",
               data: "Status"
            },
            {
               title: "Change Date",
               data: "Change_DT"
            }
         ]
      });
   }

   $("form").submit(function(e) {

       e.preventDefault();

       var formData = new FormData($(this)[0]);
       //var sheet = $('#SheetName').serialize();
       //var filepath = $('#FileUpload').serialize();

       $.ajax({
           "url": '@Url.Action("GetAttData", "Admin")',
           "data": formData,
           "type": 'POST',
           xhr: function() { 
               var myxhr = $.ajaxSettings.xhr();
               if(myxhr.upload){

               }
               return myxhr;
           },
           cache: false,
           contentType: false,
           processData: false,
           'success': function (data) {
               alert("success")
               loadDataTables(data);
           },
           'error': function () {
               alert("failed")
           }
      });
   });
});