1

i have one problem in my angular.js code.I have a table which will filled up by database value. when initially my database has no value and table should display blank but in my case now the database is empty still the table is splitting into 5 rows which has no value.I am explaining my code below.

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

Here initially my database has no value.When user is refreshing(i.e-subject.html) the page the table present in the html content is splitting into 5 rows with no data but 5 serial number is coming which i don't need.check my php file below.

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

My requirement is table will be splitted only when database has some value.When data database is blank it should not which i am getting in my current code.Please help me.

1 Answer 1

1

Try this:

  1. Add script ng-show="showtable" to table tag which will become like this:

    <table ng-show="showtable" class="table table-bordered table-striped table-hover" id="dataTable">
    
  2. Then change your controller script like below:

    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");
        });
    })
    
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.