使用angular.js

时间:2015-09-28 12:51:56

标签: javascript html angularjs

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

course.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>

在上面的代码中,一个现有的表已经存在,它显示了来自数据库的所有数据。我的要求是当用户输入数据并单击提交按钮时,这些提交的数据将在这个表中添加那些现有数据。请检查我的控制器页面。

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)
            }
        })
        }
    }
});

请帮我解决此问题。

3 个答案:

答案 0 :(得分:0)

成功, 将userdata附加到

  $scope.courseData like $scope.courseData.push(userdata).

还有一个建议。不要像$scope.coursename那样为每个值设置null,而是写入像ng-model="course.coursename"这样的ng-model。因此,您需要初始化$scope.course = {};

在Ajax的成功中,只需编写$scope.course = {};。它会使你的表格变空。

答案 1 :(得分:0)

你应该加入

$scope.courseData //will automatically update the table;

  $scope.courseData.push(response);

我还建议您使用$resource$http

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

答案 2 :(得分:0)

如果您要作为多部分实体发送,请尝试

var fd = new FormData();
fd.append('coursename', userdata.coursename);

并在$ http.post请求中将fd作为数据发送