I had succeed on creating a table and implement datatable on my angular 5 project
html code for the table:
<table class="table table-hover mt-3" datatable [dtOptions]="dtOptionsContent" [dtTrigger]="dtTriggerContent">
<thead>
<tr>
<th scope="col">Content Type</th>
<th scope="col">User Occupation</th>
<th scope="col">Content Name</th>
<th scope="col">Carrot</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let reward of rewards" >
<td data-toggle="modal" data-target="#seeDetail" (click)="onSelect(reward)">{{getRewardType(reward.type)}}</td>
<td data-toggle="modal" data-target="#seeDetail" (click)="onSelect(reward)">{{getRoleType(reward.role)}}</td>
<td data-toggle="modal" data-target="#seeDetail" (click)="onSelect(reward)">{{reward.name}}</td>
<td data-toggle="modal" data-target="#seeDetail" (click)="onSelect(reward)">{{reward.carrot}}</td>
</tr>
</tbody>
</table>
.ts code for the datatable settings:
dtOptionsContent: DataTables.Settings = {};
dtTriggerContent: Subject<any> = new Subject();
#ViewChild(DataTableDirective)
dtElementContent: DataTableDirective;
.ts for get rewards:
getRewards(): void
{
this.adminService.getRewards().subscribe(rewards=> {
this.rewards = rewards;
this.dtTriggerContent.next();
});
}
The sorting, pagination, and searching are functional and when i add/delete a content to the table , the table is updated.
However, after updated, when i tried the sorting and pagination, the table content revert to the pre-added/deleted list.
I've tried destroy and rerender the table. This is the .ts code for adding content:
addSocial(role: number, rewardName: string, carrot: number): void
{
this.dtElementContent.dtInstance.then((dtInstance: DataTables.Api) => {
let currentCarrot = 0;
if(!isNaN(carrot))
{
//Setting for rewardTemp goes here
this.adminService.addReward(this.rewardTemp)
.subscribe(reward => {
dtInstance.destroy();
this.rewards.push(reward);
this.dtTriggerContent.next();
});
//Success Notification goes here
}
else
//Error Notification goes here
});
}
while it succeed to sorting the updated content after the list was updated, it spawn warning alert after rerender.
The alert contains "DataTables warning: table id=DataTables_Table_1 - Cannot reinitialise DataTable. For more information about this error, please see http://datatables.net/tn/3"
What is wrong??
Related
I am working on a dummy API with jsonplaceholder, I am getting all posts after clicking button, but i wanna get userid and title on load,and after clicking title need to get that particular body of that id How can i achieve this.
in jasonplaceholder there are 100 posts with id,title,body. on clicking getdata i need only id and title after when I click on title I need to get body as either in paragraph or popup or something.
html
<button (click)="get()">Get Data</button>
<div class="col-md mt-4 mb-4">
<table class="table table-bordered">
<thead>
<tr>
<th>id</th>
<th>title</th>
<th>body</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let d of data">
<td><span>{{d.id}}</span></td>
<td><span>{{d.title}}</span></td>
<td><span>{{d.body}}</span></td>
</tr>
</tbody>
</table>
</div>
ts
get(){
this.userservice.getData().subscribe((data) =>{
console.log(data)
this.data = data
})
}
service
getData():Observable<any>{
const url = "https://jsonplaceholder.typicode.com/posts";
return this.http.get(url)
}
A simple solution would be to use *ngIf and a variable, eg. selectedId
<td (click)="showBody(d.id)"><span>{{d.title}}</span></td>
<td *ngIf="selectedId === d.id"><span>{{d.body}}</span></td>
Then in your component, initiate it:
selectedId: any = null;
and
showBody(id: number){
this.selectedId ? this.selectedId = null : this.selectedId = id;
}
so that it would hide the body if you click on the title again.
As I understand, you need to get the id of the element you clicked on it to show the element's body.
so to achive this, you can write something like this:
component.html
<tr *ngFor="let d of data" (click)="setBody(d)">
<td><span>{{d.id}}</span></td>
<td><span>{{d.title}}</span></td>
</tr>
<!-- Show selected item body here -->
<h1>Selected item body</h1>
<p>{{selectedItemBody}}</p>
component.ts file
[Optional]: you can define the POST interface top of your ts file to have more clean code:
interface POST {
id: number;
title: string;
body: string;
}
setBody(post: POST) {
this.selectedItemBody = post.body;
alert(this.selectedItemBody); // you can remove this alert
}
I provided you this sample working code here in stackblits
hello I need to retrieve the values entered by a form after the post method so I inserted at the service level this code :
list:Client[];
constructor(private http:HttpClient) { }
postClient(formData:Client){
return this.http.post(this.rootURL+'/Clients/',formData);
}
putClient(formData:Client){
return this.http.put(this.rootURL+'/Clients/'+formData.Engagement,formData);
}
getClient(formData:Client){
return this.http.get(this.rootURL+'/Clients/GetClientByName/'+formData.Engagement);
}
and at the component level like this :
getClient(form:NgForm){
this.clientservice.getClient(form.value).subscribe(
res =>{this.client = res as Client}
)
}
and in the HTML code this:
<table class="table table-hover">
<tr>
<th class="tname">Client</th>
<th class="tname">Enagement</th>
<th class="tname">ERP</th>
</tr>
<tr *ngFor="let clt of client">
<td >{{clt.Clientname}}</td>
<td >{{clt.Engagement}}</td>
<td >{{clt.ERP}}</td>
and I can’t get the values by ID with the get I don’t know what is the problem I have neither result or error message
I think your http service(this.rootURL+'/Clients/GetClientByName/) return Client[] not Client.
So, You should cast like this.
this.clientservice.getClient(form.value).subscribe(
res =>{this.client = res as Client[]}
)
But, your response is fixed
{"Engagement":"56789","Clientname":"ClLIENT","ERP":"ERP"}
at component level, no need to edit
getClient(form:NgForm){
this.clientservice.getClient(form.value).subscribe(
res =>{this.client = res as Client}
)
}
you should edit html file.
<table class="table table-hover">
<tr>
<th class="tname">Client</th>
<th class="tname">Enagement</th>
<th class="tname">ERP</th>
</tr>
<tr>
<td >{{client.Clientname}}</td>
<td >{{client.Engagement}}</td>
<td >{{client.ERP}}</td>
</tr>
---------------------------
or need to use ngFor loop, edit component level. and no need to edit component level.
client: Client[] = [];
getClient(form:NgForm){
this.clientservice.getClient(form.value).subscribe(
res =>{
const cli = res as Client;
this.client.length = 0;
Array.prototype.push.apply(this.client, cli)
}
)
}
So I have this, and what I want to get is the ID or Key from each user.
For example: WxA2XLigx7V1bOxm5WNSnVkgtOu1
And I'm currently showing this so far:
This is my current code that shows the table
firebase.database().ref('Users/').on('value',(data)=>{
let Users = data.val();
document.getElementById('tablaUsers').innerHTML+='';
for (const user in Users){
document.getElementById('tablaUsers').innerHTML+=`
<tr>
<td>${Users[user].Key}</td>
<td>${Users[user].email}</td>
<td>${Users[user].name}</td>
</tr>
`;
}
And this is the code from the html
<table class="mdl-data-table mdl-js-data-table">
<thead>
<tr>
<th class="mdl-data-table__cell--non-numeric" role="columnheader" scope="col">ID</th>
<th class="mdl-data-table__cell--non-numeric" role="columnheader" scope="col">Email</th>
<th class="mdl-data-table__cell--non-numeric" role="columnheader" scope="col">Nombre</th>
</tr>
</thead>
<tbody id="tablaUsers">
<tr>
<td class="mdl-data-table__cell--non-numeric"></td>
<td class="mdl-data-table__cell--non-numeric"></td>
<td class="mdl-data-table__cell--non-numeric"></td>
</tr>
</tbody>
</table>
As you see my
<td>${Users[user].Key}</td>
Is not working, it's just a placeholder. It may be a simple problem but I can´t get it or how to do it, hope anyone can help.
You should loop on the JavaScript object returned by the val() method, as follows:
firebase
.database()
.ref('users/')
.on('value', (data) => {
var obj = data.val();
Object.keys(obj).forEach((key) => {
console.log('key: ' + key);
console.log('mail: ' + obj[key].mail);
console.log('name: ' + obj[key].name);
});
});
In my MVC 5 Web App I use footable. In a table I have fields radio buttons for example that are built the form.
here is the code:
<thead>
<tr>
<th></th>
#foreach (var labelfreqdep in ViewBag.rbFreqDep)
{
<th style="text-align:center;" data-breakpoints="xs sm">
<label for="FrequencyID">#labelfreqdep.FrequencyDescription</label>
</th>
}
<th data-breakpoints="xs sm"></th>
</tr>
</thead>
<tbody>
<tr>
<td>First Chooses</td>
#foreach (var freqdep in ViewBag.rbFreqDep)
{
<td class="rd">
<input type="radio" name="A3_1" id="A3_1" value="#freqdep.FrequencyDescription" data-val="true" data-val-required="Please select">
</td>
}
<td class="rd">#Html.ValidationMessageFor(model => model.A3_1, "", new { #class = "text-danger" })</td>
</tr>
</tbody>
</table>
I also use validation for empty fields with data-val="true". The problem is that doesn't display the "Please select" message in mobile view. It works ok for desktop view. When I have expanded the tr with the plus icon the message appears. Can we just expand the rows on submit so the message to be displayed?
Any idea?
Edited
I used
$("#survey_form").submit(function (event) {
$('.surveytable').footable({
"expandAll": true
});
});`
It expands the rows on submit but it doesn't show the validation errors
For anyone who has the same issue, I solved it by giving an id to the button and then handle it through jquery
<script type="text/javascript">
jQuery(function($){
$('.surveytable').footable()
$("#survey_btn").click(function (event) {
$('.surveytable').footable({
"expandAll": true
});
});
});
</script>
I am using knockout binding to bind some data into html tables. My knockout view Model had multiple products and each product will have multiple chars. I want to display the products in one table and when i select the link "show chars" it should display the corresponding chars in below table.
This is my View Model
var ProductViewModel = function(items) {
this.items = ko.observableArray(items);
this.itemToAdd = ko.observable("");
this.addItem = function() {
if (this.itemToAdd() != "") {
this.items.push(this.itemToAdd());
this.itemToAdd("");
}
}.bind(this);
};
And this is my html tables
<div id="productTable">
<table class="ui-responsive table">
<thead>
<tr>
<th >Product Name</th>
<th >Description</th>
<th >Parent?</th>
</tr>
</thead>
<tbody id="pBody" data-bind="foreach: items">
<tr class="success" >
<td><span data-bind="text: name"></span>
</td>
<td><span data-bind="text: desc"></span>
</td>
<td>show chars</td>
</tr>
</tbody>
</table>
</div>
</div>
<div id="productChars">
<div id="productCharTable">
<table class="ui-responsive table">
<thead>
<tr>
<th >Char Name</th>
<th >Description</th>
<th >Length</th>
<th >Type</th>
</tr>
</thead>
<tbody id="pBody" data-bind="foreach: $data['chars']">
<tr class="success">
<td><span data-bind="text: name"></span>
</td>
<td>asdf asdfasdf</td>
<td>10</td>
<td>String</td>
</tr>
</tbody>
</table>
</div>
I am able to bind the products into first table. But for characteristics i am not sure how to achieve the same.
Could someone please help me in figuring out how to achieve the same.
Here is the jsfiddle
https://jsfiddle.net/sirisha_k/0Ln7h2bo/7/
As #supercool pointed out you can use "data-bind='with selectedItem'" to populate the second table with chars data. For that you need to add one more item into your model called selectedItem and every time you select or add a row, you point the selectedItem to that elementdata. And use "data-bind='with selecteItem'" for second table.
var ProductViewModel = function(items) {
this.items = ko.observableArray(items);
this.selectedItem = ko.observableArray();
this.itemToAdd = ko.observable("");
this.addItem = function() {
if (this.itemToAdd() != "") {
this.items.push(this.itemToAdd());
this.itemToAdd("");
}
}.bind(this);
};
and on row select call some function selectedItem($data) where $data refers to the current item.
then set that data to selectedItem in model.
function selectedItem(prod){
ProductViewModel.selectedItem(prod);
}