如何在使用Angular.js打开新行后添加删除按钮
How to add delete button after new row opened using Angular.js?
我需要在使用加号按钮添加新行后添加一个删除(减号)按钮。我在下面提供我的代码。
<table class="table table-bordered table-striped table-hover" id="dataTable" >
<thead>
<tr>
<th>Day</th>
<th>Category</th>
<th>Sub Subcategory</th>
<th>Comments Or special promotion</th>
<th>Add More</th>
</tr>
</thead>
<tbody id="detailsstockid">
<tr ng-repeat="d in days">
<td>{{d.day_name}}</td>
<td>
<table class="table table-bordered table-striped table-hover">
<tbody>
<tr ng-repeat="answerswer in d.answers">
<td>
<select class="form-control" id="answerswer_{{$index}}_category" name="answerswer_{{$index}}_category" ng-model="answerswer.catagory" ng-options="cat.name for cat in listOfCatagory track by cat.value">
<option value="">Select Category</option>
</select>
</td>
</tr>
</tbody>
</table>
</td>
<td>
<table class="table table-bordered table-striped table-hover">
<tbody>
<tr ng-repeat="answerswer in d.answers">
<td>
<select class="form-control" id="answerswer_{{$index}}_subcategory" name="answerswer_{{$index}}_subcategory" ng-model="answerswer.subcatagory" ng-options="sub.name for sub in listOfSubCatagory[$parent.$index] track by sub.value">
<option value="">Select Subcategory</option>
</select>
</td>
</tr>
</tbody>
</table>
</td>
<td>
<table class="table table-bordered table-striped table-hover">
<tbody>
<tr ng-repeat="answerswer in d.answers">
<td>
<input type="text" id="answerswer_{{$index}}_comment" name="answerswer_{{$index}}_comment" placeholder="Add Comment" ng-model="answerswers.comment" class="form-control oditek-form">
</td>
</tr>
</tbody>
</table>
</td>
<td>
<input type="submit" name="plus" id="plus" value="+" style="width:20px; text-align:center;" ng-click="addNewRow(d.answers)">
</td>
</tr>
</tbody>
</table>
控制器文件如下。
$scope.days=[];
$http({
method:'GET',
url:"php/customerInfo.php?action=day",
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
}).then(function successCallback(response){
//console.log('day',response.data);
angular.forEach(response.data,function(obj){
obj.answers = [];
$scope.addNewRow(obj.answers);
$scope.days.push(obj);
})
},function errorCallback(response) {
})
$scope.addNewRow = function(answers) {
answers.push({
category: null,
subcategory: null,
comment: null
});
};
在这里,当用户单击它时,我有一个plus
按钮,正在添加新行。我需要新行何时添加一个删除(minus
)按钮,为前一个按钮创建plus
并且该按钮将保留在新按钮中。当用户单击减号按钮时,相应的行将被删除。
在您的 HTML 文件中:
<button ng-click="removeRow($index)">
在角度控制器代码中:
$scope.removeRow = function(id) {
angular.forEach(answers, function(currentAnswer, index) {
if (currentAnswer.id === id) {
answers.splice(index, 1);
}
});
}
请注意,answers
需要可访问才能正常工作。
添加html for Remove 按钮旁边的加号按钮,如下所示:
<button type="button" ng-click="removeNewRow($index)" ng-show="$first && !$last">
并在控制器中添加一个函数:
$scope.removeNewRow= function(index) {
$scope.d.answers.splice(index, 1);
}
相关文章:
- 克隆字段上的“添加-删除”按钮
- 将新行添加到localStorage变量中
- 当上一行/当前行被填充时,将新行添加到HTML表中
- 当新行添加到表中时,使用AJAX更新页面
- 使用 jquery 在函数中添加删除按钮
- 如何在使用Angular.js打开新行后添加删除按钮
- 将新行添加到数据库时的 Javascript 警报
- jquery JavaScript 添加/删除按钮
- 为 HTML 文本区域中的新行添加效果
- 使用KnockoutJS在添加/删除按钮之间切换
- 将新行添加到空的剑道网格中
- 单击按钮时,将新行添加到包含下拉列表的表中
- 应用程序有错误.意外的标记."尝试添加删除按钮
- 如何从表中的每个新行中删除类
- 向输出空间或新行添加新行或空格
- jQuery,新行和输入按钮
- 为每个图像缩略图预览添加删除按钮
- 如何在jQuery的clone()方法中为新添加的表行添加删除按钮
- 如何在每行添加删除按钮
- 为按钮创建的每个新行添加i++