Fanciful Selective Ionic Directive

I recently started using fancySelect from the Ionic framework for multiple select options. As I am still getting acquainted with Ionic, I encountered an issue where I needed some items in fancySelect to be pre-selected by default.

If you want to take a look at the code and help me out, here is the link to the Code Pen: http://codepen.io/anon/pen/QjydZd?editors=101

I would greatly appreciate any assistance on how to achieve this. Thank you in advance!

enter code here

Sincerely, DS

Answer №1

Primarily AngularJS is used, not Ionic. To initialize the array of items for the select feature, make sure to set item.checked = true; Check line 164 in the javascript source code and lines 78+ in the HTML source on codepen.

<script>
var $scope.countries = [
  {id: 1, name: 'United States', checked: true},
  {id: 2, name: 'Germany',       checked: false}
]
</script>


<ion-toggle
     ng-repeat="item in items"
     ng-if="multiSelect"
     ng-checked="item.checked"
     ng-model="item.checked"
     class="item item-text-wrap">

If you want to implement single select, include an angular expression to set a css class:

ng-class="{selected : item.checked}"

Therefore, the label would look like this:

<!-- Single select -->
<label
   ng-repeat="item in items"
   ng-if="!multiSelect"
   class="item item-text-wrap"
   ng-class="{selected : item.checked}"
   ng-click='validateSingle(item)'>

Add a class to your stylesheet to highlight selected countries when the modal opens:

.selected {
  background-color: red !important;
}

If you wish to display the selected country as the label for the fancy select, use something similar to this:

$scope.countries_text_single = $scope.countries[0].name;

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

Unlock the potential of JavaScript by accessing the local variable values in different functions

I've been struggling for days to find a solution to this issue... https://i.stack.imgur.com/KDN7T.jpg https://i.stack.imgur.com/tOfCl.jpg The image above illustrates the challenge I'm facing - trying to apply data values from elsewhere to the ...

Exploring the search capabilities of input fields that are disabled

Lately, I've been struggling with a search function on my table of information. The trouble started when I made the text in the table editable - now the search function refuses to cooperate. I've tried troubleshooting it from different angles, b ...

Can you tell if there are any distinctions between the two code snippets?

Initial Code console.log('Begin'); // output 1 await axios({ method: 'post', url: '<HTTP_URL>' data: <SOME_DATA>, }).then ((response) => { // Performing some action... This may take a few seconds. con ...

Tips for building a live React app!

I'm looking to develop a real-time news application that displays a list of countries with the latest news next to each country name, automatically updating as new information is added to the API. For instance, I have an endpoint like this: (for Aust ...

Using JavaScript to extract information from a dropdown menu

Currently, I am working on a project that involves reading values from a dropdown box on a webpage and alerting the selected value. Below is the code I have written for this task. JavaScript function main(input) { value = getValue(); alert(value) ...

Apply a see-through overlay onto the YouTube player and prevent the use of the right-click function

.wrapper-noaction { position: absolute; margin-top: -558px; width: 100%; height: 100%; border: 1px solid red; } .video-stat { width: 94%; margin: 0 auto; } .player-control { background: rgba(0, 0, 0, 0.8); border: 1px ...

Array of generic types in Typescript

Here's a method that I have: getFiveObjectsFromArray(array: T[]) { return array.slice(0, 5); } I've been using this method multiple times. Is there a way in TypeScript to pass a generic argument instead of using multiple types? Also, when ...

Having difficulties transmitting numerical values through res.send() in express with node

Currently, I'm faced with a challenge in my express application on node.js as I attempt to retrieve the "imdb rating." movies.json [{ "id": "3962210", "order": [4.361276149749756, 1988], "fields": { "year": 2015, "title": "David and Goliath" ...

Avoid repeat sending of post requests using AJAX

I'm facing an issue with my ajax functionality. It seems to send my request only once. I am using a cssmap plugin that contains the 'onSecondClick' option, allowing me to perform an action when clicking twice. Upon clicking, a post request i ...

Iterate over the object to verify if the field contains an empty array, then output null

Looking for help with handling empty arrays in an object: productDetails: { cislife: [], prime: [] } Is there a way to have null returned instead of an empty array if no values are available? For example, I'd like to determine if either t ...

Is it possible to utilize getInitialProps in both _app.js and in individual pages within the application?

I am currently developing my first significant NextJS application. Instead of hardcoding the left navigation data directly into the app, I have set it up to pull in JSON data. This allows me to make minor changes to the site's navigation without havin ...

Utilize href attribute for triggering a PHP function

Being new to web development, I have thoroughly researched my issue before reaching out for help. Unfortunately, I have not been able to find a solution to my specific problem. My goal is to redirect to a specific page and execute a PHP function that requ ...

Has the HTML attribute 'step' stopped functioning properly?

I'm currently working on an Angularjs project that primarily uses material design. I am trying to set up a timepicker using the input control with the type=time attribute. However, I want to restrict the user to select times only within a 30-minute ra ...

What is the best way to remove a class from an element upon successful completion?

How can I make an image disappear when the user clicks a button in the associated form? I'm struggling to implement this feature. Any suggestions? <script type="text/javascript"> $(document).ready(function() { $(".removebutton").s ...

How can I pass an Objective-C object to JavaScript?

Currently, I am working on a UIWebView project that involves HTML and JavaScript. One of my friends is working on something similar but for Android. The challenge I'm facing is that in Android, there is a simple way to send an Android object to JavaS ...

React re-rendering only when arrays are filtered, and not when new elements are added

I need help with a table simulation where I can add and remove products. The issue I'm facing is that the component only updates when an item is removed, not when a new row is added. Currently, I am utilizing the useState hook. Below is my array data ...

AngularJS enables users to showcase and edit images row by row in a table simply by hovering over them

I am currently facing an issue with my code. I initially implemented a mouseover function, but it was quite slow in displaying my edited pictures per row. After some research, I learned that using mouseenter might be faster (which is crucial since I have a ...

What causes MongoDB to modify my UTC time zone from UTC+2:00 to UTC+0 when utilizing Mongoose in conjunction with moment-timezones.js?

MongoDB recently updated their UTC time from UTC+2:00 to UTC+0. Despite attempting to include the option "{ forceServerObjectId: true }", I still encountered issues. My code relies on moment-timezones.js for creating dates, here's a snippe ...

Deleting a database row when the cancel button is pressed

I have a scenario where I need to remove a database row containing a file name when the user clicks on the cancel button. However, despite my efforts, the database row is not being deleted. Can anyone help me identify what I might be doing wrong? Here is ...

Sending data with an AJAX post request from JavaScript to a Python backend

Attempting to pass a dictionary from JavaScript to a Python script using AJAX POST method. Here's the JavaScript code: function foo(){ context = { 'var1': val1, 'var2': val2 } $.ajax({ ...