javascript - 动态创建表和行

标签 javascript php jquery angularjs

如何动态创建表,然后向表中添加行?我已经能够创建包含一行和表单字段的表,但如何添加其他行?任何有关如何实现的示例都会有所帮助。我在 Angular 2中做了。 screenshot

$scope.newItem = function($event, valI) {
  $scope['questionelemnt'].push({   
    id: counter, 
    title: '',
    desc: '',
    Qty: '', 
    price: '',
    total: ''
  }); 
}

有关 Angular 或 jquery 的任何帮助。

最佳答案

Directive ng-app & ng-controller code
HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
   <html ng-app="helloApp">
   <head>
   <title>Hello AngularJS - Add Table Row Dynamically</title>
   <link rel="stylesheet"   href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
   <script
    src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">  </script>
   <script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.17/angular.min.js"></script>
<script src="assets/js/controllers.js"></script>
</head>
<!-- Controller name goes here -->
<body ng-controller="CompanyCtrl">
...
</body>
</html>

Controller CompanyCtrl code in controller.js            
<script>
var helloApp = angular.module("helloApp", []);
helloApp.controller("CompanyCtrl", function($scope) {
$scope.questions = [];
$scope.addRow = function(){     
    $scope.questions.push({ 'title':$scope.title, 'desc': $scope.desc,  'Qty':$scope.Qty,'price':$scope.price,'total':$scope.total });
    $scope.title='';
    $scope.desc='';
    $scope.Qty='';
    $scope.price='';
    $scope.total='';
};
)};
</script>
 Directive ng-repeat code
 <table class="table">
<tr>
    <th>title
    </th>
    <th>desc
    </th>
    <th> Qty
    </th>
    <th>price
    </th>
   <th>total
    </th>
</tr>
<tr ng-repeat="question in questions">
    <td>{ {question.title}}
    </td>
    <td>{ {question.desc}}
    </td>
    <td>{ {question.Qty}}
    </td>
    <td>{ {question.price}}
    </td>
     <td>{ {question.total}}
    </td>
</tr>
</table>

**Directive ng-submit code**

<form class="form-horizontal" role="form" ng-submit="addRow()">
<div class="form-group">
    <label class="col-md-2 control-label">title</label>
    <div class="col-md-4">
        <input type="text" class="form-control" name="title"
            ng-model="title" />
    </div>
</div>
<div class="form-group">
    <label class="col-md-2 control-label">desc</label>
    <div class="col-md-4">
        <input type="text" class="form-control" name="desc"
            ng-model="desc" />
    </div>
</div>
<div class="form-group">
    <label class="col-md-2 control-label">Qty</label>
    <div class="col-md-4">
        <input type="text" class="form-control" name="Qty"
            ng-model="Qty" />
    </div>
</div>
<div class="form-group">
    <label class="col-md-2 control-label">price</label>
    <div class="col-md-4">
        <input type="text" class="form-control" name="price"
            ng-model="price" />
    </div>
</div>
<div class="form-group">
    <label class="col-md-2 control-label">total</label>
    <div class="col-md-4">
        <input type="text" class="form-control" name="total"
            ng-model="total" />
    </div>
</div>
<div class="form-group">                                
    <div style="padding-left:110px">
        <input type="submit" value="Submit" class="btn btn-primary"/>
    </div>
</div>

祝您编码愉快,希望对您有所帮助

关于javascript - 动态创建表和行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43276089/

相关文章:

JavaScript 电子邮件验证表单不起作用

javascript - 如果 div 打开则关闭,如果关闭则打开

javascript - ThreeJS DOMException : Failed to execute 'texImage2D' on 'WebGLRenderingContext'

javascript - 使用 mySQL 数组对象作为 int

jquery - 使用 jquery 在父 div 下面添加 div

javascript - 检查过渡是否将应用于元素

php - echo $_SESSION 中的非法字符串偏移量

php mysql 在连接表上搜索部分匹配的数据

jquery - 设置溢出时,Bootstrap datetimepicker 不可见

javascript - 向下滚动时隐藏 div