Search code examples
javascripthtmlangularjssmart-table

Smart table st-search won't work


I've implemented a smart table in my page through ng-include and the st-search doesn't work. Here's my code :

Table

 <div class="horizontal-scroll" ng-controller="SmartTableController">
  <div class="form-group select-page-size-wrap ">
    <label>Rows on page
      <select class="form-control selectpicker show-tick" title="Rows on page" selectpicker
              ng-model="smartTablePageSize" ng-options="i for i in [5,10,15,20,25]">
      </select>
    </label>
  </div>
  <table class="table" st-table="displayedData" st-safe-src="smartTableData" >
    <thead>
    <tr class="sortable ">
      <th class="table-id" st-sort="id" st-sort-default="true">#</th>
      <th st-sort="firstname">Prénom</th>
      <th st-sort="lastname">Nom</th>
      <th st-sort="role">Rôle</th>
      <th st-sort="email">Email</th>
      <th st-sort="chargeRate">Taux de charge</th>
    </tr>
    <tr>
      <th></th>
      <th><input st-search="firstname" placeholder="Chercher Prénom" class="input-sm form-control search-input"
                 type="search"/></th>
      <th><input st-search="lastname" placeholder="Chercher Nom" class="input-sm form-control search-input"
                 type="search"/></th>
      <th><input st-search="role" placeholder="Chercher Rôle" class="input-sm form-control search-input"
                 type="search"/></th>
      <th><input st-search="email" placeholder="Chercher Email" class="input-sm form-control search-input" type="search"/>
      </th>
      <th><input st-search="chargeRate" placeholder="Chercher Taux de charge" class="input-sm form-control search-input" type="search"/>
      </th>
    </tr>
    </thead>
    <tbody>
    <tr ng-repeat="item in displayedData">
      <td class="table-id">{{item.id}}</td>
      <td>{{item.firstname}}</td>
      <td>{{item.lastname}}</td>
      <td>{{item.role[0].name}}</td>
      <td><a class="email-link" ng-href="mailto:{{item.email}}">{{item.email}}</a></td>
      <td>{{item.chargeRate}}</td>
    </tr>
    </tbody>
    <tfoot>
    <tr>
      <td colspan="6" class="text-center">
        <div st-pagination="" st-items-by-page="smartTablePageSize" st-displayed-pages="5"></div>
      </td>
    </tr>
    </tfoot>
  </table>
</div>

and then I'm getting the data from rest application and here's my controller :

angular.module('BlurAdmin.pages.users')
.controller('SmartTableController',SmartTableController);

function SmartTableController ($log, $scope, smartTableFactory) {

$scope.smartTableData = [];
$scope.selectedUsers = {};
$scope.smartTablePageSize = 10;


/**
 * Get Smart Table data
 */
$scope.getSmartTableData = function () {
    smartTableFactory.getSmartTableData()
            .success(function (data) {
                $scope.smartTableData = data;
                $scope.displayedData = angular.copy($scope.smartTableData);
                console.log(" Data : " , data);
//Edit 
    $scope.displayedData = $scope.smartTableData;
            })
            .error(function (data, status) {

                $scope.addMessage = "Erreur data : " + data + ' ' + status;
                $log.log(data.error + '' + status);
            });
    };

$scope.getSmartTableData();

}

My service works fine and I'm getting data plus it does show on the table, I just can't search.

Any help will be appreciated. Thanks


Solution

  • I have tried. Conclusionly Your code may happens $http error not smart table one. JSFiddle
    I could filter all columns except 'role' column when I used these JSON data.

       $scope.smartTableData =
            [
            { firstname: 1, lastname: 'aaaa', role: [{ name: 'employee1' }], chargeRate: 10, email: 'aaaa@' },
            { firstname: 2, lastname: 'bbbb', role: [{ name: 'employee2' }], chargeRate: 20, email: 'bbbb@' },
            { firstname: 3, lastname: 'cccc', role: [{ name: 'employee3' }], chargeRate: 30, email: 'cccc@' },
            { firstname: 4, lastname: 'dddd', role: [{ name: 'employee4' }], chargeRate: 40, email: 'dddd@' }
            ];
    

    Please confirm whether your data is JSON format correctly or not.

    In 'role' column, you should code like this.
    OK <input st-search="role.name"
    NG <input st-search="role"

    And I think you don't have to code this part because it's unnecessary.

       $scope.displayedData = angular.copy($scope.smartTableData);
       $scope.displayedData = $scope.smartTableData;