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);
}
}
Related
I have a jquery code that clones a "selector" wherein it is a parent of many other elements.
EDIT : I added the function that calls the "cloneMore". It supposed to be called when the user clicks the button inside the row and creates another row below it.
EDIT 2 : I added the table that the <tr> belongs to. I tried to run it without the <tr> <td> tags and the function works! But sadly it removes it from the html table. Why does this happen?
jquery snippet
$(document).on('click', '.add-form-row', function(e){
alert("Button Click!");
e.preventDefault();
cloneMore('.form-row.spacer:last', 'form');
return false;
function cloneMore(selector, prefix) {
var newElement = $(selector).clone(true);
newElement.find('input[type=text]').each(function() { //loops through the textfields
console.log("print1");
});
newElement.each(function () {
console.log("print2");
});
html code
<div class="display_table">
<table class="table table-hover table-striped">
<thead class="thead-dark">
<tr class="tablerow">
<th scope="col">Item</th>
<th scope="col">Item Description</th>
<th scope="col">Quantity</th>
<th scope="col">Delete</th>
</tr>
</thead>
<tbody>
{{ formset.management_form }}
{% for form in formset %}
<div class="row form-row spacer">
<tr scope="row">
<div class="input-group">
<td><input type="text" name="form-0-item" class="form-control" id="id_form-0-item" value="" readonly="readonly"></td>
<td><input type="text" name="form-0-description" class="form-control" id="id_form-0-description" value="" readonly="readonly"></td>
<td><input type="text" name="form-0-quantity" class="form-control" id="id_form-0-quantity" value="" readonly="readonly"></td>
<div class="input-group-append"><td><button class="btn btn-success add-form-row">+</button></td></div>
</div>
</tr>
</div>
{% endfor %}
</tbody>
</table>
</div>
However, it doesn't even pass through the loop even once and I don't get a "print" in the console. I'm sure that there is a text field inside the parent. In this case its the div with class "row form-row spacer"
Is there something wrong with my syntax? I've seen somewhere where they get the parent through a selector but in my case I put it in a variable. Is there anything wrong or any work around this?
I think I kind of figured it out. I was trying to select the <input> tags even without referencing the <td> and <tr> tags. So jquery didn't know what I was trying to select. So after I made some changes in the whole table and the jquery script, it works just as I intended it would. However, any suggestions on how I could improve this code will be appreciated! I'm putting this up in case someone needs it.
Also, I forgot to credit to this Stack Overflow answer for the snippets.
html code
<table class="table table-hover table-striped" id="part_table">
<thead class="thead-dark">
<tr class="tablehead">
<th scope="col">Item</th>
<th scope="col">Item Description</th>
<th scope="col">Quantity</th>
<th scope="col">Delete</th>
</tr>
</thead>
<tbody>
{{ formset.management_form }}
{% for form in formset %}
<div class="row form-row">
<tr class="tablerow">
<td><input type="text" name="form-0-item" class="form-control" id="id_form-0-item" value="" readonly="readonly"></td>
<td><input type="text" name="form-0-description" class="form-control" id="id_form-0-description" value="" readonly="readonly"></td>
<td><input type="text" name="form-0-quantity" class="form-control" id="id_form-0-quantity" value="" readonly="readonly"></td>
<td><button class="btn btn-success add-form-row">+</button></td>
</tr>
</div>
{% endfor %}
</tbody>
</table>
jquery snippet
<script type="text/javascript">
function cloneMore(prefix) {
var lastrowtbl = $('#part_table tr.tablerow:last').clone(true);
var total = $('#id_' + prefix + '-TOTAL_FORMS').val();
var currentnum = total - 1
lastrowtbl.find('input[type=text]').each(function (i, el) {
console.log(this);
var name = $(this).attr('name');
if(name) {
name = name.replace('-' + currentnum + '-', '-' + total + '-');
var id = 'id_' + name;
$(this).attr({'name' : name, 'id' : id}).val('').removeAttr('checked');
}
});
total++;
$('#id_' + prefix + '_TOTAL_FORMS').val(total);
$('#part_table tr.tablerow:last').after(lastrowtbl);
var conditionRow = $('#part_table tr.tablerow:not(:last)');
conditionRow.find('.btn.add-form-row')
.removeClass('btn-success').addClass('btn-danger')
.removeClass('add-form-row').addClass('remove-form-row')
.html('-');
return false;
}
$(document).on('click', '.add-form-row', function(e){
alert("Button Click!");
e.preventDefault();
cloneMore('form');
return false;
});
</script>
I need help in the following scenario: Let say if we created 10 rows for below HTML Then how we'll get all 10 rows Inserted First Name and Last Name Value on FORM SUBMIT in angular 6?
<form name="userForm">
<table>
<tr *ngFor="let item of itemList; let in = index">
<td><input type="text" name="lastname-{{in}}" [(ngModel)]="item.lastname"></td>
<td><input type="text" name="middlename-{{in}}" [(ngModel)]="item.middlename"></td></tr></table></form>
You can use itemList on submit of form.
<form name="userForm">
<table>
<tr *ngFor="let item of itemList; let in = index">
<td><input type="text" name="lastname-{{in}}" [(ngModel)]="item.lastname">
</td>
<td><input type="text" name="middlename-{{in}}" [(ngModel)]="item.middlename">
</td>
</tr>
<input type="button" (click)="onSubmit()" value="submit">
</table>
</form>
<pre>
{{this.itemList | json}}
</pre>
And in ts code..
itemList = [{
lastname: "",
middlename: ""
},
{
lastname: "",
middlename: ""
},
{
lastname: "",
middlename: ""
}]
constructor() { }
onSubmit() {
console.log(JSON.stringify(this.itemList));
}
Is it possible to get the date and time when the user click the submit button in angular? For example i have a form with name and email input. When user done filling the input and click submit, the date and time will be showed to the table along with name and email. Here some snippets:
//app.service.ts
insertData(data: App) {
this.dataList.push({
name: data.name,
email: data.email,
});
}
//app.component.ts
onSubmit(form: NgForm) {
this.AppService.insertData(this.AppService.selectedData);
}
<!--app.component.html-->
<form #dataForm="ngForm" (ngSubmit)="onSubmit(dataForm)">
<label>Name</label>
<input class="form-control" name="nama" #nama="ngModel" [(ngModel)]="AppService.selectedData.name"><br>
<label>Email</label>
<input class="form-control" name="email" #email="ngModel" [(ngModel)]="AppService.selectedData.email"><br>
<button type="submit">Submit</button>
</form>
<table>
<tr>
<th>Name</th>
<th>Email</th>
<th>Time submitted</th>
</tr>
<tr *ngFor="let data of data">
<td>data.name</td>
<td>data.email</td>
<td>Time submitted</td>
</tr>
</table>
Just do :
this.dataList.push({
nama: data.nama,
email: data.email,
time : new Date(),
});
This will store the current time when you are inserting data and then use simply on the template side.
{{data.time | date: 'dd/MM/yyyy'}}
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 {
}
guys I did a code for knowing if my checkbox is checked and works fine:
HTML:
<div ng-app>
<div ng-controller="UserController">
<table id="tblUsers" class="Table">
<thead>
<tr>
<th></th>
<th>Name</th>
<th>Last Name</th>
<th>Phone</th>
</tr>
<tr ng-repeat="user in users">
<td><input type="checkbox" ng-model="user.checked"/></td>
<td>{{user.name}}</td>
<td>{{user.lastName}}</td>
<td>{{user.phone}}</td>
</tr>
</table>
</div>
</div>
This is the Angularjs code:
function UserController($scope) {
$scope.users = [
{ checked: false, name: 'Jorge', lastName: 'Martinez', phone: 012345 },
{ checked: false, name: 'Juan', lastName: 'Perez', phone: 78964 }
];
$scope.updateUser = function () {
angular.forEach($scope.users, function (user) {
if (user.checked) {
user.name = $scope.nameUpdate;
user.lastName = $scope.lastNameUpdate;
user.phone = $scope.phoneUpdate;
}
});
};
}
The problem is when I change the checkbox for a radio button:
<td><input type="radio" ng-model="user.checked"/></td>
When I do this, this value "if(user.checked)" appears as "undefined".
Some one that knows how to fix this or how to know if the radio button is checked or not in the AngularJS controller.
see https://docs.angularjs.org/api/ng/input/input%5Bradio%5D
you'll want to have multiple radio buttons, each with it's own value to set some property to (although this is weird for a yes no you are better off with a checkbox, but if you had multiple values this is how radio buttons work)
<input type="radio" ng-model="user.checked" ng-value="true" />Yes
<input type="radio" ng-model="user.checked" ng-value="false" />No
when you have just one radio button, if it is not checked by default or you have no ng-value it will be undefined, but also having only one radio button, you could never unselect it.
You need to give the radio button a value.
<td><input type="radio" ng-model="user.checked" value="true"/></td>
<td><input type="radio" ng-model="user.checked" value="false"/></td>
Working Fiddle