Angular 9 - ngModel select and array of objects (inside a ngFor loop) - select

I have an array of objects
private data=[{title:'a',num:10},{title:'b',num:20},{title:'c',num:30}];
and I want, for each of the objects to create a select box that modifies it's num field
<form class="form-horizontal" (ngSubmit)='onSubmit()'>
{{ data | json }}
<div *ngFor="let item of data;let i=index">
<div>{{ data[i]['title'] }}</div>
<select [(ngModel)]="data[i]['num']">
<option [ngValue]="10">10</option>
<option [ngValue]="20">20</option>
<option [ngValue]="30">30</option>
</select>
</div>
...
IF THE CODE IS INSIDE A FORM, then the data is NEVER updated and all select box start empty
thanks

You're accessing a private property in your template, it has to be public.
Component
public data=[{title:'a',num:10},{title:'b',num:20},{title:'c',num:30}];
Template
{{ data | json }}
<div *ngFor="let item of data;">
<div>{{ item.title }}</div>
<select [(ngModel)]="item.num">
<option [ngValue]="10">10</option>
<option [ngValue]="20">20</option>
<option [ngValue]="30">30</option>
</select>
</div>
Working example :
https://stackblitz.com/edit/angular-ivy-nazzvh?file=src/app/app.component.html

apparently, forms and angular still dont go well together
I changed to and it works like a charm
<form class="form-horizontal" (ngSubmit)='onSubmit()'>
{{ data | json }}
<div *ngFor="let item of data;let i=index">
<div>{{ data[i]['title'] }}</div>
<select [(ngModel)]="data[i]['num']">
<option [ngValue]="10">10</option>
<option [ngValue]="20">20</option>
<option [ngValue]="30">30</option>
</select>
</div>

Related

Load dropdown item but disable for selection

So I want to be able to load the 'Choose' dropdown option, but disable it as soon as a change is made on the drop-down.
I've attempted adding a disabled attribute tag on the selection, but it loads on 'Alabama' and I don't want that.
How would I be able to tackle this?
<div class="row mt-3">
<div class="col-md">
<label for="resident">I am a resident of:</label>
</div>
</div>
Adding hidden and selected to the first option should do the trick.
<div class="row mt-3">
<div class="col-md">
<label for="resident">I am a resident of:</label>
<select class="custom-select" id="resident" required>
<option hidden selected value="">Choose...</option>
<option>Alabama</option>
<option>Alaska</option>
<option>Colorado</option>
<option>New York</option>
<option>South Dakota</option>
</select>
<div class="invalid-feedback">
Select state of residence.
</div>
</div>
</div>
If you want to still see the "Choose..." after the user has made a selection, replace hidden with disabled.
Why don't you disable the first option? (no javascript is needed and the result is like what you want to achieve I think):
<div class="row mt-3">
<div class="col-md">
<label for="resident">I am a resident of:</label>
<select class="custom-select" id="resident" required>
<option value="" disabled>Choose...</option>
<?php foreach (Vars::states() as $abbr => $state): ?>
<option value="<?= $abbr; ?>"><?= $state; ?></option>
<?php endforeach; ?>
</select>
<div class="invalid-feedback">
Select state of residence.
</div>
</div>
</div>
And a demo code snippet of what it looks like:
<select required>
<option value="" disabled selected>Select your option</option>
<option value="1">Alabama</option>
<option value="2">Alaska</option>
<option value="3">...</option>
</select>

Form not sending out select and checkbox inputs in Angular 2

I'm working on a small Angular 2 To-do app. I didn't want to have any problems with browser compatibility with inputs types like date, datetime-local, etc., so I did make <select> inputs for user to type in the date and time. Everything works fine, inputs are dynamic, so the user cannot choose the day that does not exist (like e.g. 02/29/2017), etc.
The problem is, I want to send form's data to the service and then to the Back-End of my app, but when I submit the form, values from <select> inputs are not included in the sent object, as well as my checkbox input. I'm not often using these types of inputs, so I'm sorry if this is a noobish question, but I cannot figure out what I'm doing wrong.
Here is the code:
<form #f="ngForm" (ngSubmit)="add(f.value)">
<div *ngIf="error">
<p>{{ error }}</p>
</div>
<div class="login-input-container">
<input type="text" placeholder="title" name="title" ngModel autocomplete="off" required minlength="1" maxlength="100">
</div>
<div class="login-input-container">
<div class="datetime-container">
<div>
<select #year name="year" (change)="showMonths(); selectedYear = year.value; yearDays(year.value);" required>
<option class="invisible" value="" disabled selected>year</option>
<option *ngFor="let year of years" [value]="year" placeholder="year">{{ year }}</option>
</select>
<select #month *ngIf="showedMonths" name="month" (change)="showDays(month.value, year.value); selectedMonth = month.value;" required>
<option class="invisible" value="" disabled selected>M</option>
<option *ngFor="let month of months" [value]="month">{{ month }}</option>
</select>
<select *ngIf="showedDays" name="day" (change)="showTime()" required>
<option class="invisible" value="" disabled selected>d</option>
<option *ngFor="let day of days" [value]="day">{{ day }}</option>
</select>
</div>
<div *ngIf="showedTime">
<select name="hours" required>
<option class="invisible" value="" disabled selected>hh</option>
<option *ngFor="let hour of hours" [value]="hour">{{ hour }}</option>
</select>
:
<select name="minutes" required>
<option class="invisible" value="" disabled selected>mm</option>
<option *ngFor="let minute of minutes" [value]="minute">{{ minute }}</option>
</select>
</div>
</div>
</div>
<div class="login-input-container">
<textarea id="todo-description" type="text" placeholder="description (optional)" name="description" ngModel autocomplete="off" minlength="1" maxlength="500"></textarea>
</div>
<div class="login-input-container">
<span><p>should we notify you?</p><label for="notify-1"><input id="notify-1" type="checkbox" [checked]="todo.notify" value="1"><span></span></label></span>
</div>
<div class="login-input-container">
<input type="submit" value="Submit" (click)="error = ''">
</div>
</form>
If you use [value] or [ngValue] you need to use [(ngModel)] or ngModel (ngModelChange)="showMonths()..." instead of `(change)="..."

Codeigniter - Error While Passing Variables From Container to View

Hi I want to Pass input elements values while clicking button using post method and pass values to next view using CI. When submitting the button with values in input elements and redirecting it works well. But When I'm refreshing after redirecting the page the values of input elements are lost and shows blank page/the page i display when the value becomes null.
1st View
<form action="<?php echo base_url() ?>Mapcontrol/display_garage_by_location" method="post">
<div class="section_1">
<select id="cmbcity" name="cmbcity" class="frm-field required">
<option value="null">Select Your City</option>
<option value="1">fghfghfghf</option>
<option value="2">fghfghfgh</option>
<option value="3">fghfgh</option>
<option value="4">ffffffff</option>
<option value="5">hhhh</option>
<option value="6">dddd</option>
<option value="7">treee</option>
<option value="8">kiu</option>
</select>
</div>
</div>
<div class="col-md-3 dropdown-button">
<div class="input-group">
<span class="glyphicon glyphicon-map-marker" aria-hidden="true"></span>
<input type="text" id="txtPlaces" placeholder="Enter a location" />
<input type="hidden" id="city2" name="city2" />
<input type="hidden" id="cityLat" name="cityLat" />
<input type="hidden" id="cityLng" name="cityLng" />
</div>
</div>
<div class="col-md-3 dropdown-button">
<div class="section_1">
<select id="cmbvehicletype" name="cmbvehicletype" class="frm-field required">
<option value="null">Type</option>
<option value="cr">dd</option>
<option value="mb">ghjghjd</option>
<option value="tk">ghjghj</option>
<option value="ar">hgjghj</option>
<option value="bi">gggh</option>
<option value="ov">gg</option>
</select>
</div>
</div>
<div class="col-md-3 dropdown-button">
<div class="section_1">
<select id="cmbservice" name="cmbservice" class="frm-field required">
<option value="null">Select</option>
<option value="vr">vr</option>
<option value="oc">oc</option>
<option value="br">br</option>
<option value="vs">vc</option>
<option value="sp">cc</option>
<option value="os">cd</option>
</select>
</div>
</div>
<div class="clearfix"> </div>
</div>
<div class="col-md-2 submit_button">
<input type="submit" id="searchindex" value="SEARCH"/>
</div>
<div class="clearfix"> </div>
</div>
</div>
</div>
Controller
public function display_garage_by_location()
{
// Loading Library
$this->load->library('googlemaps');
//Initialize our map. Here you can also pass in additional parameters for customising the map
$this->googlemaps->initialize();
// Create the map. This will return the Javascript to be included in our pages <head></head> section and the HTML code to be
// placed where we want the map to appear.
$data['map'] = $this->googlemaps->create_map();
// Load our view, passing the map data that has just been created
//$this->load->view('my_view', $data);
$data['locationvalue']=$this->input->post('city2');
$data['latvalue']=$this->input->post('cityLat');
$data['longvalue']=$this->input->post('cityLng');
$data['vehicleid']=$this->input->post('cmbvehicletype');
$data['serviceid']=$this->input->post('cmbservice');
$this->garage_location_markers($data);
}
public function garage_location_markers($data)
{
$this->load->view('templates/_header');
$this->load->view('content/locations-in-marker-view',$data);
$this->load->view('templates/footer');
}
2nd View
// In Second View I want to display if location value is null then dispaly no data found else display search results ..
The Problem is When location is not null it displays the details but when i refresh the page again control go back to the controller and results no data found.. How to Solve this?
Try Like this
In View
View user
In Controller
public function FunctionName($id) # determine function that receiving incoming parameter
{
if (empty($id)) {
echo "Token Missed Matched";
}
else{
# get data from model
$result = $this->model_name->model_function($id);
if(empty($result))
{
echo "No data found";
}
else{
$this->load->view('view_name');
}
}
}

Form for filters in Symfony2?

I have a list of Link and I want to filter them by Category.
In my view I hard-coded a form with a select.
<form method="GET" action="{{ path('crm_links') }}">
<input class="btn btn-success pull-right" type="submit"/>
<select name="category" class="selectpicker pull-right" data-width="180" data-size="auto">
<option value="0">Select a category</option>
{% for category in categories %}
<option value="{{ category.id }}">{{ category.name }}</option>
{% endfor %}
</select>
</form>
Now in my controller I want to check for a "category" GET parameter.
And if it is > 0, I grap all link with this category id.
Do you know a cool way to do this ? Is it an appropriated way or it's not the symfony way to do this ?
You should use this FormFilterBundle : https://github.com/lexik/LexikFormFilterBundle, with a filter_choice type.
It's straightforward and easy to get started with.

symfony2 - date choice input renders timestamp instead of month name

I just recently ran into a problem, where my Symfony2 app renders form with invalid date choice input. Specifically, it renders the "month" select with numbers inside, instead of month names.
The definition of the form type goes like this:
$builder
->add('title')
->add('shortcut')
->add('description')
->add('category')
->add('release_date', 'date', array(
'input' => 'datetime',
'widget' => 'choice'
));
This definition then (by using form_widget in Twig) renders the following for month select:
<select id="nucleo_gamesbundle_gametype_release_date_month" name="nucleo_gamesbundle_gametype[release_date][month]" required="required">
<option value="1">1326585600</option>
<option value="2">1329264000</option>
<option value="3">1331769600</option>
<option value="4">1334448000</option>
<option value="5">1337040000</option>
<option value="6">1339718400</option>
<option value="7">1342310400</option>
<option value="8">1344988800</option>
<option value="9">1347667200</option>
<option value="10">1350259200</option>
<option value="11">1352937600</option>
<option value="12">1355529600</option>
</select>
Does anyone know, how to force Symfony to render option texts as month names (eg. January, February, etc.)? Thank you very much.
EDIT:
Twig template (the form part) looks like this:
<form action="{{ path('game_admin_update', {'id' : entity.id}) }}" method="post" class="form" {{ form_enctype(edit_form) }}>
<fieldset>
<div class="widget">
<div class="title"><img src="{{ asset('admin/images/icons/dark/list.png') }}" alt="" class="titleIcon" /><h6>Základní informace</h6></div>
<div class="formRow">
{{ form_label(edit_form.title, 'form.game.title'|trans({},'admin')) }}
<div class="formRight">{{ form_widget(edit_form.title) }}</div>
<div class="clear"></div>
</div>
<div class="formRow">
{{ form_label(edit_form.shortcut, 'form.game.shortcut'|trans({},'admin')) }}
<div class="formRight">{{ form_widget(edit_form.shortcut) }}</div>
<div class="clear"></div>
</div>
<div class="formRow">
{{ form_label(edit_form.description, 'form.game.description'|trans({},'admin')) }}
<div class="formRight">{{ form_widget(edit_form.description, { 'attr': {'class': 'richEditor'} }) }}</div>
<div class="clear"></div>
</div>
<div class="formRow">
{{ form_label(edit_form.release_date, 'form.game.release_date'|trans({},'admin')) }}
<div class="formRight">{{ form_widget(edit_form.release_date) }}</div>
<div class="clear"></div>
</div>
{{ form_rest(edit_form) }}
<div class="formSubmit"><input type="submit" value="{{ 'form.game.submit.edit'|trans({},'admin') }}" class="greenB" /></div>
<div class="clear"></div>
</div>
</fieldset>
So nothing special there
Of course, you can provide a format option to transform the datetime into the format of your choice:
$builder
->add('title')
->add('shortcut')
->add('description')
->add('category')
->add('release_date', 'date', array(
'input' => 'datetime',
'widget' => 'choice',
'format' => 'yMMMMd',
));
This will print a select box for the year, e.g. 2012, one for the month with the long month names, and one for the day, e.g. 21. Take a look here for more formats.