How to highlight multiple selected row in angular 4 ,
Here I can edit with checkbox and perform other action too. What I am looking is to highlight the row which is checked.
<tbody>
<tr *ngFor='let row of rowData' [ngClass]="{ 'selected': row.selected }">
<td class="text-center>
<input type="checkbox" [(ngModel)]="row.selected" />
</td>
<td>
<input type="text" *ngIf="row.editable" [(ngModel)]="row.name" />
<ng-container *ngIf="!row.editable">{{row.name}}</ng-container>
<!-- You can use span or whatever instead of ng-container-->
</td>
<!-- Repeat for other cells -->
</tr>
</tbody>
It is dead simple simply use angular class directive
<tr *ngFor='let row of rowData' [class.selected]="row.selected">
Now it will add class selected when row.selected is true to the row
To highlight a row, you need to highlight the cells (<td>). From what I know, you can't highlight a row.
Here is the logic :
<tbody>
<tr *ngFor='let row of rowData'>
<td class="text-center" [class.selected]="row.selected">
<input type="checkbox" [(ngModel)]="row.selected" />
</td>
<td [class.selected]="row.selected">
<input type="text" *ngIf="row.editable" [(ngModel)]="row.name" />
<ng-container *ngIf="!row.editable">{{row.name}}</ng-container>
<!-- You can use span or whatever instead of ng-container-->
</td>
<!-- Repeat for other cells -->
</tr>
</tbody>
HTML :
<tr *ngFor="let record of mf.data; let i = index" [attr.data-index]="i" [className]=" selectedAll==true || selectedRow.indexOf(i)>-1 ?
'pointer selected' : 'pointer'">
<td class=" text-left" width="20">
<input type="checkbox" (change)="selectAll(i)" [checked]="selectedAll == true">
</td>
...
</tr>
TS :
export class YourComponent implements OnInit {
selectedRow: any;
selectedAll: boolean = false;
constructor(){
this.selectedRow = [];
}
selectAll(index) {
if (typeof (index) == 'undefined') {
this.selectedAll = !this.selectedAll;
this.selectedRow = [];
} else {
this.selectedRow.push(index);
}
}
HTML :
<tr *ngFor="let record of mf.data; let i = index" [attr.data-index]="i" [className]=" selectedAll==true || selectedRow.indexOf(i)>-1 ?
'pointer selected' : 'pointer'">
<td class=" text-left" width="20">
<input type="checkbox" (change)="selectAll(i)" [checked]="selectedAll == true">
</td>
...
</tr>
TS :
export class YourComponent implements OnInit {
}
Related
Validation not working for radio button in Angular.
When radio button is not selected, form is getting submitted.
Also not showing error.
HTML Code
<form [formGroup]="feedbackFormWithArray" (ngSubmit)="submitData()">
<table class="res-tbl">
<tbody formArrayName="skills" class="tbl">
<tr class="skill-tr table-data" *ngFor="let skill of skills; let i = index">
<td class="table-data res-td tbl-border skill-td">
<b value="skill.skillId"><span class="text-danger">*</span>{{skill.skillName}}<span>:</span></b><p class="skill-description-p"> {{skill.skillDescription}}</p>
</td>
<td class="table-data center rating-td">
<input type="radio" formControlName="{{ i }}" [value]="3" />
</td>
<td class="table-data center rating-td">
<input type="radio" formControlName="{{ i }}" [value]="2" />
</td>
<td class="table-data center rating-td">
<input type="radio" formControlName="{{ i }}" [value]="1" />
</td>
</tr>
</tbody>
<div *ngIf="(submitted && skills.invalid)">
<small *ngIf="skills.errors?.required" class="text-danger">Please select Skills</small>
</div>
</table>
<Button type="submit" [disabled]="feedbackFormWithArray.invalid" class="btn button-btn" >{{feedbackId ? 'Update' : 'Create'}}</Button>
</form>
TS Code
import { Validators } from '#angular/forms';
submitted : boolean = false;
this.feedbackFormWithArray= this.fb.group({
skills: this.fb.array(
this.skills.map((t) => {
this.fb.control(t);
}), {validators: Validators.required}
)
});
submitData() {
this.submitted = true;
}
How to solve this?
Thank you!
I am gonna guess you have a button inside your form, something like:
component.html
<button (click)="submitData()">Submit</button>
You could add a check to change submitted state like
submitData() {
if (this.feedbackFormWithArray.valid) {
this.submitted = true;
}
if (this.feedbackFormWithArray.invalid) {
// Do something like this.tryToSubmitWithError = true
// Popup to say you cannot submit
// ...
}
}
I think also you wanted to use feedbackFormWithArray instead of skills in the *ngIf below:
<div *ngIf="(submitted && skills.invalid)">
<small *ngIf="skills.errors?.required" class="text-danger">Please select Skills</small>
</div>
That could be like:
<div *ngIf="tryToSubmitWithError && feedbackFormWithArray.invalid">
<small *ngIf="feedbackFormWithArray.errors?.required" class="text-danger">
Please select Skills
</small>
</div>
I have a table where I have a column name and near the column name I have a checkbox. That particular column name values inside the table by default are check boxes. What I need is that when I enable the checkbox near the column name, all the checkboxes in the table under that column should be enabled and when I disable the checkbox in column name all checkboxes in table under the column should also be disabled?
<div class="Container">
<div class="table-responsive">
<table class="table">
<thead class="table_header">
<tr>
<th>ID</th>
<th>Name</th>
<th>Category</th>
<th>Old date</th>
<th>New date</th>
<th>
<input type="checkbox" id="vehicle1" name="vehicle1" value="Bike"> Status
</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let form of formlist; index as i">
<td>{{ form.name }}</td>
<td>{{form.cat }}</td>
<td>{{form.olddate }}</td>
<td>{{ form.newdate }}</td>
<td>
<input type="checkbox" id="vehicle1" name="vehicle1" value="Bike">
</td>
</tr>
</tbody>
</table>
</div>
</div>
What I need is that when I enable the checkbox near the column name status, all the checkboxes in the table under that column should be enabled and when I disable the checkbox in column name all checkboxes in table under the column should also be disabled?
You can give class to checkbox and with jQuery you can check enable or disable:
.attr("disabled",true)
.attr("disabled",false)
remove "vehicle1" id from <td> elements, then copy and paste script given below.
<script>
$(document).ready(function($){
$("#vehicle1").click(function () {
$('input:checkbox').not(this).prop('checked', this.checked);
});
});
</script>
In Angular
<label class="btn btn-filter">
<input type="checkbox" name="allTrades" [value]="trade" (change)="allTrades($event)">All Trades
</label>
<ng-container *ngFor="let trd of trade">
<label class="btn btn-filter">
<input type="checkbox" name="trades" [checked]="trd.selected" (change)="changeTradesByCategory($event)">{{ trd.label }}
</label>
</ng-container>
export class AppComponent {
name = 'Angular';
trade = [
{ label: 'aa', selected: false },
{ label: 'bb', selected: false },
{ label: 'cc', selected: false },
{ label: 'dd', selected: false }
];
allTrades(event) {
const checked = event.target.checked;
this.trade.forEach(item => item.selected = checked);
}
}
There are two date fields plannedRatingDate and PlannedRatingCommitteeDate on my UI.
If the user fills in plannedRatingDate field,rcReview field should be set to 'No'.
If the user fills in plannedRatingCommitteeDate field,rcReview field should be set to 'Yes'.
rcReview field is a radio button field on my UI.
In app.component.html:
<tr>
<td colspan="2"></td>
<td style="width:500px">
<label for="PlannedReviewDate">Planned Review Date</label>
<input id="PlannedReviewDate" name="PlannedReviewDate" (change)="basedOnRatingDate($event)" [(ngModel)]="deal.plannedReviewDate" placement="right" type="text" bsDatepicker [bsConfig]="datePickerConfig" class="form-control">
</td>
</tr>
<br>
<tr>
<td colspan="3"></td>
<td><label for="isReviewComplete">Is Review Complete?</label>
<input id="isReviewComplete" name="isReviewComplete" [(ngModel)]="deal.isReviewComplete" type="radio" class="form-control" value="Complete">
Complete<br></td>
</tr>
<tr>
<td colspan="3"></td>
<td><input id="isReviewComplete" name="isReviewComplete" [(ngModel)]="deal.isReviewComplete" type="radio" class="form-control" value="Not Complete">
Not Complete<br></td>
</tr>
<br>
<tr>
<td colspan="3"></td>
<td style="width:500px">
<label for="PlannedRatingCommitteeDate">Planned rating committee date</label>
<input id="PlannedRatingCommitteeDate" name="PlannedRatingCommitteeDate" (change)="populateRCReview($event)" [(ngModel)]="deal.plannedRatingCommitteeDate" placement="right" type="text" bsDatepicker [bsConfig]="datePickerConfig" class="form-control">
</td>
</tr>
<br>
<tr>
<td colspan="3"></td>
<td>
<label>RC Review</label>
<input id="rcReview" name="rcReview" [(ngModel)]="deal.rcReview" type="radio" class="form-control" value="Yes">
Yes<br>
</td>
</tr>
<tr>
<td colspan="3"></td>
<td>
<input id="rcReview" name="rcReview" [(ngModel)]="deal.rcReview" type="radio" class="form-control" value="No">
No<br>
</td>
</tr>
In app.component.ts:
export class AppComponent implements OnInit {
title = 'angular-app';
name = 'angular-app';
datePickerConfig: Partial<BsDatepickerConfig>;
dropdownList = [];
selectedItems: any;
selected:any;
dropdownSettings:IDropdownSettings;
dealid : number;
isReviewComplete: any;
plannedRatingCommitteeDate: Date;
plannedReviewDate: Date;
ExpectedReleaseDate: Date;
ReleaseTimeCriteria: any;
SubsequentRating: any;
Priority: any;
rcReview: any;
ApplicationReceived: any;
subsequentRating: any;
deal:DealApi= new DealApi();
constructor(private service:HttpclientService) {}
ngOnInit(){
this.datePickerConfig = Object.assign({},{ containerClass:'theme-dark-blue', showWeekNumbers: false});
};
public savenow(){
this.service.getdeals(this.deal).subscribe((data:any)=>{alert("Deal added successfully.");});
}
basedOnRatingDate(event:any){
this.deal.rcReview = "No";
}
populateRCReview(event:any){
this.deal.rcReview = "Yes";
}
Above code is not autopopulating rcReview field with 'No' if user enters plannedRatingDate field.
What changes should be done?
Any Help will be appreciated!
I resolved the issue by using ngModelChange event instead of change event.
I have two arrays. The first array contain rows in table one of this row name department id and one row has a selection (transfer to another department which contain the second array)
now transfer to should filter to not contain the same first array department, pic may explain more.
when i add *ngIf="DepartmentDTO.dept_id != ALLREQUEST.dept"
i got the list but come null value with list, pic show.
also i if possible to get the list with other filter which
*ngIf="DepartmentDTO.dept_id != ALLREQUEST.dept ; DepartmentDTO.dept_id != 0"
Expected Output
Current Output
<table class="table table-hover">
<tr>
<td *ngFor="let DepartmentDTO of depts">
<label><input class="input1" name="radio" ng-control="depts" type="radio" [value]="DepartmentDTO.dept_id" [(ngModel)]="deptId" (click)="getClinicTotal(DepartmentDTO.dept_id)">
{{DepartmentDTO.dept_name}}<br>
</label></td>
</tr>
</table>
TotalCount ={{total_count}}
<br> Department _id:= {{deptId}} all:= {{all}}, Triage:= {{triage}} , clinics:= {{clinics}} , gb:= {{gb}} , dentail:= {{dentail}}. selectedRow = {{selectedRow}}
<table class="table table-hover">
<thead>
<tr>
<th>qid</th>
<th>qno</th>
<th>dept</th>
<th>Tocken For</th>
<th>Transfer to</th>
</tr>
</thead>
<tbody *ngFor="let ALLREQUEST of AllRequest;let i = index">
<tr *ngIf="deptId==0 || deptId== ALLREQUEST.dept">
<td>
{{ALLREQUEST.qid}}
</td>
<td>
{{ALLREQUEST.qno}}
</td>
<td>
{{ALLREQUEST.dept}}
</td>
<td>
{{ALLREQUEST.dept_name}}
</td>
<td>
<select type="list" name="action" [(ngModel)]="ALLREQUEST.transfer_to" [disabled]="!ALLREQUEST.isEditable">
<option *ngFor="let DepartmentDTO of depts " [value]="DepartmentDTO.dept_id">
<tr *ngIf="DepartmentDTO.dept_id!=ALLREQUEST.dept">{{DepartmentDTO.dept_name}}</tr>
</option>
</select>
</td>
<td>
<button (click)="ALLREQUEST.isEditable=!ALLREQUEST.isEditable" *ngIf="!ALLREQUEST.isEditable" class="btn btn-primary">Action</button>
<button *ngIf="ALLREQUEST.isEditable" (click)="transferToken(ALLREQUEST.qid,ALLREQUEST.transfer_to)" class="btn btn-primary">Save</button>
</td>
</tr>
</tbody>
</table>
i got list
now how can i put it in selection list?
getDept(indx:number){
let array1= this.depts;
let array2= this.AllRequest;
var array = array2.map(function(e) {
var f = array1.filter(a => a.dept_id != e.dept && a.dept_id != 0);
return f.map(function(a) {return a.dept_name;}); //? f.dept_id : 0
});
console.log(array[indx]);
}
There are two such models:
note.model.ts:
export class Note{
constructor(
public note_id?:number,
public note_text?: string,
public years?: number
) { }
}
year.model.ts:
export class Year{
constructor(
public years?:number
) { }
}
How can I choose the date (the Year model) from the list to display only the information in the table that matches the date with the selected from select?
<select>
<option type="number" *ngFor="let year of years">
{{ year.years }}
</option>
</select>
<div>
<table>
<thead>
<tr>
<th>Note</th>
<th>Year</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let note of notes">
<td>{{note.note_text}}</td>
<td>{{note.years}}</td>
</tr>
</tbody>
</table>
</div>
In your HTML you can add a click event:
<select>
<option type="number" *ngFor="let year of years" (click)="onYearSelection(year.years)">
{{ year.years }}
</option>
</select>
and iterate through a filtered notes list:
<div>
<table>
<thead>
<tr>
<th>Note</th>
<th>Year</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let note of filteredNotes">
<td>{{note.note_text}}</td>
<td>{{note.years}}</td>
</tr>
</tbody>
</table>
</div>
And in your ts file you can write code to filter the notes list on the basis of the selected year.
filteredNotes = [];
onYearSelection(year){
this.filteredNotes = []
this.filteredNotes = this.notes.filter(note => note.years == year);
}