Mootools 1.3.2 Request.HTML fetch table rows - cakephp

First off please don't bash me for using old technology (polling) and an old version of Mootools (1.3.2) as I don't have control over these factors.
Ok here's my problem.
I have a page which refreshes every few seconds to fetch new data from the database via AJAX. Ideally the structure of the returned value should be as such:
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr>
After receiving this table row structure result, I need to append that to the current table in the page which essentially just adds a new record on the table if there are new records. If there's none, then no changes to the table are made.
Currently I am using
var req = new Request.HTML({url: url_to_get_new_rows,
onSuccess: function(html, responseHTML) {
// append table row 'html' here
}
}).send();
However, the returned value in the 'html' variable that I'm supposed to append at the end of the table only returns
1 2 3 4
This obviously is an undesired behavior as I need the tr and td elements to make it work.
I hope someone could help me with this problem.
THANKS!

Javascript:
new Request.HTML({
url:'tr.php',
onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
var tbody = document.id('tbody');
tbody.set('html', tbody.get('html') + responseHTML);
// or
var tr = new Element('table', {'html': responseHTML}).getElement('tr');
tr.inject(tbody);
}
}).get();
HTML:
<table>
<thead>
<tr>
<th>1</th>
<th>2</th>
<th>3</th>
<th>4</th>
</tr>
</thead>
<tbody id="tbody">
<tr>
<td>a</td>
<td>b</td>
<td>c</td>
<td>d</td>
</tr>
</tbody>
</table>

Related

Parsing json data and show in loop in a table

I'm developing a website with laravel where I've a json response from server with a blade.php view like below,
[{"id":1,"user_id":"1","patient_name":"kk","age":"44","sex":"Male"},
{"id":2,"user_id":"1","patient_name":"noor","age":"7","sex":"Male"},
{"id":3,"user_id":"1","patient_name":"noor","age":"44","sex":"Male"}]
How can I iterate through this json object so that I can show the data in a table with patient_name,age,sex column in blade.php?
First you would have to convert the JSON to an array with $array_data = json_decode($array, true), then you can pass the data to your view with return view('page', ["array_data"=>$array_data]);, next you would have to parse it in blade like:
<table>
<tr>
<td>id</td>
<td>User id</td>
<td>Patient name</td>
<td>Age</td>
<td>Sex</td>
</tr>
#foreach($array_data as $key=>$value){
<tr>
<td>{{$value["id"]}}</td>
<td>{{$value["user_id"]}}</td>
<td>{{$value["patient_name"]}}</td>
<td>{{$value["age"]}}</td>
<td>{{$value["sex"]}}</td>
</tr>
#endforeach
And so your code in your controller would be:
$array_data = json_decode($array, true);
return view('page', ["array_data"=>$array_data]);
Note that the string page must be the name of your blade template minus the .blade.php, i.e. if your template is called page.blade.php you would use the string page

how to fix ngtable pagination?

I have developed a ngtable with a filter. The pagination on the table does not work anymore though? When I select the show 10 rows on the bottom of the table it still shows 14 rows? How can i fix the pagination/indexing?
This is my table definition:
<table ng-table="tableParams" class="table">
<tr ng-repeat="account in $parent.filtered =(data | filter:search.accountName | filter:search.id)">
<td data-title="'id'">
{{account.account.accountId.id}}
</td>
<td data-title="'name'">
{{account.account.accountName}}
</td>
</tr>
</table>
plunkr:http://plnkr.co/edit/Rqt6px?p=preview
You need to figure pagination function by yourself. You may see ng-table's example in here.
var Api = $resource("/data");
this.tableParams = new NgTableParams({}, {
getData: function(params) {
// ajax request to api
return Api.get(params.url()).$promise.then(function(data) {
params.total(data.inlineCount); // recal. page nav controls
return data.results;
});
}
});
It first load all the data into Api. The params.url() contains current page and page count. It then use these two variable to return part of dataset. So you may need to figure out how to return this part of data.

ng-table data will be shown in one line in one array instead of multiple lines

Each click on my google Map adds new data to my array.
I have this array:
tableDATA = [{range:[],distances:[]}];
This are the push commands in the click function:
"total" and "round" are calculated integers
tableDATA[0].distances.push(total+' km');
tableDATA[0].range.push(round+' km');
This is my ng-table params code in javascript
$scope.tableParams = new ngTableParams(
{
page: 1, // show first page
count: 15, // count per page
sorting: {
distances: 'asc' // initial sorting
}
},
{
total: tableDATA[0].distances.length, // length of data
getData: function($defer, params) {
// use build-in angular filter
var orderedData = params.sorting() ?
$filter('orderBy')(tableDATA, params.orderBy()) : tableDATA;
$defer.resolve(orderedData.slice((params.page() - 1) * params.count(), params.page() * params.count()));
}
}
);
And this is my ng-table code in html
<button ng-click="tableParams.sorting({})" class="btn btn-default pull-right">Clear sorting</button>
<p><strong>Sorting:</strong> {{tableParams.sorting()|json}}</p>
<table ng-table="tableParams" class="table">
<tr ng-repeat="point in $data">
<td data-title="'real'" sortable="'real'">{{point.distances}}</td>
<td data-title="'Kombi'" sortable="'Kombi'">{{point.range}}</td>
</tr>
</table>
The entries in the array won't be shown in multiple lines (one entry per line) but in one line with all array entries:
real Kombi
line 1: ["50.32 km","50.89 km", etc.] ["44.32 km","48.65 km", etc.]
line 2: won't be created
line 3: won't be created
What's wrong? Pls help
try using an angular foreach see https://docs.angularjs.org/api/ng/function/angular.forEach
I don't come to the right solution with forEach because I have an array behind the keys and no static values.. ?!
var tableDATA_arr = [];
tableDATA = {range:[],distances:[]};
tableDATA.distances.push(total+' km');
tableDATA.range.push(round+' km');
angular.forEach(tableDATA, function(value, key) {
this.push(key + ': ' + value);
}, tableDATA_arr);
<tr ng-repeat="point in $data">
<td><tr ng-repeat="distance in point.distances">
<td data-title="'real'" sortable="'real'">{{distance}}</td>
</tr></td>
then do the same for the other column
This is my code now. The data is retrievable(!), but the data still will be written in one line -.- i hate it :D
<tr ng-repeat="point in $data">
<td ng-repeat="dist in point.distances track by $index" data-title="'real'" sortable="'real'">
{{dist}}
</td>
<td ng-repeat="range in point.range track by $index" data-title="'Kombi'" sortable="'Kombi'">
{{range}}
</td>
</tr>
I found the mistake.
First the table data array had a structure like this: tableDATA = [{range:[],distances:[]}];
There was only one object at all times and only the arrays in this one object got filled.
I changed the table data array to: tableDATA = []; and per each click I fill the array with a new object like this: tableDATA.push({range:round+' km',distances:total+' km'});
Now it works fine :)

Angular ui binding issue

I'm trying to bind a late calculated field to my ui with not much of success, This is my controller -
PaymentsController = ['$scope', 'Payment','Campaign', ($scope, Payment,Campaign)->
Payment.query().then((payments) ->
$scope.payments = payments
angular.forEach($scope.payments,(payment,index)->
payment.from = $scope.getFrom(payment)
)
The concept is simple, but the method getFrom returns a promise, when i get to this controller i load a list, the ui looks like this:
<table>
<tr>
<th ng-bind="'from'|i18n"></th>
<th ng-bind="'amount'|i18n"></th>
<th ng-bind="'to'|i18n"></th>
<th ng-bind="'description'|i18n"></th>
</tr>
<tr ng-repeat="payment in payments">
<td ng-bind="payment.from"></td>
<td>{{getAmount(payment)}}</td>
<td>{{getTo(payment)}}</td>
<td>{{getDescription(payment)}}</td>
</tr>
<tr ng-show="!payments.length">
<td ng-bind="'no_payments'|i18n"></td>
</tr>
</table>
nothing fency,
all fields are bind as expected except the from, according to controller i expect to see "asd"
but i see nothing.
I need to return a promise in this method but first I'm trying to make it work with simple code.
My problem was more conceptual I guess, I managed to fix this by making the controller control the view and not the view to control the controller, so instead of assigning a promise to the "from" field, I declared it when I had a value to put in, so my code looks like this:
angular.forEach($scope.payments, (payment, index)->
$scope.resolveFrom(payment)
)
$scope.resolveFrom = (payment)->
if payment.amount < 0
payment.from = "1"
else if payment.campaign_id
Campaign.get(payment.campaign_id)
.then((campaign)->
payment.from = campaign.name)))
payment.from = "unkown"
I'm not sure if this is the best practice, would appreciate comments/alternative solutions.

Handlebars.js - Accessing parent index from 2D array

I have a 2D array in a JSON object (called table ;)
data = {
tableID : "testTable",
table : [
[{type:'a', value:'x'},{type:'a', value:'y'},{type:'a', value:'z'}],
[{type:'a', value:'x'},{type:'a', value:'y'},{type:'a', value:'z'}],
[{type:'a', value:'x'},{type:'a', value:'y'},{type:'a', value:'z'}]
]
};
And have been successfully rendering it out with handlebars using the template:
<table id = "{{tableID}}-table">
{{#each table}}
<tr id = "{{../tableID}}-row-{{#index}}">
{{#each this}}
<td id = "{{../../tableID}}-row-{{../index}}-col-{{#index}}">
{{this.type}}-{{this.value}}
</td>
{{/each}}
</tr>
{{/each}}
</table>
However, in the td tag id I can't seem to access the parent index
{{../index}} - the index of the row. Nothing is returned:
<td id = "testTable-row--col-x">
However, I can access the index of the current context {{#index}}.
Any ideas??
Many many thanks in advance!!
Rich
p.s. Using 1.0.0-rc.3
This is an open issue/feature on handlebar. You can check the progress for the same here
However you can check the workaround here
Since Handlebars version 2.0.0, you can use
{{#../index}}

Resources