使用jquery向数据表添加行

时间:2015-10-01 05:19:20

标签: jquery angularjs database html5 datatable

我试图通过使用metronic主题为datatable文件赋值来直接向.js添加行。但是datatable没有添加任何值。问题是它没有反映在表上。当调试它时,项目值会显示但不会被添加到表的行数组中。

HTML code:



    <!-- BEGIN MAIN CONTENT -->
                <div class="row" ng-controller="compctrl">
                    <div class="col-md-12">
                        <!-- BEGIN EXAMPLE TABLE PORTLET-->
                        <div class="portlet box blue-hoki">
                            <div class="portlet-title">
                                <div class="caption">
                                    <i class="fa fa-globe"></i>Datatable with TableTools
                                </div>
                                <div class="tools">
                                </div>
                            </div>
                            <div class="portlet-body">
                                <table class="table table-striped table-bordered table-hover " id="sample_1">
                                    <thead>
                                        <tr>
                                            <th>
                                                name
                                            </th>
                                            <th>
                                                strength
                                            </th>
                                        </tr>

                                    </thead>
                                    </table> 
                            </div>
                        </div>
                        <!-- END EXAMPLE TABLE PORTLET-->


  .js code:
MetronicApp.controller('compctrl', ['$scope',function($scope) {
        $scope.$on('$viewContentLoaded', function() {

    alert("Controller called!");

            var tableData= [{'name':'Infosys Technologies','strength':1000}];

                  var table=$('sample_1').DataTable();

              tableData.forEach(function(item)
              {
                            var row=[];  
                            row.push(item.name); 
                            row.push(item.strength);
                            table.add(row);

               });
            table.draw(row);
                 } );
    }]);

1 个答案:

答案 0 :(得分:0)

我试图在小提琴中复制这个问题,但不能这样做。所以我写了一个更简单的版本并进行了测试。它正在发挥作用。

    var MetronicApp = angular.module('myApp', []);
    MetronicApp.controller('compctrl', function($scope) { 

        alert("Controller called!");

        var tableData= [{'name':'Infosys Technologies','strength':1000}];

        var table=$('#sample_1').DataTable();

        tableData.forEach(function(item)
        {
            var row=[];  
            row.push(item.name); 
            row.push(item.strength);
            table.row.add(row).draw(false);
        });

   });