0

I have a list of dashboards(vm.dashboards) out of which I want to filter the dashboards created by me. By default it displays the entire list but when i click on the "show my dashboard" checkbox , it should display only the list of dashbaords created by me.

HTML code:

<div>
          <input type="checkbox" ng-model="showMyDashboard" ng-value="vm.myDashboard" ng-checked="false" ng-true-value="1" ng-false-value="0"><span for="radio" style="margin-left:5px;" class="text-only">Show my dashboards</span>
        </div>

<ul ng-repeat="dashboard in vm.dashboards" class="list-group">
          <li class="list-group-item">
            <div class="row">
              <div class="col-sm-5"><a class="list-group-item-heading dashboard-heading">{{ dashboard.title }} </a>
                <p class="list-group-item-text dashboard-text">{{ dashboard.desc }}</p>
              </div></li>
    </ul>

Now the tricky part is that there is no direct way to get myDashboards list. If(currentUserId === dashboardOwnerId) then that particular dashboard is myDashboard. So I have to apply the condition in the filter. I tried doing this approach:

ul.list-group(ng-repeat="dashboard in vm.dashboards | filter: dashboard.creator["@href"].split("/").pop() === vm.currentUSerID")

So, dashboard.creator["@href"].split("/").pop() will give values like 1, 101, 123. and currentUSerID will give similar values too. When both matches thats when i get the dashboard created by me.

This filter above does totally opposite to what i want and lists all dashboards that are not created by me. Can i give such condition to filters ? do I need to build custom filter for it? Any help/suggestions would be appreciated. Thanks!

1
  • Please check the below link. Link Commented Mar 23, 2017 at 16:44

1 Answer 1

0

The cleanest way to do was to create a custom filter and return the dashboard with correct values.

.filter("myDashboardFitler", function() {
            return function(input, currentUserId, checkedValue) {
                var owner = [];
                var i;

                angular.forEach(input, function(dashboard) {
                    if (checkedValue === "YES") {
                        if (dashboard.creator["@href"].split("/").pop() === currentUserId) {
                            owner.push(dashboard);
                        }
                    } else {
                        owner.push(dashboard);
                    }
                });
                return owner;
            };
        });

HTML page:

<input type="checkbox" ng-model="checkedValue" ng-true-value="'YES'" ng-false-value="'NO'" ng-change="myDashboardFilter()"><span for="radio" style="margin-left:5px;" class="text-only">Show only my dashboards</span>
<ul ng-repeat="dashboard in vm.dashboards | myDashboardFitler : vm.currentUserId : checkedValue" class="list-group">

I hope this helps someone facing similar issue. Thanks!

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.