gpt4 book ai didi

javascript - 使用 angular.js 提交表单后添加数据

转载 作者:行者123 更新时间:2023-11-28 06:52:38 24 4
gpt4 key购买 nike

我需要在使用 angular.js 提交表单后在现有表中添加数据。我在下面解释我的代码。

类(class).html:

<form name="billdata" id="billdata" method="post" enctype="multipart/form-data">


<div id="SHOWDATA">
<div id="transactionsPortlet" class="panel-collapse collapse in">
<div class="portlet-body">
<div class="totalaligndiv">


<div class="col-md-6">


<div class="input-group bmargindiv1 col-md-12">
<span class="input-group-addon ndrftextwidth text-right" style="width:180px">Course Name :</span>
<input type="text" name="itemname" id="coursemname" class="form-control" placeholder="Add course name" ng-model="coursename" >
</div>


<div class="input-group bmargindiv1 col-md-12">
<span class="input-group-addon ndrftextwidth text-right" style="width:180px">Short name :</span>
<input type="text" name="itemname" id="shortmname" class="form-control" placeholder="Add short name" ng-model="course_short_name" >
</div>


</div>


<div class="col-md-6">


<div class="input-group bmargindiv1 col-md-12">
<span class="input-group-addon ndrftextwidth text-right" style="width:180px">Semester :</span>
<select id="coy" name="coy" class="form-control" ng-model="semester" >
<option value="">Select Semester</option>
<option value="IV">IV</option>
<option value="VI">VI</option>
<option value="VIII">VIII</option>
</select>
</div>

</div>


<div class="clearfix"></div>
<div style="text-align:center; padding-top:10px;">
<button class="btn btn-success" type="button" ng-click="addCourseData();">Submit</button>
</div>

<div class="clearfix"></div>

</div>
</div>
</div>
</div>

</form>
</div>
</div>
<!-- /.col-lg-12 -->

</div>
</div>





<div class="col-lg-12">
<div class="portlet portlet-blue" style="margin-bottom:12px;">
<div class="portlet-body">
<div class="table-responsive dashboard-demo-table">
<table class="table table-bordered table-striped table-hover" id="dataTable">
<colgroup>
<col class="col-md-1 col-sm-1">
<col class="col-md-2 col-sm-2">
<col class="col-md-2 col-sm-2">
<col class="col-md-2 col-sm-2">
<col class="col-md-2 col-sm-2">
<col class="col-md-2 col-sm-2">
<col class="col-md-1 col-sm-1">
</colgroup>
<thead>
<tr>
<th>Sl. No</th>
<th>Cource Name</th>
<th>Short Name</th>
<th>No of Semester</th>
<th>Edit</th>
</tr>
</thead>
<tbody id="detailsstockid">
<tr ng-repeat="course in courseData">
<td>{{course.course_id}}</td>
<td>{{course.course_name}}</td>
<td>{{course.short_name}}</td>
<td >{{course.semester}}</td>
<td>
<a href='#' >
<input type='button' class='btn btn-xs btn-green' value='Edit' >
</a>
</td>
</tr>
</tbody>
</table>
</div>
</div>

在上面的代码中,已经存在一个现有表,它显示数据库中的所有数据。这里我的要求是,当用户输入数据并单击提交按钮时,提交的数据将与现有数据一起添加到该表中。请检查我的 Controller 页面。

courseController.js:

var courseApp=angular.module('GofastoHome');
courseApp.controller('coursecontroller',function($scope){
$.ajax({
type:'GET',
url:"php/readCourseData.php",
success: function(data){
$scope.$apply(function(){
$scope.courseData=angular.fromJson(data);
});
}
})
$scope.addCourseData=function(){
if($scope.coursename==null){
alert('course name field could not blank');
}else if($scope.course_short_name==null){
alert('short name field could not blank');
}else if($scope.semester==null){
alert('semester field could not blank');
}else{
var userdata={'course_name':$scope.coursename,'course_short_name':$scope.course_short_name,'semester':$scope.semester};
console.log('userdata',userdata);
$.ajax({
type:'POST',
url:"php/addCourse.php",
data:userdata,
success: function(response){
$scope.$apply(function(){
alert(response);
$scope.coursename=null;
$scope.course_short_name=null;
$scope.semester=null;
});
},
error: function(result){
alert(result)
}
})
}
}
});

请帮我解决这个问题。

最佳答案

您应该添加到

$scope.courseData//会自动更新表格;

  $scope.courseData.push(response);

我还建议您使用$resource$http

https://docs.angularjs.org/api/ngResource/service/ $资源

关于javascript - 使用 angular.js 提交表单后添加数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32823482/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com