Angular directive incorporating a reset feature

Need help with a directive I created called filterButton. It allows users to select filter options and adds a check mark on the right. However, I am struggling with resetting all filter fields back to default when the reset button is clicked on filter.html. I have tried multiple methods without success. Any suggestions would be greatly appreciated. Thank you!

app.js

app.directive('filterButton', function () {
return {
        restrict: 'EA',
        templateUrl: 'template/filter-button.html',
        replace: true,
        scope: {
                list: '=', //two-way binding
                selected: '=', //two-way binding
                field: '@', //one-way binding
        },
        link: function ($scope) {
            $scope.selected = [];
            $scope.setSelectedOption = function() {
                var id = this.option.id;

                if(_.contains($scope.selected, id)) {
                    $scope.selected = _.without($scope.selected, id)
                } else {
                    $scope.selected.push(id);
                }
                return false;
            };
            $scope.isChecked = function(id) {
                if(_.contains($scope.selected, id)) {
                    return "fa fa-check pull-right-selected";
                }
                return "";
            };
        }
    }
});

filter-button.html

<div class="row filter-item">
<div class="btn-group" dropdown is-open="{open: open}">
    <button type="button" class="btn btn-primary dropdown-toggle" dropdown-toggle ng-disabled="disabled">Fiter By {{field}} <span class="caret"></span></button>
    <ul class="dropdown-menu" aria-labelledby="dropdownMenu">
        <li data-ng-repeat="option in list" class="dropdown-btn"> <a data-ng-click="setSelectedOption()">{{option.name}}<span data-ng-class="isChecked(option.id)"></span></a></li>
    </ul>
</div>

filter.html

<filter-button list="statusList" selected="selectedStatus" field="Status" popover="Error Customer? or No Error Customer?" popover-trigger="mouseenter"></filter-button>
<filter-button list="systemList" selected="selectedSystemType" field="System Type" popover="N+1 or Stand alone?" popover-trigger="mouseenter"></filter-button>
<filter-button list="customerFilterList" selected="selectedCustomer" field="Customer" popover="Male or Female?" popover-trigger="mouseenter"></filter-button>
<div class="row filter-item">
    <div class="filter-label">
        <button class="btn btn-primary">Reset</button>
    </div>
</div>

Answer №1

If you want to reset the selected arrays when the reset button is clicked, you can do so with the following code:

<button class="btn btn-primary" ng-click="reset()">Reset</button>

Inside your controller:

$scope.reset = function() {
    $scope.selectedStatus = [];
    $scope.selectedSystemType = [];
    $scope.selectedCustomer = [];
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Generate a random number not found within the given array

Hopefully, I can explain this clearly. Imagine I have 8 boxes with the class .boxA, each containing a numeric value generated from JavaScript: <div class="tfooter"> <div class="boxA" id="bx3" value="3">3</div> <div class="box ...

Is there a YUI Custom Event available for pre-selecting the value in a Dropdown

Imagine a scenario where I have a dropdown field that automatically selects a value as soon as it is rendered (for example, a Country field in a signup form). This dropdown interacts with other components, causing the selected value to change dynamically. ...

Installing npm modules can be a time-consuming task when running docker-compose

I have been working on a project using Next.js and a PostgreSQL database in a Docker container. The setup of my project involves Docker Compose. However, I've noticed that when I run docker-compose up, the npm install command takes an incredibly long ...

Angularjs cascading dropdownlist for dynamic data

Can someone help me troubleshoot my second dropdownlist? The first one is working fine but the second one doesn't seem to be functioning correctly. Here is the HTML code: <select class="form-control" ng-options="option as option.label for opt ...

When a directive is within an ng-repeat loop, binding an event to it does not function properly

Why is everyone asleep on the AngularJS Google Group? :) Here's the issue I'm facing: I have a select element inside a directive, and I want to attach a function to the 'change' event of that select. However, when I use this directive ...

Enhancing data rendering by incorporating extra verifications through the logical AND operator to prevent crashes upon page refresh

Upon refreshing the page, my app crashed. The issue stemmed from the page loading faster than my data, prompting me to include additional checks using the logical AND operator. While effective in preventing crashes, this approach seems laborious and begs t ...

Choose a unique text color

Having trouble with my HTML select form and modifying text color for specific options. In the provided example, I changed the color for the Tuesday option, but it only shows up when scrolling through the options. How can I make the color change visible for ...

Three Divs stacked on top of each other, with varying heights based on

I am seeking a solution for creating a layout with three vertically stacked divs. The top div must have a fixed height of 60px. The middle div might contain content exceeding its height, therefore we've set it to overflow: auto. But regardless of th ...

What is the best way to incorporate an optional URL within Angular routes using ui-router?

Is it possible to define state with an optional URL in Angular UI-Router? If so, how can I achieve this functionality? For instance: state('test',{ url:'/list/:id/color?/:color?' } In the given example, both "color" and ":color" ...

How do I stop my sliding content from continuing after the first pass?

Welcome to my blog! If you take a moment to observe the "Audio" tile, you'll notice it moving. Suddenly, another tile labeled "cat" appears and slides in from the right, pushing "Audio" to the left. But here's where things get interesting - after ...

Instructions on how to use WebClient in C# to download an image and retrieve it from an ASPX page using JavaScript

I have been searching for solutions to this question everywhere, but none of them seem to work for me. The goal is to download an image from the internet using an aspx page. I want to call the aspx page from JavaScript, retrieve the data, and display it i ...

The implementation of multiple nested named views in ui-router

I have a nested view structure that looks like this <div ui-view="master" id="ng-view"> <div class="container-fluid height-full"> <div ui-view="globalNavigationPanel" class="row"></div> <div ui-view="detail" id="d ...

Strategies for capturing POST data sent to a JavaScript webpage

The request is sent from an external source beyond my control xyz.php <form action='https_:_//xyz.com/javascriptFile' method='POST'> <input type='text' name='data' /> </form> to: (My custom f ...

Extending EJS partials using jQuery

I am currently exploring the integration of JQuery to dynamically add an EJS partial. My goal is to enable infinite scrolling within a table - I have opted for EJS to render the rows of the table as partials and leverage more EJS to exhibit the variables p ...

Load additional slides in bxslider, monitor when it has reached the last one

I am using a horizontal carousel slider with bxslider, and I have disabled the infiniteLoop feature to prevent the slider from looping. My goal is to fetch additional slides via an AJAX request and add them to the slider once it reaches the end of the cu ...

Convert base64 to PDF for instant download

I am experiencing difficulties with downloading PDF files in my SAPUI5 application. The issue lies in receiving a base64 string from the backend system, but struggling to convert it and display it as a PDF. I can successfully convert and download smaller ...

JS - what could be causing this to not work properly? Could there be a bug that

Can anyone help me troubleshoot this code? It is supposed to add a search parameter page=value to the URL and load the page, but it's not working. I'm not sure if there's a typo in there. $(function(){ $('.page').on('cl ...

Attempting to incorporate my Resolve into the controller so that it can then be injected into the HTML

Here is the content of my index.js file: 'use strict'; angular.module('wildenglish', ['ngAnimate', 'ngTouch', 'restangular', 'ngRoute', 'ui.bootstrap']) .config(function ($routeP ...

Adding a three-dimensional perspective to an HTML5 canvas without utilizing CSS3 or WebGL

Here is a canvas I am working with: http://jsbin.com/soserubafe Here is the JavaScript code associated with it: var canvas=document.getElementById("canvas"); var ctx=canvas.getContext("2d"); var w = canvas.width; var h = canvas.height; var cw=canvas. ...

Performance comparison between Ember and Angular.JS in rendering a large table

I am planning to construct a substantial table containing a plethora of data (approximately 2000 elements <td>). I intend to include functions for calculating values based on model, but without incorporating any bindings. Primarily, my goal is to s ...