Get $ index after filter

I have a small image gallery that has a search box, and when the user clicks on an image, it will open a lightbox with the same image.

Basically I am passing the $ index function that opens the item in $ scope.list [lb.index] .

my code:

HTML
<input type="text" ng-model="query.name" />
<ul class="list" ng-show="list.length>0">
    <li ng-repeat="item in list | filter:query">
        <a class="img" ng-style="{'background-image':'url(/uploads/<%item.image%>)'}" ng-click="openLB($index)"></a>
    </li>
</ul>
<div class="lightbox" ng-if="lb.show">
    <a class="prev" ng-show="list.length>1" ng-click="changeItemLB(lb.index, 'prev')"></a>
    <a class="next" ng-show="list.length>1" ng-click="changeItemLB(lb.index, 'next')"></a>
    <div class="holder">
        <img ng-if="list[lb.index].image.length" ng-src="/uploads/<%list[lb.index].image%>" />
    </div>
</div>

Angular
$scope.openLB = function(index) {

    $scope.lb.show = true;
    $scope.lb.index = index;

};
$scope.changeItemLB = function(index, action) {

    var tot = $scope.list.length-1,
        goto = 0;

    if(action=='prev') goto = index==0 ? tot : index-1; 
    if(action=='next') goto = index==tot ? 0 : index+1; 

    $scope.openLB(goto);
}

      

The problem is that the user is filtering the results (search input), the click still maintains an index from the list without a filter that makes the lightbox open the wrong image. Does anyone know how to solve this?

thank

+3


source to share


1 answer


pass object instead of index

Let's say you have 5 items in your list

showing two results.

If clicked, the $ index value will be your current view index

but your list still has 5 items.

try it

<a class="img" ng-style="{'background-image':'url(/uploads/<%item.image%>)'}" ng-click="openLB(item)"></a>

      

controller

$scope.openLB = function(item) {

    var index = $scope.list.indexOf(item);
    $scope.lb.show = true;
    $scope.lb.index = index;

};

      



EDIT

How to get Filtered Result

try it

view

<li ng-repeat="item in filtered= (list | filter:query)">

      

controller

$scope.filtered=[];

      

Now you can get the filtered list from $scope.filtered

+5


source







All Articles