Pagination not working when Dynamic result is displayed to datatable <tbody> - json

using a datatable (bootstrap )
i am displaying the result of ajax in part of datatable . the result ged addded to datable but
pagination not working on new result as the page loads only once.Who the pagination will work on new displayed result.
Thanks in advance.
<script>
$(document).ready(function () {
$('.dataTables-example').dataTable();
});
var data = $.parseJSON(responseData);
var tbl_op ='';
$.each(data.result,function(k,v){
console.log(v.id);
$('.dataTables-example').dataTable();
tbl_op +="<tr class='odd gradeX'>"+
'<td>'+v.id+'</td>'+
'<td>'+v.country_name+'</td>'+
'<td>'+v.created+'</td>'+
'<td>'+'Edit</td>'+
'</tr>';
});
$('#country_list').html(tbl_op);
$('#Response').fadeIn(1000);
$('#Response').html(data.response_msg);
$('#Response').fadeOut(8000);
</script>
<div class="table-responsive">
<table class="table table-striped table-bordered table-hover dataTables-example" id="dataTables-example">
<thead>
<tr>
<th>Id</th>
<th>Country</th>
<th>Date</th>
<th>Action</th>
</tr>
</thead>
<tbody id='country_list'>
</tbody>
</table>
</div>
result:
<tbody id="country_list"><tr class="odd gradeX"><td>1</td><td>India</td><td>2016-09-13 11:33:30</td><td>Edit</td></tr><tr class="odd gradeX"><td>2</td><td>Usa</td><td>2016-09-13 11:33:30</td><td>Edit</td></tr><tr class="odd gradeX"><td>3</td><td>Russia</td><td>2016-09-13 11:33:30</td><td>Edit</td></tr><tr class="odd gradeX"><td>4</td><td>R1</td><td>2016-09-13 11:33:42</td><td>Edit</td></tr><tr class="odd gradeX"><td>5</td><td>R5</td><td>2016-09-13 11:34:30</td><td>Edit</td></tr><tr class="odd gradeX"><td>6</td><td>R6</td><td>2016-09-13 11:34:40</td><td>Edit</td></tr><tr class="odd gradeX"><td>7</td><td>R7</td><td>2016-09-13 11:34:48</td><td>Edit</td></tr><tr class="odd gradeX"><td>8</td><td>R8</td><td>2016-09-13 11:34:54</td><td>Edit</td></tr><tr class="odd gradeX"><td>9</td><td>R9</td><td>2016-09-13 11:35:00</td><td>Edit</td></tr><tr class="odd gradeX"><td>10</td><td>R10</td><td>2016-09-13 11:35:07</td><td>Edit</td></tr><tr class="odd gradeX"><td>11</td><td>R11</td><td>2016-09-13 11:35:13</td><td>Edit</td></tr><tr class="odd gradeX"><td>12</td><td>R12</td><td>2016-09-13 11:35:25</td><td>Edit</td></tr><tr class="odd gradeX"><td>13</td><td>R67</td><td>2016-09-13 11:37:30</td><td>Edit</td></tr></tbody>

In order to have pagination updated on XHR requests, you need to have the information about the pagination as well. That is:
Current page number
Total amount of entries
The amount of entries to show per page
This information should be returned with your XHR request. You could for example add these properties to your result object (result.currentPage, result.totalPages, result.totalEntries).
Now with each concurrent request you should send these parameters to the server, and the server should fetch the proper dataset according to these parameters.
And last but not least, with this data you can re-calculate the state of your pagination element.
Another option:
If you download the complete dataset from the server (which seems to be the case in your example), you should keep the parameters required for pagination (entriesPerPage, currentPage, totalEntries) in your JavaScript code so you can keep track of your pagination state with that and update your pagination component as such.
Please be aware that downloading all data with one XHR request could possible lead a bottleneck in your application. Imagine with a dataset with tens of thousands of records will do with your bandwidth, or even worse: with the memory consumed by JavaScript. That's why I would suggest option #1.

Related

How to use CSS grid with a dynamic number of columns and rows?

I'm trying to create a table in Angular with a dynamic number of columns and rows. I've found this easy to accomplish using the HTML table element by doing something like this:
<table class="html-table">
<tr class="headers">
<th>Name</th>
<th>Age</th>
<th>Sex</th>
<th *ngIf="columns == 4">Species</th>
</tr>
<tr>
<td>Ryan</td>
<td>30</td>
<td>M</td>
<td *ngIf="columns == 4">Human</td>
</tr>
</table>
In this example there might be some button that toggles the value of columns between 3 and 4.
Every explanation I have looked at online involves changing CSS variables, which seems like a somewhat hacky way to accomplish something that should be simple. Is there some way I can specify that something should be a new column in CSS grid rather than that having to specify the number of columns in grid-template-columns?
In Angular you can use ng-repeat to have a dynamic table
A typical example could be
<table ng-table="tableParams" class="table table-striped">
<tr ng-repeat="data in datas">
<td title="Title_Of_First_Variable">{{data.variable1}}</td>
<td title="Title_Of_Second_Variable">{{data.variable2}}</td>
<td title="Title_Of_Third_Variable">{{data.variable3}}</td>
...
</tr>
</table>
Of course with your controller you should pass your dynamic data into the correct $scope, in this case should be $scope.datas (usually an object)...maybe something like this, using NodeJS:
$http.post('route_of_your_method')
.success(function (result) {
$scope.datas = result;
})
.error(function (err) {
...
});
I explained fastly but i hope this is enough

Sinatra & Angular $HTTP.delete 404

Trying to delete from my API which is hosted on Heroku.
In Ruby file CROSS-ORIGIN is enable and I can get information from my API.
Although I have a problem when I want to make a delete request to the server.
I get an error 404 (Page not found), but when typing the url into the browser myself I can easily get this page.
cURL call works fine without any problems too.
script.js
$scope.DeleteData = function (index) {
$scope.id = $scope.companies[index].companyID;
var deleteUrl = 'https://*****.herokuapp.com/api/v1/companies/' + $scope.id;
$http.delete(deleteUrl,'DELETE').then(function(response){
console.log(response);
},function(errorResponse){
console.log(errorResponse);
});
};
Ruby.rb
delete '/companies/:companyID' do
tempCompanyID = params['companyID']
company = Company.where(companyID: tempCompanyID).first
company.destroy
end
Html:
<div ng-app="myApp" ng-controller="companyCtrl">
<table class="table table-striped table-hover">
<thead class="thead-dark">
<th>Company ID</th>
<th>Company Name</th>
<th>Address</th>
<th>City</th>
<th>Country</th>
<th>Owners</th>
<th></th>
</thead>
<tr ng-repeat="company in companies">
<td>{{company.companyID}}</td>
<td>{{company.companyName}}</td>
<td>{{company.address}}</td>
<td>{{company.city}}</td>
<td>{{company.country}}</td>
<td>{{company.owners}}</td>
<td><button class="btn" name="_method" ng-click="DeleteData($index)">Delete</button></td>
</tr>
</table>
</div>
Chrome response:
The response

angular 2 dynamic columns data table

Currently i have a data table with hard coded column headers and filling in data.. I would like to change this table to make it dynamic so the user can pick the columns to build the table. I would like to know how or in what way i will have to change my json object to ensure dynamic column data table creation.
This is what i have tried but the data is not being loaded.
<table>
<thead>
<tr>
<th *ngFor="let col of columnArray">{{col}}</th>
</tr>
</thead>
<table>
<tbody>
<tr *ngFor="let col of columnArray">
<td *ngFor="let data of columnData"> {{col.data}} </td>
</tr>
</tbody>
Currently since my data for the table comes from one object with hard coded headers here is my current working object:
data = [ {'id': 'idValue', 'name': 'nameValue', 'date': 'dateValue', 'description': 'descriptionValue'}, ...
]
but since i don't know what columns the user will pick to create the table it may be columns: id, name, description. Or columns: id, name. I need to have the data flexible so that when the user picks which ever columns to display in a table
Working format of the data:
columnArray = [ {'header': 'headerValue', 'data': 'dataValue'}, ...
]
Then the template can be:
<table>
<thead>
<tr><th *ngFor="let col of columnArray">{{col.header}}></th></tr>
</thead>
<tbody>
<tr>
<td *ngFor="let col of columnArray"> {{col.data}} </td>
</tr>
</tbody>
</table>
If you can provide the data format more apt solution can be provided.
EDIT#1:
Based on your data format, I'd extract keys from an object in your data array for headers.
headers = Object.keys(data[0]);
Then the html should be:
<table>
<thead>
<tr><th *ngFor="let col of headers">{{col}}></th></tr>
</thead>
<tbody>
<tr *ngFor="let obj of data">
<td *ngFor="let col of headers"> {{obj[col]}} </td>
</tr>
</tbody>
</table>

Receiving error I don't understand using Smartadmin Bootstrap Datatables

Hi I'm trying to implement a Datatable to a webpage that is rendered using Perl and Template Toolkit.
I'm receiving a pop-up error from the data table when it is rendered
This is the error:
DataTables warning: table id=datatable_tabletools - Requested unknown
parameter '1' for row 1. For more information about this error, please see
http://datatables.net/tn/4
I've read the documentation on this error but I'm still unsure as to why I'm receiving it
Here is the code I believe it is related to.
<table iq-datatable id="datatable_tabletools" class="table table-striped table-bordered table-hover render_me_as_datatable" width="100%">
<thead>
<tr role="row">
<th class="sorting_asc">ID</th>
<th class="sorting">thing</th>
<th class="sorting">otherthing</th>
<th class="sorting">anotherthing</th>
<th class="sorting">morething</th>
<th class="sorting">something</th>
<th class="sorting"></th>
</tr>
</thead>
<tbody>
[% FOREACH item IN list%]
<tr role="row" class="odd [% item.var%]"
[% IF item.var== "CLOSED" %]
style="background-color: lightgreen;"
[% ELSE %]
style="background-color: lightyellow;"
[% END %]>
<td class="sorting_1">[% item.var%]</td>
<td>[% item.var1%]</td>
<td>[% item.var2%]</td>
<td>[% item.var3 FILTER currency %]</td>
<td>[% item.var4%]</td>
<td>[% item.var5%]</td>
<td> <i class="fa fa-edit"></i> View </td>
</tr>
The documentation provided in the error, explains it very well.https://datatables.net/manual/tech-notes/4
Each cell in DataTables requests data, and when DataTables tries to obtain data for a cell and is unable to do so, it will trigger a warning, telling you that data is not available where it was expected to be
DataTables warning: table id={id} - Requested unknown parameter '{parameter}' for row {row-index}, column{column-index}`
where:
{id} is replaced with the DOM id of the table that has triggered the error
{parameter} is the name of the data parameter DataTables is requesting
{row-index} is the DataTables internal row index (row().index()API) for the row that has triggered the error.
{column-index} is the column data index (column().index()API) for the column that has triggered the error. The column index information was added in DataTables 1.10.10.
So to break it down, DataTables has requested data for a given row, of the {parameter} provided and there is no data there, or it is null or undefined (DataTables doesn't know, by default how to display these parameters - see below if your data does contain these values).
id in your case being datatable_tabletools
parameter in your case being 1
row-index in your case being row 1
Finally, to give you a short answer, column 1 row 1 in datatable_tabletools does not contain the data it is expecting, it is either null or empty or incorrect format.
So have a look at what the code is requesting and see what is not in the table.

React submit form to process AJAX request from checkbox input

I'm building a react contact list component that lists contacts and their data.
For one column I want checkboxes to allow the user to check off the contact he wishes to delete. For those that are checked off, the form should collect the corresponding contact's id and then pass it to an AJAX call to post those id's in a delete to the backend API. I'm stuck with what I have below and main problem is
1) building out form correctly to collect contact id's through checkboxes
[how do I wrap the checkboxes in a form when they belong to different rows in a table?]
2) passing those ids to a AJAX call
[how do I retrieve the data from a submitted form?]
render: function () {
var contacts = this.state.contacts.map(function (contact, idx) {
return (
<tr key={idx}>
<td>{contact.firstName}</td>
<td>{contact.lastName}</td>
<td>{contact.city}</td>
<td>{contact.phone}</td>
<td>{contact.email}</td>
<td><input type="checkbox" name="delete" value={contact.id}/></td>
</tr>
);
});
return(
<div>
<table>
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>City</th>
<th>Phone Number</th>
<th>Email</th>
<th>Delete</th>
</tr>
</thead>
<tbody>{contacts}</tbody>
</table>
</div>
);
}