angularjs - 当我们点击angularjs中的提交按钮时如何动态获取额外的行
问题描述
这是我的 html 代码,我有两个字段,如类别和多选列。
<!Doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.4.1.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
crossorigin="anonymous"></script>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<link rel="stylesheet" href="style.css">
</head>
<body>
<main ng-app = "myApp">
<div class="container-fluid" ng-controller = "roomCategoryController">
<div class="container mt-4">
<h3>Room Category</h3>
<div class="col-12 d-flex">
<div class="col-4">
<p>Room Category</p>
</div>
<div class="col-8">
<div class="input-group mb-3">
<input type="text" class="form-control" ng-model = "category">
</div>
</div>
</div>
<div class="col-12 d-flex">
<div class="col-4">
<p>Room Amenities</p>
</div>
<div class="col-8">
<div class="form-group">
<select id="dates-field2" class="multiselect-ui form-control" multiple="multiple" ng-model = "amenities">
<option value="cheese">Cheese</option>
<option value="tomatoes">Tomatoes</option>
<option value="mozarella">Mozzarella</option>
<option value="mushrooms">Mushrooms</option>
<option value="pepperoni">Pepperoni</option>
<option value="onions">Onions</option>
</select>
</div>
</div>
</div>
<div class='col-12'>
<div class='col-md-12 text-center'>
<button type="submit" class="btn btn-primary" ng-click="submit()">Submit</button>
</div>
</div>
<div class="col-12">
<table class="table mt-5 table-striped table-dark">
<thead>
<tr>
<th>S.No</th>
<th>Room Category</th>
<th>Room Amenities</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="x in formdata">
<td>{{$index+1}}</td>
<td>{{x.category}}</td>
<td>{{x.amenities}}</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</main>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.8/angular.min.js"></script>
<script src="roomCategoryController.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.13/js/bootstrap-multiselect.min.js"></script>
<!-- <script src="https://cdnjs.cloudflare.com/ajax/libs/multi-select/0.9.12/js/jquery.multi-select.js"></script> -->
<script src="multidropdown.js"></script>
<script type="text/javascript">
$(function() {
$('.multiselect-ui').multiselect({
includeSelectAllOption: true
});
});
</script>
</body>
</html>
当我提交表单时,输入字段的数据需要进入表格。
当我多次提交表单时,只有一行中的数据会使用最新的表单值更新表,但不会添加新行
app.controller("mycontroller", function($scope){
$scope.formdata=[];
$scope.mydata={};
mydata.myname=$scope.name
mydata.mynumber=$scope.number
$scope.formdata.push(mydata);
$scope.name ='';
$scope.number='';
})
解决方案
Anusha,您的代码中有很多拼写错误。此外,您需要在控制器中访问mydata
with 。$scope
在模板上,您可以通过 访问它mydata
。
请尝试以下代码。
app.controller('roomCategoryController', function($scope) {
$scope.formdata = [];
$scope.category ='';
$scope.amenities ='';
$scope.submit = function(){
$scope.mydata = {};
$scope.mydata.category = $scope.category;
$scope.mydata.amennity = $scope.amenities.toString();
if($scope.mydata.category){
$scope.formdata.push($scope.mydata);
$scope.category ='';
}
}
});
您可以在此处查看代码的工作示例 -示例
推荐阅读
- sql - 如何拥有单个日期字段或更有效地编写它
- c# - Npgsql C#多行/值插入
- scala - 在 spark scala 中查找年月的最小值和最大值
- docker - 詹金斯在第二次构建时没有向我显示初始管理员密码
- amazon-web-services - 将数据从 Amazon Redshift 迁移到 Aurora 数据库
- flutter - 扩展时更改 ExpansionTile 小部件中标题文本的颜色
- c++ - 线程上下文的静态存储对象优化
- react-native - EXPO - OTA 仍在工作,即使 update.enable=false
- python - Python:在生成的进程之间共享时间锁定,以便它们之间存在延迟
- python-3.x - Flask 容器未启动并使用 docker 运行