我在angular.js代码中有一个问题。我有一个表,该表将由数据库值填充。当最初我的数据库没有值并且表应该显示为空白时,但是在我的情况下现在数据库为空,但表仍被拆分为5行而没有值。我在下面解释我的代码。
subject.html:
<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>
<select style="width:100%; border:none; font-weight:bold;">
<option>Course Name</option>
</select>
</th>
<th>Semester</th>
<th>Subject Name</th>
<th>Short Name</th>
<th>Edit</th>
</tr>
</thead>
<tbody id="detailsstockid">
<tr ng-repeat="sub in subjectData" >
<td>{{ $index+1 }}</td>
<td>{{sub.course_name}}</td>
<td>{{sub.semester}}</td>
<td>{{sub.subject_name}}</td>
<td >{{sub.short_name}}</td>
<td>
<a href=''>
<input type='button' class='btn btn-xs btn-green' value='Edit'>
</a>
</td>
</tr>
</tbody>
</table>
subjectController.js:
var subApp=angular.module('GofastoHome');
subApp.controller('subjectcontroller',function($scope,$http){
$http({
method: 'GET',
url: "php/subject/readSubjectData.php",
}).then(function successCallback(response) {
console.log('response',response);
$scope.subjectData=angular.fromJson(response);
},function errorCallback(response) {
alert("could not fetch data");
});
})
这里最初我的数据库没有值。当用户正在刷新(i.e-subject.html
)页面时,html内容中的表被拆分为5行,没有数据,但是5个序列号即将到来,请检查下面的php文件。
readSubjectData.php:
$connect = mysqli_connect("localhost", "root", "******", "go_fasto");
$result = mysqli_query($connect, "select * from db_subject order by subject_id desc ");
$data = array();
while ($row = mysqli_fetch_assoc($result)) {
$data[] = $row;
}
print json_encode($data);
我的要求是仅在数据库具有某些值时才对表进行拆分。当数据数据库为空时,我不应该在当前代码中获取它。请帮助我。
尝试这个:
将脚本添加ng-show="showtable"
到表标记中,它将变为:
<table ng-show="showtable" class="table table-bordered table-striped table-hover" id="dataTable">
然后像下面那样更改您的控制器脚本:
var subApp=angular.module('GofastoHome');
subApp.controller('subjectcontroller',function($scope,$http){
$http({
method: 'GET',
url: "php/subject/readSubjectData.php",
}).then(function successCallback(response) {
console.log('response',response);
$scope.subjectData=angular.fromJson(response);
if($scope.subjectData.length > 0) //
$scope.showtable = true; // add these lines
else //
$scope.showtable = false; //
},function errorCallback(response) {
alert("could not fetch data");
});
})
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句