mat-checkbox: The checkboxes are checked by default and affect each other? - angular-material

I have 7 checkboxes in my RactiveForm. This is how I am generating them using FormArray:
ts:
weekDaysDe: string[] = ['Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa', 'So'];
taskForm: FormGroup = this.formBuilder.group({
weekDays: this.formBuilder.array([
this.formBuilder.control(false),
this.formBuilder.control(false),
this.formBuilder.control(false),
this.formBuilder.control(false),
this.formBuilder.control(false),
this.formBuilder.control(false),
this.formBuilder.control(false),
]),
});
html:
<!-- weekdays -->
<div class="task-form-control" *ngIf="isRecurrent">
<mat-label>Tage</mat-label>
<div class="weekdays" formArrayName="weekDays">
<mat-checkbox
*ngFor="
let weekDay of taskForm.get('weekDays').value;
let i = index
"
color="primary"
labelPosition="before"
[formControlName]="i"
>{{ weekDaysDe[i] }}</mat-checkbox
>
</div>
</div>
The checkboxes are rendered as expected but there are two problems:
They are all checked at first
They check/uncheck each other. For example when I check the first one the last one also get checked
What have I done wrong?

In template, Use Form Array controls to iterate over with *ngFor.
Try this:
<div class="weekdays" formArrayName="weekDays">
<mat-checkbox *ngFor="
let weekDay of weekDaysArray.controls;
let i = index
" color="primary" labelPosition="before" [formControlName]="i">{{ weekDaysDe[i] }}
</mat-checkbox>
</div>
Access the FormArray control with a getter method like this.
componen.ts
get weekDaysArray() : FormArray{
return this.taskForm.get('weekDays') as FormArray;
}
Example

Related

How to add new row from last using swimlane/ngx-datatable

Tried to add new row to swimlane/ngx-datatable but not working properly. I want to add new row to from last but in my code it is adding on that same row.If you see my stackblitz demo you can understand my issue.Click on addnewrow option and see it.I do not know how to combine rowData and data to add the new row in the table. How to resolve this issue.
Note: We can use the addNewRow option(button or link) outside of table.
app.component.html:
<ng-template #buttonsTemplate let-row="row" let-value="value" let-rowIndex="rowIndex">
<div class="actions">
<a href="javascript:void(0)" (click)='addNewRow(row,rowIndex)'> AddNewRow </a>
</div>
</ng-template>
app.component.ts:
addNewRow(data, rowIndex) {
//Copy the data
let rowData = {
name: "Beryl Rice New",
gender: "Male",
company: "Velity",
actions: ""
};
//rowData.BATCH_CODE = "";
//rowData.qty = 0;
this.rows.splice(rowIndex, 0, rowData);
this.rows = [...this.rows];
}
Demo: https://stackblitz.com/edit/angular-ymttep?file=app%2Fapp.component.ts
Please update your addNewRow() method
this.rows.splice(rows.length, 0, rowData);

How to toggle a div based on a select box value change inside same component in ember

I am new to ember. I have two divs with a select box each. I want the second div to toggle based on the change in the first select box value.
First DIV with select box:
<div class="row row-space">
<div class="col-sm-10">
<div class="input-group input-group-sm">
<span class="input-group-addon edit_schedule_label" id="sizing-addon3">Schedule Level</span>
<div class="recurrence_box">
{{view "select" content=schedule_levels optionValuePath="content.value" optionLabelPath="content.label" selection="default_schedule_level.value" value="default_schedule_level.value" selectionBinding="default_schedule_level"}}
</div>
</div>
</div>
</div>
Second DIV:
{{#if group_level_decider }}
<div class="row row-space">
<div class="col-sm-10">
<div class="input-group input-group-sm">
<span class="input-group-addon edit_schedule_label" id="sizing-addon3">Location </span>
<div class="recurrence_box">
{{view "select" content=schedule_levels optionValuePath="content.value" optionLabelPath="content.label" selection="default_schedule_level.value" value="default_schedule_level.value" selectionBinding="default_schedule_level"}}
</div>
</div>
</div>
</div>
{{/if}}
In Controller, I am observing the change in selection binding of the first select box and setting the condition to toggle for the second div. I can see the condition variable here "group_level_decider" getting set to the correct value but it is not reflecting in the view.
Controller:
group_level_decider : false,
schedule_level_changed: function() {
var model = this.get('store');
model.set('selected_schedule_level',this.get('default_schedule_level.value'))
model.set('group_level_decider',true)
}.observes('default_schedule_level')
Please help me. Thanks!
Here is an example of filtering out cities based on a state:
App.IndexController = Ember.ArrayController.extend({
cities: function(){
var state = this.get('state');
var states = this.get('model');
var cities = [];
if(state){
cities = states.filterBy('state', state).get(0).get('cities');
}
return cities;
}.property('state'),
states: function(){
return this.get('model').mapBy('state');
}.property(),
stateObserver: function(){
this.set('city', null);
}.observes('state')
});
Then, inside your template:
States: {{view "select" prompt="Pick State..." content=states value=state}}
Cities: {{view "select" prompt="Pick City..." content=cities value=city }}
Working solution here

About bean of hasErrors

Assuming i have the following object in my controller
objCollection = [obj1, obj2, obj3]
And in each obj is the following
obj1 = [
input = value, inputA1 = valueA1, inputA2 = valueA2
]
obj2 = [
input = value, inputB2 = valueB2, inputA3 = valueA3
]
obj3 = [
input = value, inputC2 = valueC2, inputA3 = valueA3
]
And my GSP looks something like this
<div class="control-group ${hasErrors(bean: objCollection.obj, field: 'input', 'error')}">
<g:textField name='input' value='${objCollection?.obj}'>
</div>
<div id="subForm" class="subContainer">
<g:each var='obj' in='${objCollection}' status='a'>
<div id='obj${a+1}'>
<div class='subaccordion'>
<div>
<g:each var='objInstance' in='${obj}' status="b">
<g:render template="miniForm" model="[b:b, objInstance:objInstance]"/>
</g:each>
</div>
</div>
</div>
</g:each>
</div>
While for the template miniForm
<div class="control-group ${hasErrors(bean: objInstance, field: 'input', 'error')}">
<g:textField name='input' value='${objInstance?.obj}'>
</div>
<div class="control-group ${hasErrors(bean: objInstance, field: 'input', 'error')}">
<g:textField name='input' value='${objInstance?.obj}'>
</div>
Where the name attribute is changed using jquery to input A1, A2, B1, B2, C1 and C3 upon form load.
Now my question is, did i passed the object properly to the bean on hasErrors? Because i tried to get the hasErrors to work but somehow it didn't work to my expectation.
Did i made any mistake anywhere?
Also did i map the object correctly to every input in GSP? I'm not quite sure on how to use the safe operator '?' to avoid nullPointer error.

Angular Ui-grid cannot change column displayName

I have ui-grid.
My objects are boxes that may contain boxes or Samples.
Box1 = { 'Name': 'box1', 'Children': [{'Name': 'box2'},{'Name': 'box3'}]}
Box2 = { 'Name': 'box2', 'Children': [], 'Samples [{'Name': '1'}]}
At the beginning I show Box1. User can click Box1 or Box2 and then it opens a link that shows what the box contains.
nameLinkTemplate is link template ("getExternalScopes().followlink)
$scope.gridOptions.columnDefs = ({field: 'Name', displayName: 'Name', cellTemplate: nameLinkTemplate});
If box contains samples I change column to show just name without link.
$scope.gridOptions.columnDefs = ({field: 'Name', displayName: 'Name2', });
As a result I do not see link (that is what I wanted),
but Ui-Grig column headers are not displayed? Why?
I have also tried to reset the Ui-grid by: $scope.gridOptions = []; And define the grid again. Still I cannot change column displayName correctly.
I just added conditions on template and it works:
cellContainerNameLinkTemplate:
<div class="ui-grid-cell-contents" ng-if="row.entity.IsSample" ng-class="col.colIndex()">
<div class="gridnolinkpointer">
{{grid.getCellValue(row, col)}}
</div>
</div>
<div ng-if="!row.entity.IsSample">
<div class="nav nav-pills" ng-class="col.colIndex()" ng-click="getExternalScopes().followlink({event:$event,row:row})">
<a class="gridlinkpointer">{{grid.getCellValue(row, col)}}</a>
</div>
</div>
cell
after you change the name try calling :
$scope.gridApi.core.notifyDataChange(
uiGridConstants.dataChange.COLUMN );
it worked for me

Multiple ValueBinding Instance in Ember

I have a controller like this,
App.NewController = Ember.Controller.extend({
// the initial value of the `search` property
model: this.get('model'),
start_date:new Date(),
end_date: new Date()
});
My View template looks like this,
<div class="control-group">
<label class="control-label" for="inputPassword">Event Start Date</label>
<div class="controls">
{{view App.DatePickerField valueBinding='controller.start_date'}}
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputPassword">Event End Date</label>
<div class="controls">
{{view App.DatePickerField valueBinding='controller.end_date'}}
</div>
</div>
finally i have my datepicker view like this:
App.DatePickerField = Ember.View.extend({
templateName: "Datepicker",
didInsertElement: function () {
var OnChangeDate, self;
self = this;
OnChangeDate = function (evt) {
return self.set('value', evt.date.toString());
};
return $('.date').datetimepicker({
language: 'en',
}).on('changeDate', OnChangeDate);
}
});
the problem now is that if i select my start date, controller.start_date updates at the same time, just as i want. But when i select the end date, both start_date and end_date gets updated,now the start_date has the same value of end_date. It's like the ValueBinding keeps both in sync. I'm fairly new to Ember. Could someone please help me, what i'm doing wrong here?
This is hooking up to both pickers
return $('.date').datetimepicker({
language: 'en',
}).on('changeDate', OnChangeDate);
you can scope it to this element using, this.$()
return $(this.$('.date')).datetimepicker({
language: 'en',
}).on('changeDate', OnChangeDate);

Resources