Convert table cell to editable input - html

I want to create a table with 2 columns: Name, Email. Everytime I press the edit button, I want to transform the td into editable inputs. The problem is that if I have more users and I press the edit button, all users will become editable, not just the selected one. How can I solve this problem?
<table class="table table-hover">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Email</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let user of usersList">
<td *ngIf="!editUser">{{ user.name }}</td>
<td *ngIf="editUser"><input class=" form-control" size="1" type="text" [(ngModel)]="user.name"></td>
<td *ngIf="!editUser">{{ user.email }}
<td *ngIf="editUser"><input class=" form-control" size="1" type="text" [(ngModel)]="user.email"></td>
<td *ngIf="!editUser">
<a class="action-btn" (click)="onEdit()">
<p class="material-icons pointer">edit</p>
</a>
</td>
</tr>
</tbody>
</table>
editUser: boolean = false
onEdit() {
this.editUser = !this.editUser
}
How the table looks before pressing the red button
How the table looks after pressing the button
Thank you for your time! (this is what I want to achieve

Do you have an id for the user?
Then you could do something like:
<tbody>
<tr *ngFor="let user of usersList">
<td *ngIf="editUserId !== user.id">{{ user.name }}</td>
<td *ngIf="editUserId === user.id"><input [(ngModel)]="user.name"></td>
<td *ngIf="editUserId !== user.id">{{ user.email }}
<td *ngIf="editUserId === user.id"><input [(ngModel)]="user.email"></td>
<td *ngIf="editUser !== user.id">
<a class="action-btn" (click)="onEdit(user.id)">
<p class="material-icons pointer">edit</p>
</a>
</td>
</tr>
</tbody>
and
editUserId: number;
onEdit(userId: number) {
this.editUserId = userId;
}

Try this, it will work for you.
<table class="table table-hover">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Email</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let user of usersList; index as i">
<td *ngIf="i!=selectedRowIndex">{{ user.name }}</td>
<td *ngIf="selectedRowIndex == i"><input class=" form-control" size="1"
type="text" [(ngModel)]="user.name"></td>
<td *ngIf="i!=selectedRowIndex">{{ user.email }}
<td *ngIf="selectedRowIndex == i"><input class=" form-control" size="1"
type="text" [(ngModel)]="user.email"></td>
<td>
<a class="action-btn" (click)="onEdit(i)">
<p class="material-icons pointer">edit</p>
</a>
</td>
</tr>
</tbody>
</table>
selectedRowIndex = -1
onEdit(rowIndex) {
this.selectedRowIndex = rowIndex;
}

Related

Submit selected option in a scrollable <select> in Angular

So here's my html code:
<div class="container">
<div>
<h1 class="table-title">Employees</h1>
<table class="table">
<thead class="thead-dark">
<tr>
<th>User Name</th>
<th>Email</th>
<th>Role</th>
<th>References</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let tempEmployee of employees">
<td class="align-middle">{{ tempEmployee.userName }}</td>
<td class="align-middle">{{ tempEmployee.email }}</td>
<td class="align-middle">{{ tempEmployee.roleId }}</td>
<td class="align-middle">
<a routerLink="/projects">Assigned Projects</a>
<br />
Assigned Tickets
</td>
</tr>
</tbody>
</table>
</div>
<div>
<h4>Select 1 or multiple users</h4>
<select id="user-list" multiple size="employees.length" >
<option
*ngFor="let employee of employees; let indexOfEmployee=index;"
value="indexOfEmployee"
>{{ employee.userName }}</option>
</select>
<button id="submit" type='submit' class="btn btn-primary" (click)="onSubmit()">Submit</button>
</div>
</div>
I'm confused on how I can know if an option is selected so that in the component on the onSubmit() function i can check if an option is selected and submit it. Also multiple options can be selected.
I tried to console.dir the option element to see if it has a selected property but maybe i'm missing something.
some quick and easy javascript:
for(var i = 0; i < document.getElementById("user-id").length; i++){
if(document.getElementsByTagName("option")[i].selected == true){
console.log("selected")
}
}

'How to add' mouseover and mouseout on a <tr>?

I've got a table that I need three different links to show up on the specific row that the mouse is hovering over. The links are "View", "Edit", and "Trigger Now".
I need this event to happen only on the sessions table.
Here's my current HTML:
<div>
<h1>Manage Workflows</h1>
</div>
<div class="tablez">
<table class="table table-hover" id = "groups">
<thead>
<tr>
<th scope="col">Groups</th>
</tr>
</thead>
<tbody>
<tr *ngFor = "let row of rows" (click) = "onRowClick(row.id)">
<td class="data">{{row.group}}</td>
</tr>
</tbody>
</table>
<table class="table" id = "sessions">
<thead>
<tr>
<th scope="col">Sessions</th>
<th scope="col" id = "trigger">Next Trigger</th>
</tr>
</thead>
<tbody>
<tr *ngFor = "let row of tableTwo">
<td scope="row">{{row.session}}</td>
<td scope="row" id = "trigger" >{{row.nextTrigger}}</td>
</tr>
</tbody>
</table>
</div>
You can use mouseover event of the angular
<tr *ngFor = "let row of tableTwo" (mouseover)="showLinks=true" (mouseout)="showLinks=false" >
<td scope="row">{{row.session}}</td>
<td scope="row" id = "trigger" >{{row.nextTrigger}} </td>
<td *ngIf="!showLinks" scope="row"> View Edit Trigger Now links </td>
</tr>

TABLE HTML -> How can I change the way it is done?

I am trying to make a table with checkboxes on every row of the table.
I got an example(Code Example link), but I am not being able to get it done. I do not want to use the "data-url" as a source of the table. Except that, everything is accordingly with what I need.
I want to feed the "tbody" by myself.
Here is the example I am following: Code Example
What I want to accomplish:
<tbody>
<tr>
<!-- I do not know what to put over here in order to get it working-->
<td data-field="state" data-checkbox="true"></th>
<td>Foo</td>
<td>666</td>
<td>6969</td>
<td>Let there be rock</td>
</tr>
</tbody>
<!-- UPDATING CODE! -->
<table data-toggle="table" data-click-to-select="true" class="table">
<thead>
<tr>
<th class="bs-checkbox " data-field="state">
<div class="th-inner">
<input name="btSelectAll" class="selectall" type="checkbox">
</div>
<div class="fht-cell"></div>
</th>
<th data-field="name">Name</th>
<th data-field="stargazers_count">Stars</th>
<th data-field="forks_count">Forks</th>
<th data-field="description">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="bs-checkbox ">
<input data-index="0" name="btSelectItem" type="checkbox">
</td>
<td style="">bootstrap-table</td>
<td style="">526</td> <td style="">122</td>
<td style="">Huehuehue</td>
</tr>
<tr>
<td class="bs-checkbox ">
<input data-index="0" name="btSelectItem" type="checkbox">
</td>
<td style="">bootstrap-table</td>
<td style="">528</td> <td style="">122</td>
<td style="">huehuheheuhe</td>
</tr>
</tbody>
</table>
UPDATE NOTES
Now I can feed the "tbody" by myself (thanks to #Adriano Silva), but the "select all" checkbox is not working.
DONE
Example of the solution given by #AdrianoSilva:
JSFiddle Example
You can insert a row as example below
<table data-toggle="table" data-click-to-select="true" data-url="none">
<thead>
<tr>
<th class="bs-checkbox " data-field="state">
<div class="th-inner">
<input name="btSelectAll" type="checkbox">
</div>
<div class="fht-cell"></div>
</th>
<th data-field="name">Name</th>
<th data-field="stargazers_count">Stars</th>
<th data-field="forks_count">Forks</th>
<th data-field="description">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="bs-checkbox ">
<input data-index="0" name="btSelectItem" type="checkbox">
</td>
<td style="">bootstrap-table</td>
<td style="">526</td> <td style="">122</td>
<td style="">An extended Bootstrap table with radio, checkbox, sort, pagination, and other added features. (supports twitter bootstrap v2 and v3)</td>
</tr>
</tbody>
</table>

How to count checked checkboxes (not in itemgroup)

i have a question. Did not found answer before, or it wasn't exacly my problem.
I'm building static app, and i need to have table like this on the page. (They are just binded in the angular... I'm new in it and i have stuck.
<table class="table table-responsive table-striped">
<thead>
<tr>
<th class="text-center"></th>
<th class="text-center">1st</th>
<th class="text-center">2nd</th>
<th class="text-center">3rd</th>
<th class="text-center">4th</th>
<th class="text-center">5th</th>
<th class="text-center">6th</th>
</tr>
</thead>
<tbody>
<tr>
<td>First header</td>
<td class="text-center"><input type="checkbox" ng-model="myModel.myData[1].attribute1" /></td>
<td class="text-center"><input type="checkbox" ng-model="myModel.myData[1].attribute2" /></td>
<td class="text-center"><input type="checkbox" ng-model="myModel.myData[1].attribute3" /></td>
<td class="text-center"><input type="checkbox" ng-model="myModel.myData[2].attribute1" /></td>
<td class="text-center"><input type="checkbox" ng-model="myModel.myData[2].attribute2" /></td>
<td class="text-center"><input type="checkbox" ng-model="myModel.myData[2].attribute3" /></td>
</tr>
And it goes further... (ie. myModel.myData[3] (with 3 attributes), myModel.myData[4] (with 3 attributes) , myModel.myData[5] (with 3 attributes) )
Now... i have to count:
How much attributes1 (with whole myModel.MyData[1 to N]) are selected
How much attributes2 (with whole myModel.MyData[1 to N]) are selected
And so on...
All i got now, that i can accces to the value, by: {{ myModel.myData[1].attribute1 }}
But it returns me true/false (that is good) - but can't acces to this in controller. (My controller js file is almost clear, so i don't put it here)
If some1 could help me, where i can search solution i would be thankfull.
Look this:
when checkbox is clicked, this call the function for populate array with data of this. After you can get lenght of array using lenght.
var app = angular.module('StarterApp', []);
app.controller('AppCtrl', function($scope){
$scope.myModel = {myData1: [], myData2: [], myData3: []};
$scope.getChecked = function(){
console.log($scope.myModel.myData1);
console.log($scope.myModel.myData2);
console.log($scope.myModel.myData3);
}
$scope.toggleSelection = function (data, value) {
var idx = data.indexOf(value);
// is currently selected
if (idx > -1) {
data.splice(idx, 1);
}
// is newly selected
else {
data.push(value);
}
};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<table ng-app="StarterApp" ng-controller="AppCtrl" class="table table-responsive table-striped">
<thead>
<tr>
<th class="text-center"><button type="button" ng-click="getChecked()"> Get checked </button></th>
<th class="text-center">1st</th>
<th class="text-center">2nd</th>
<th class="text-center">3rd</th>
<th class="text-center">4th</th>
<th class="text-center">5th</th>
<th class="text-center">6th</th>
<th class="text-center">7th</th>
<th class="text-center">8th</th>
<th class="text-center">9th</th>
</tr>
</thead>
<tbody>
<tr>
<td>
First header
</td>
<td class="text-center">
<input type="checkbox" ng-click="toggleSelection(myModel.myData1, 'check1')"/>
</td>
<td class="text-center">
<input type="checkbox" ng-click="toggleSelection(myModel.myData1, 'check2')"/>
</td>
<td class="text-center">
<input type="checkbox" ng-click="toggleSelection(myModel.myData1, 'check3')"/>
</td>
<td class="text-center">
<input type="checkbox" ng-click="toggleSelection(myModel.myData2, 'check1')"/>
</td>
<td class="text-center">
<input type="checkbox" ng-click="toggleSelection(myModel.myData2, 'check2')"/>
</td>
<td class="text-center">
<input type="checkbox" ng-click="toggleSelection(myModel.myData2, 'check3')"/>
</td>
<td class="text-center">
<input type="checkbox" ng-click="toggleSelection(myModel.myData3, 'check1')"/>
</td>
<td class="text-center">
<input type="checkbox" ng-click="toggleSelection(myModel.myData3, 'check2')"/>
</td>
<td class="text-center">
<input type="checkbox" ng-click="toggleSelection(myModel.myData3, 'check3')"/>
</td>
</tr>
</tbody>
</table>
If you need the count in your controller, then i guess you need to loup through the checkboxes. As in
$scope.attribute1Counter = 0;
angular.forEach($scope.myModel.myData, function(data){
if(data.attribute1 === true){
$scope.attribute1Counter++
}
});
Pull underscore.js into your project and use _.where (http://underscorejs.org/#where) to get the count in your controller.

How to create different section in angular template (html file) conditionally

I am constructing three different table as following.
There is a lot of repetitive code in it.
The only different in each table is studentPermissions[] array which contain three objects for each student. So under each table i am just changing it for each student like studentPermissions[0], studentPermissions[1] and studentPermissions[2].
Is there any further better i can write same code in less no of lines?
<div class="module-container">
<div>
<table>
<thead>
<tr >
<td>Student</td>
<td>{{studentPermissions[0].SubjectName}}</td>
</tr>
<tr>
<th></th>
<th ng-repeat="permission in permissions"><div><span>{{permission.Name}}</span></div></th>
</tr>
</thead>
<tbody>
<tr ng-repeat="student in studentPermissions[0].Students">
<td>{{student.FirstName}} {{student.LastName}} <small class="" style="color: #999999;">({{student.Role}})</small></td>
<td ng-repeat="permission in student.Permissions">
<input ng-model="permission.Checked" type="checkbox">
</td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<thead>
<tr >
<td>Student</td>
<td>{{studentPermissions[1].SubjectName}}</td>
</tr>
<tr>
<th></th>
<th ng-repeat="permission in permissions"><div><span>{{permission.Name}}</span></div></th>
</tr>
</thead>
<tbody>
<tr ng-repeat="student in studentPermissions[1].Students">
<td>{{student.FirstName}} {{student.LastName}} <small class="" style="color: #999999;">({{student.Role}})</small></td>
<td ng-repeat="permission in student.Permissions">
<input ng-model="permission.Checked" type="checkbox">
</td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<thead>
<tr >
<td>Student</td>
<td>{{studentPermissions[2].SubjectName}}</td>
</tr>
<tr>
<th></th>
<th ng-repeat="permission in permissions"><div><span>{{permission.Name}}</span></div></th>
</tr>
</thead>
<tbody>
<tr ng-repeat="student in studentPermissions[2].Students">
<td>{{student.FirstName}} {{student.LastName}} <small class="" style="color: #999999;">({{student.Role}})</small></td>
<td ng-repeat="permission in student.Permissions">
<input ng-model="permission.Checked" type="checkbox">
</td>
</tr>
</tbody>
</table>
</div>
</div>
I was not sure about question title so please feel free to update it to make it more relevant with question contents. thanks
Also, I have to show data for each customer in septate table. this is requirement
Why not just use another ng-repeat?
<div class="module-container">
<div ng-repeat="studentPermission in studentPermissions">
<table>
<thead>
<tr >
<td>Student</td>
<td>{{studentPermission.SubjectName}}</td>
</tr>
<tr>
<th></th>
<th ng-repeat="permission in permissions"><div><span>{{permission.Name}}</span></div></th>
</tr>
</thead>
<tbody>
<tr ng-repeat="student in studentPermission.Students">
<td>{{student.FirstName}} {{student.LastName}} <small class="" style="color: #999999;">({{student.Role}})</small></td>
<td ng-repeat="permission in student.Permissions">
<input ng-model="permission.Checked" type="checkbox">
</td>
</tr>
</tbody>
</table>
</div>
</div>