Is there a way to switch view or model by clicking a button?

In a simple demo I created, there is a button labeled "openpopup". When the button is clicked, a pop-up screen appears allowing the user to select multiple elements. Initially, the first and second elements are selected by default. Upon running the application, you will notice that there are two columns displayed because the first two options are selected in the multiple selection box. When the pop-up is opened and the third option is selected, a third column is automatically created in the view. My requirement is for this to only happen when the user clicks the "select" button on the pop-up screen. In other words, any selection or deselection of checkboxes should only be reflected in the model and displayed in the view after the "select" button is pressed.

Here is my code:


<button ng-click="openPopover($event)">
      openpopup
</button>
<script id="my-column-name.html" type="text/ng-template">
<ion-popover-view>
<ion-header-bar>
<h1 class="title">My Popover Title</h1>
</ion-header-bar>
<ion-content>
<ul class="list" ng-repeat="item in data">

<li class="item item-checkbox">
<label class="checkbox">
<input type="checkbox" ng-model="item.checked" ng-click="itemCheckedUnCheckedhandler(item)">
</label>
{{item.label}}
</li>
</ul>
<button ng-click="closePopover()">select</button>
</ion-content>

</ion-popover-view>
</script>

Answer №1

In my opinion, the most efficient approach would be to link the selected items to a central variable (checkedItems in this instance), which does not immediately impact the display

<input type="checkbox" ng-model="checkedItems[$index]" ng-click="checkboxClicked($index)">

The "checkboxClicked" function simply updates the checked status of the current item

  $scope.checkcoxClicked = function(n){
    $scope.checkedItems[n] = !$scope.checkedItems[n];
  };

Upon closing the popup (remember, the function is only triggered when the "select" button is clicked, not if you click outside the popup), we update the data variable linked to the view with the new check/uncheck statuses

$scope.closePopover = function() {
   for (var i = 0; i < $scope.data.length; i++){
        $scope.data[i].checked = $scope.checkedItems[i];
   }
   $scope.popover.hide();
};

You can see a demonstration on this plunker.

Answer №2

Make a slight adjustment to your code snippet: like:


//***for html template
<input type="checkbox" ng-model="item.tempChecked" ng-click="itemCheckedUnCheckedhandler(item)">

//*for Controller
//****Include a new field("tempChecked") in your data object*

$scope.data = [{
      "label": "Invoice#",
      "fieldNameOrPath": "Name",
      "checked": true,
      'tempChecked': true
    }, {
      "label": "Account Name",
      "fieldNameOrPath": "akritiv__Account__r.Name",
      "checked": true,
      'tempChecked': true
    }, {
      "label": "Type",
      "fieldNameOrPath": "Type__c",
      "tempChecked": false,
      'checked': false
    }, {
      "label": "SO#",
      "fieldNameOrPath": "akritiv__So_Number__c",
      'checked': false,
      "tempChecked": false
    }]

//****"make some adjustments to your select button"***

$scope.closePopover = function() {
      for (var d in $scope.data) {
        if ($scope.data[d].tempChecked == true) {
          $scope.data[d].checked = true;
        } else {
          $scope.data[d].checked = false;
        }
      }
      $scope.popover.hide();
    };

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

Searching for data in Node.js using Mongoose and dates

I'm in search of a way to execute a specific query with mongoose. In my mongodb database, I have data structured like this: "startDateTime" : ISODate("2017-03-22T00:00:00.000Z"), "endDateTime" : ISODate("2017-03-27T00:00:00.000Z"), My goal is to r ...

Guide to displaying a loading message while performing a synchronous AJAX request in a web browser

Is there a way to display a waiting message during a synchronous AJAX call in the browser? I attempted to use the code below, but even after turning off the web server, the "Saving" message did not appear. After some time, only an error event from the AJA ...

State calculation occurs too tardily

I'm working on creating a simple like button that changes color based on whether it's liked or not. I've tried to calculate this beforehand using React.useEffect, but it seems to be calculating afterwards instead... The hearts are initially ...

Mobile devices do not support internal link scrolling in Material UI

Currently, I'm utilizing internal links like /lessons/lesson-slug#heading-slug for smooth scrolling within a page. While everything functions perfectly on desktop, it ceases to work on mobile view due to an overlaid drawer nav component. Take a look a ...

Positioning tooltip arrows in Highcharts

I'm attempting to modify the Highcharts tooltip for a stacked column chart in order to have the arrow on the tooltip point to the center of the bar. I understand that I can utilize the positioner callback to adjust the tooltip's position, but it ...

Issue with text input field causing the Enter key to not create a new line

In the example above, the text is placed in the middle of the text area. Here is the CSS code : .form-control { height: 300px; display: block; width: 100%; padding: 0.375rem 0.75rem; font-size: 1rem; font-weight: 400; line-heig ...

Tips on utilizing useStyle with ReactJS Material UI?

Is there a way to utilize a custom CSS file in the useStyle function of Material UI? I have created a separate useStyle file and would like to incorporate its styles. Can someone explain how this can be accomplished? input[type="checkbox"], inp ...

Following the addition of a row that includes select and input fields, it appears that the functionality of the select and input

After appending a div.row using dropdown selection within another div and making it sortable using the Kendo Sortable method in jQuery, the elements are loading correctly. However, the select options are not displaying, and the input field is not accepting ...

Execute a series of repetitive HTTP GET requests and remain patient for all of them to complete

I am working with a REST service that provides a list of 'Json' objects, where each object may contain a link to another resource of the same class. To fetch all these resources recursively starting from a specific one, I have written the followi ...

Last month's display is currently unidentified

function calculateTime() { var Make_It_12_Hour = true; var currentTime = new Date(); var hour1 = currentTime.getHours() - 1; var hour2 = currentTime.getHours(); var hour3 = currentTime.getHours() + 1; var minutes = currentTime.getM ...

Audio in A-Frame does not function properly when in VR mode

My friend and I are collaborating on a small project involving a VR simulation that requires audio instructions. While everything functions smoothly in the web version and even on mobile devices, we encountered an issue when switching to VR mode on mobile ...

Prevent uploading files in Azure storage if the file with the same name already exists

Currently, I am facing an issue with uploading a file to azure storage. The package multer-azure works well for me, but whenever I try to upload a file with the same name as one already stored in the storage, it replaces the existing file. After going thr ...

Click on the radio button to receive a pair of boxes

I need help with creating a feature where clicking on this button <input type="radio" name="reise" value="reise">Tur/retur will display another box named "avreise" on the same page: This is the code for the box that should appear when the button is ...

Troubleshooting a dysfunctional collapsed navbar in React with Bootstrap 5

I am experiencing an issue where the collapsed navbar icon does not expand when clicked on smaller screens. I followed the example provided by bootstrap 5 and made sure to include bootstrap css/js and jquery. class CustomNavBar extends Component { re ...

Tips and tricks for switching user agents in the selenium-webdriver using nodejs?

Currently immersed in the world of javascript, mocha, and node. I attempted to set userAgent and 'user-agent' as keys on capabilities: var webdriver = require('selenium-webdriver'); var ua = 'Mozilla/5.0 (iPhone; CPU iPhone OS 5_ ...

"Encountering an issue with AngularJS: Want to use bindToController with a required controller, but

In one of my directives, I am utilizing the require and bindToController properties in the definition. (function(){ 'use strict'; angular.module('core') .directive('formValidationManager', formValidationManag ...

Change the format of the modelValue to align with the viewValue within an Angular directive

I need to customize the format of a datepicker in a form. The challenge is to display the date in 'dd/mm/yyyy' format for the user, while sending it in ISO format to an API. Below is the directive I am using: app.directive('standardDatepic ...

What's the best way to wrap elements in a div dynamically, inside another div?

I am trying to modify the output of WordPress content, but I do not have direct access to edit it. My goal is to use jQuery to wrap each link with accompanying text and line breaks in a div element and then organize them into two columns. The desired final ...

The art of combining arrays of objects while eliminating duplicates

I am in need of a solution to compare two object arrays, remove duplicates, and merge them into a single array. Although I am relatively new to javascript/Typescript, I have come across some blogs suggesting the use of Map, reduce, and filter methods for t ...

Encountering a "dependency resolution error" while deploying a React application with Parcel on Heroku

I've developed a compact application and I'm in the process of deploying it to Heroku. However, I keep encountering an error stating: '@emotion/is-prop-valid' dependency cannot be resolved. It's worth mentioning that this project d ...