Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep matched row on top of other rows in table using angularjs?

I'm using Smart table i want to keep matched rows on top of other rows in table using angularjs(1), in my case i have been matching string from table column, on the basis of matched string I'm changing row background color, but i want to show those colored rows on top of the other rows, so that I'm able see matched rows instantly.

it is worked if i sort like below, but it affect only current page, i want display all matched rows to top of other rows irrespective pagination and all.

<tr ng-repeat="emp in employees | orderBy:set_color" ng-style="set_color(emp)">

how i can achieve this. if you want more info please take a look at this link

var myApp = angular.module('myApp', [])
  
      .controller('employeeController', function ($scope) {
     
   var employees = [{
    "Name": "Alfreds Futterkiste",
    "City": "Berlin",
    "Country": "Germany"
  }, {
    "Name": "Berglunds snabbköp",
    "City": "Luleå",
    "Country": "Sweden"
  }, {
    "Name": "Blauer See Delikatessen",
    "City": "Mannheim",
    "Country": "Germany"
  }, {
    "Name": "Blondel père et fils",
    "City": "Strasbourg",
    "Country": "France"
  }, {
    "Name": "Bólido Comidas preparadas",
    "City": "Madrid",
    "Country": "Spain"
  }, {
    "Name": "Bon app'",
    "City": "Marseille",
    "Country": "France"
  }, {
    "Name": "Bottom-Dollar Marketse",
    "City": "Tsawassen",
    "Country": "Canada"
  }, {
    "Name": "Cactus Comidas para llevar",
    "City": "Buenos Aires",
    "Country": "Argentina"
  }, {
    "Name": "Centro comercial Moctezuma",
    "City": "México D.F.",
    "Country": "Mexico"
  }, {
    "Name": "Chop-suey Chinese",
    "City": "Bern",
    "Country": "Switzerland"
  }, {
    "Name": "Comércio Mineiro",
    "City": "São Paulo",
    "Country": "Brazil"
  }];
      $scope.employees=employees;
      
      $scope.set_color = function (emp) {
                    var inputString = emp.Country;
                    for (i = 0; i < inputString.length; i++) {
                        var findme = "France";
                        if (inputString.indexOf(findme) > -1) {
                            return { 'background-color': '#FFCCCB' }
                        }
                    }
                }
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.min.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

<body ng-app="myApp">
    <div ng-controller="employeeController">
        <div class="container" style="margin-top:40px;">
            <div class="row">
                {{error}}
                <div class="col-md-6">
                    <table class="table table-bordered table-condensed">
                        <thead>
                            <tr>
                                <th>Name</th>
                                <th>City</th>
                                <th>Country</th>
                            </tr>
                        </thead>
                        <tbody >
                            <tr ng-repeat="emp in employees" ng-style="set_color(emp)">
                                <td>{{emp.Name}}</td>
                                <td>{{emp.City}}</td>
                                <td>{{emp.Country}}</td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</body>

Thank you

like image 892
PK-1825 Avatar asked Jan 30 '18 17:01

PK-1825


1 Answers

With AngularJS i believe I would choose this option:

HTML

orderBy can take multiple filters

 <tr ng-repeat="emp in employees | orderBy: [sort, 'Name']" 
     ng-class="{'matched': emp.matched }">

JS

var findme = "France";
$scope.sort = (emp) => {
   emp.matched = emp.Country.indexOf(findme) > -1; 
   return !emp.matched; 
}

See working fiddle:

https://jsfiddle.net/kraaness/vft90229/

like image 109
Kim Raaness Avatar answered Sep 27 '22 02:09

Kim Raaness