使用ajax json加载数据后,Bootstrap数据表无法正常工作

时间:2016-01-04 16:40:07

标签: jquery html ajax jsp datatables

我正在将数据动态加载到html表中,如下所示 使用的技术堆栈是:Spring MVC Hibernate Ajax JQuery 但是数据表功能没有响应

function getdata()
     {
            $.ajax({

            type: "GET",
             url: "/controllerURL.html", //controller URL
             contentType: "application/json; charset=utf-8",
             dataType: "json",    

             success: function (results) {
                console.log(results)
                 var success = results.success;
                if(success){
                var finaldata = "<tbody><thead><tr><th>ID</th><th>data1</th><th>data2</th><th>Update</th></tr></thead>"; //data
                var data = results.message;
                    data = jQuery.parseJSON(data);
                alert(data);
                    for(var i = 0; i < data.length; i++){
                        var value = data[i];                
                        finaldata = finaldata+  "<tr><th>"+value.ID+"</th><th>"+value.variable1+"</th><th>"+value.variable2+"</th></tr>";
                                                    }
                    finaldata = finaldata + "</tbody>";
                $("#tableID").html(finaldata);
                $("#tableID").DataTable();//re-intializing datatable
                 }            
             },
             error: function (data) {       
                alert("fail");
             console.log('ajax call error');    

             }
         });
     }

此数据表功能无效后

3 个答案:

答案 0 :(得分:2)

您需要拥有<thead></thead><tbody></tbody>的正确表格结构。此外,th元素的数量应与td元素的数量相匹配。

假设代码的其他部分正常工作,请改用以下代码:

function getdata() {
    $.ajax({
        type: "GET",
        url: "/controllerURL.html", //controller URL
        contentType: "application/json; charset=utf-8",
        dataType: "json",

        success: function(results) {
            console.log(results)
            var success = results.success;
            if (success) {
                var finaldata = "<thead><tr><th>ID</th><th>data1</th><th>data2</th><th>Update</th></tr></thead><tbody>"; //data
                var data = results.message;
                data = jQuery.parseJSON(data);
                alert(data);
                for (var i = 0; i < data.length; i++) {
                    var value = data[i];
                    finaldata = finaldata + "<tr><td>" + value.ID + "</td><td>" + value.variable1 + "</td><td>" + value.variable2 + "</td><td></td></tr>";
                }
                finaldata = finaldata + "</tbody>";
                $("#tableID").html(finaldata);
                $("#tableID").DataTable();
            }
        },
        error: function(data) {
            alert("fail");
            console.log('ajax call error');

        }
    });
}

答案 1 :(得分:1)

$.ajax({..

 success: function(data) {..  

var table = $('#datatable').DataTable();
table.clear().draw();
var rowNode= new Array();
for (var key=0, size=data.length; key<size; key++){
            var j = -1;
            var r = new Array();
// represent columns as array
                r[++j] ='<tr><td><input type="hidden" name="somename1" value="'+data[key].id+'"/><input type="hidden" name="somename2" value="'+data[key].is_deleted+'"/>'+data[key].lic_category_name+'</td>';
                r[++j] = '<td>'+data[key].someval1+'</td>';
                r[++j] = '<td>'+ data[key].someval2+'</td>';
                r[++j] = '<td>'+ data[key].someval13+ '</td>';
                r[++j] ='<td class="last"><a href="view?id='+data[key].id+' title="View"><i class="fa fa-eye"></i></a> <a href="edit?id='+data[key].id+'title="Edit"><i class="fa fa-edit"></i></a> <a href="delete?id='+data[key].id+'onclick="return confirm("Are you sure you want to delete?")" title="Delete"><i class="fa fa-trash"></i></a></td></tr>'; 
                rowNode = table.row.add(r);

        }

        rowNode.draw().node()

   }
}

我的JSON回复,

[{"id":70,"somekey1":"GC 1","somekey2":"GC 1","somekey3":8,"somekey4":5000,"somekey5":1,"somekey5":1,"is_deleted":0}]

它为我工作

答案 2 :(得分:0)

要在ajax响应上加载动态数据表,请通过下面的代码

$.ajax({
     type: "GET",
     url: "Controller/Action",
     async: true,
     contentType: "application/json; charset=utf-8",
     dataType: "json",
     success: function (data) {
         if (data !== null && data.length > 0) {
             var tableData = JSON.parse(data);

             $("#table0").DataTable().destroy();


             $("#table0").DataTable({
                 dom: "Bfrtip",
                 data: tableData, // this is to be based on your json structure 

                 columns: [

                     {
                         className: 'select-checkbox',

                         orderable: false,

                         data: null,

                         defaultContent: ''

                     },

                     { data: "name" },
                     { data: "position" },
                     { data: "office" },
                     { data: "extn." },
                     { data: "start_date" },
                     { data: "salary" }

                 ],
                 rowReorder: {
                     dataSrc: 'DT_Rowid'
                 },
                 select: {
                     style: 'os',
                     selector: 'td:first-child'
                 }
                 , buttons: [
                     { extend: "create", editor: editor },
                     { extend: "edit", editor: editor },
                     { extend: "remove", editor: editor }
                 ]

             });


         }
     },
     error: function Error(result, error) {
         alert("error " + result.status + " " + result.statusText);
     }
 });}

详细说明Data table