Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert a select option to boolean value

Tags:

I am learning angular2. Here I have the following Problem:

I have a select field which should work as a boolean value:

<select [(ngModel)]="caseSensitive">     <option>false</option>      <option>true</option>    </select> 

No if i use it in my Filter it will send as a string not as a boolean. Is it possible to convert it using a converter or something like that:

Here my complete HTML code:

<input [(ngModel)]="nameFilter"/> <select [(ngModel)]="caseSensitive">     <option>false</option>      <option>true</option>    </select>    <table>   <tr *ngFor="let p of (persons | MyFilter: nameFilter:caseSensitive); let i = index">     <td>{{i + 1 }} </td>     <td>{{       p.givenName+" "+ p.familyName     }}</td>     <td><img src="/img/flags/{{ p.nationality}}.png"></td>    </tr> </table> 

The ts code:

import { Component } from '@angular/core';  import {MyFilter} from './MyFilter';  @Component({   selector: 'pizza-root',   pipes: [MyFilter],   templateUrl: 'app.component.html'   }) export class AppComponent {     public year = new Date().getFullYear();     public persons =[{"givenName":"Paul", "familyName": "Smith", "nationality":"american"},                      {"givenName":"Jens", "familyName":"myName1", "nationality":"german"},                      {"givenName":"Ernst", "familyName":"myName1", "nationality":"german"},                      {"givenName":"Jenny", "familyName":"myName1", "nationality":"german"}];     constructor (){         console.log(this.persons);     } } 

The Pipe:

import { Pipe, PipeTransform} from '@angular/core';  @Pipe({     name: 'MyFilter' })  export class MyFilter implements PipeTransform{     transform( items: any[], args: string, caseSensitive : boolean ):any {         if (items != null && args != undefined && args  != ''){             if (caseSensitive){                  console.log("caseSensitive")                 return items.filter(item=>item.givenName.indexOf(args)!== -1);             } else {                 console.log("caseInSensitive")                 return items.filter(item=> item.givenName.toLowerCase().indexOf(args.toLowerCase())!== -1);             }         }         console.log("else")         return items;     }  } 

The Problem is, that the pipe not working correctly because caseSensitive is the bind as a string not as a boolean value.

like image 398
Jens Avatar asked Oct 07 '16 06:10

Jens


People also ask

How do you assign a value to a boolean?

To declare a Boolean variable, we use the keyword bool. To initialize or assign a true or false value to a Boolean variable, we use the keywords true and false. Boolean values are not actually stored in Boolean variables as the words “true” or “false”.

How do you return a boolean and not a String when using select?

Use the Vue way, set an array of options in your Vue data then use v-for to render all the options from the array. Each option should have 2 properties: a text and a value. The value should be the boolean you are looking for. Now whenever the user changes the selected option, it will always be boolean.

Can we convert String to boolean in Java?

To convert String to Boolean, use the parseBoolean() method in Java. The parseBoolean() parses the string argument as a boolean. The boolean returned represents the value true if the string argument is not null and is equal, ignoring case, to the string "true".

How do you change boolean to text?

We can convert boolean to String in java using String. valueOf(boolean) method. Alternatively, we can use Boolean. toString(boolean) method which also converts boolean into String.


1 Answers

The values in <option> are strings or they are stringified if other types are provided. If you use ngValue instead, you can use other types and ngModel retains the type.

  <select [(ngModel)]="caseSensitive">     <option [ngValue]="false">false</option>      <option [ngValue]="true">true</option>    </select> 
like image 145
Günter Zöchbauer Avatar answered Oct 10 '22 16:10

Günter Zöchbauer