Sorting data from API to table using jQuery - javascript
I trying to learn jquery as im new to it. here I have made a request to this example API and got an array of object that i have to list in to the table. However, im stuck on how to sort in to the table? please help me out
I have my html
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
<th>Salary</th>
</tr>
</thead>
<tbody>
<tr>
<td data-column="ID" id="_id"></td>
<td data-column="Name" id="_name"></td>
<td data-column="Age" id="_age"></td>
<td data-column="Salay" id="_salay"></td>
</tr>
</tbody>
</table>
and my script
$(document).ready(function(){
$("button").click(function(){
$.get("http://dummy.restapiexample.com/api/v1/employees", function(data, status){
if(data.status == "success"){
let listData = JSON.parse(data.data)
console.log(listData);
}
});
});
});
here i got an error as below:
Uncaught SyntaxError: Unexpected token o in JSON at position 1
at Function.parse [as parseJSON] ()
$.each(listData,function(key,value){
var trrow='<tr><td data-column="ID" id="_id">'+value["ID"]+'</td><td data-column="Name" id="_name">'+value["Name"]+'</td><td data-column="Age" id="_age">'+value["Age"]+'</td><td data-column="Salay" id="_salay">'+value["Salay"]+'</td>
</tr>';
$("tbody").append(trrow);
});
Basically you are again parsing a JSON array which is return from API.
Your API is returning the below data.
var data={"status":"success","data":[{"id":"1","employee_name":"Tiger Nixon","employee_salary":"320800","employee_age":"61","profile_image":""},{"id":"2","employee_name":"Garrett Winters","employee_salary":"170750","employee_age":"63","profile_image":""},{"id":"3","employee_name":"Ashton Cox","employee_salary":"86000","employee_age":"66","profile_image":""},{"id":"4","employee_name":"Cedric Kelly","employee_salary":"433060","employee_age":"22","profile_image":""},{"id":"5","employee_name":"Airi Satou","employee_salary":"162700","employee_age":"33","profile_image":""},{"id":"6","employee_name":"Brielle Williamson","employee_salary":"372000","employee_age":"61","profile_image":""},{"id":"7","employee_name":"Herrod Chandler","employee_salary":"137500","employee_age":"59","profile_image":""},{"id":"8","employee_name":"Rhona Davidson","employee_salary":"327900","employee_age":"55","profile_image":""},{"id":"9","employee_name":"Colleen Hurst","employee_salary":"205500","employee_age":"39","profile_image":""},{"id":"10","employee_name":"Sonya Frost","employee_salary":"103600","employee_age":"23","profile_image":""},{"id":"11","employee_name":"Jena Gaines","employee_salary":"90560","employee_age":"30","profile_image":""},{"id":"12","employee_name":"Quinn Flynn","employee_salary":"342000","employee_age":"22","profile_image":""},{"id":"13","employee_name":"Charde Marshall","employee_salary":"470600","employee_age":"36","profile_image":""},{"id":"14","employee_name":"Haley Kennedy","employee_salary":"313500","employee_age":"43","profile_image":""},{"id":"15","employee_name":"Tatyana Fitzpatrick","employee_salary":"385750","employee_age":"19","profile_image":""},{"id":"16","employee_name":"Michael Silva","employee_salary":"198500","employee_age":"66","profile_image":""},{"id":"17","employee_name":"Paul Byrd","employee_salary":"725000","employee_age":"64","profile_image":""},{"id":"18","employee_name":"Gloria Little","employee_salary":"237500","employee_age":"59","profile_image":""},{"id":"19","employee_name":"Bradley Greer","employee_salary":"132000","employee_age":"41","profile_image":""},{"id":"20","employee_name":"Dai Rios","employee_salary":"217500","employee_age":"35","profile_image":""},{"id":"21","employee_name":"Jenette Caldwell","employee_salary":"345000","employee_age":"30","profile_image":""},{"id":"22","employee_name":"Yuri Berry","employee_salary":"675000","employee_age":"40","profile_image":""},{"id":"23","employee_name":"Caesar Vance","employee_salary":"106450","employee_age":"21","profile_image":""},{"id":"24","employee_name":"Doris Wilder","employee_salary":"85600","employee_age":"23","profile_image":""}]};
let listData = JSON.parse(data.data) // replace this line
let listData=data.data;// its already JSON Array
for Example
var a=[{name:"John"},{name:"DOE"}]
JSON.parse(a) // it will give you same error which is mentioned in a question.
Related
Reference Error: Data is not defined when trying to pass a json object to java file
I am trying to pass a json object to a query the show the query results. I believe everything I have done is correct but i keep getting thrown the Reference Error: data is not defined. I have tried everything and even debugging and stepping through the program is not working for me because my break points in my javascript never seem to be getting hit; same with my java. Anyway here is my java code: #POST #Path("module") #Consumes(MediaType.APPLICATION_JSON) #Produces(MediaType.APPLICATION_JSON) public List<ModuleProcCount> getInput(int jobId) throws IOException{ try (Dbc dbc = vehmPool.getDbc()){ List<ModuleProcCount> pusher = statements.inMod(dbc, jobId); return pusher; } } Here is my javascript: $scope.sendJobId = function(cellVal) { data.jobId = cellVal; $http.post("rest/performance/module", data.jobId).then(function(response){ $scope.pusher = response.data; }); } Here is my html: <table id="Table" class="JobID-table" style="text-align:center" > <tr class="table-Header"> <th>JOB ID</th> <th>TIME FOR ALL MODULES(MILLISECONDS)</th> </tr> <tr class="jobID-Table-tr" ng-repeat="p in puller | orderBy : '-modCount'"> <td ng-click="sendJobId(p.modName)" class={{p.cellClass}}> {{p.modName}} </td> <td class={{p.cellClass}}> {{p.modCount}} </td> </tr> </table>
Angularjs : ngRepeat list down all the data from server
I successfully fetch data by using $http get from php server. But I have no idea how to display the data in Table form by using ngRepear because all the data is in few different project. I am going to display all the object of data into different row of a table. The following shows data I got from php server.
Following glimpse of code can give you idea $scope.retrievedData = []; //retrieve data from your server //take the data into above scope variable <table> <tr ng-repeat = "data in retrievedData"> <td>data.AssetDescription</td> <td>data.AssetNumber</td> <td>data.ComputerName</td> </tr> </table>
You need to add that data to controller variable: Controller function YourController($scope, $http) { $scope.tableData = []; $http.get('url').then(function(result) { $scope.tableData = result.data; }); } Template <table> <thead> <tr> <th>Description</th> <th>Computer name</th> <th>Borrow date</th> </tr> </thead> <tbody> <tr ng-repeat="row in tableData "> <td>{{row.data.AssetDescription}}</td> <td>{{row.data.ComputerName}}</td> <td>{{row.data.borrowDate}}</td> </tr> </tbody> </table>
Knockout.js: Updating objects loaded with mapping plugin
I want to render a table containing a list of objects my server is sending me. I'm currently doing this: <table> <thead> <tr> <th>id</th> <th>Name</th> <th>Status</th> </tr> </thead> <tbody data-bind="foreach: services"> <tr> <td data-bind="text: id"></td> <td data-bind="text: name"></td> <td data-bind="text: status"></td> </tr> </tbody> </table> And the Knockout.js binding part: var mappedData = komapping.fromJSON('{{{ services }}}'); ko.applyBindings({services: mappedData}); services is a variable containing JSON data and the whole page is rendered with handlebars. So far so good. I'm able to render the data received in the table. Now the problem: I'd like to receive a notification which tells me that the status of a service has changed, and update the corresponding object within mappedData. The problem is that mappedData seems pretty opaque and I'm unable to retrieve an object and update it given its id. Help appreciated!
Your mappedData variable at this point will be a knockout array with a bunch of objects that contain knockout observables. So all you have to do is change the status observable in the correct object from the array. function updateServiceStatus(id, status) { var service = mappedData().filter(function(e) { return e.id() == id; }); if (service.length) { service[0].status(status); } }
To get the object, you can write a helper function that will retrieve for you a service object. You could do something like this (assuming mappedData is an observableArray and id observable) : function get_service_by_id(service_id){ for(var i=0;i<mappedData().length;i++){ if (mappedData()[i].id() === service_id){ return mappedData()[i]; } } return false; }
How to get information from datatable - javascript - MVC
I have created an ASP.net MVC app and I have created a DataTable [DataTable.net] as follows: <table id="invoiceTable"> <thead> <tr> <th>Invoice ID</th> <th>Date</th> <th>Reciept Date</th> <th>Category</th> <th>Total Value</th> <th>Invoice Ref</th> <th>Client</th> <th>Status</th> </tr> </thead> <tbody> #{ foreach (FreeAgentApp.Models.CustomInvoice _invoice in ViewBag.Invoices) { <tr> <td>#_invoice.InvoiceId</td> <td>#_invoice.Date</td> <td>#_invoice.RecpDate</td> <td>#_invoice.Category</td> <td>#_invoice.TotalValue</td> <td>#_invoice.InvoiceRef</td> <td>#_invoice.Client</td> <td>#_invoice.Status</td> </tr> } } </tbody> </table> And i can get the information from a row when its selected using javascript as follows: // Row data $(document).ready(function () { oTable = $('#invoiceTable').dataTable(); oTable.$('tr').click(function () { var data = oTable.fnGetData(this); alert(data); // ... do something with the array / object of data for the row }); }); The variable data will provide a string of every value in the row separated by a comma as follows: "000,26-01-14,27-01-14,001,1000,inv,something ltd,paid" I want to have all these values separated. Note this could be done by splitting on the comma however a value in the table could contain commas. How can I separate this string?
According to the DataTables documentation oTable.fnGetData(this); return an array filled with the data of the definitions in the row so you should be able to acces the data directly from data. var invoiceId = data[0]; var date = data[1]; var recpDate = data[2]; // etc. etc.
JQuery tablesorter appended data not sorting
Im trying too append data to a table with the tablesorter plugin (http://tablesorter.com) Im using the following code: <table id="sortme" border="1" style="width: 200px;"> <thead> <tr> <th>first name</th> <th>last name</th> <th>age</th> </tr> </thead> <tbody> <tr> <td>will</td> <td>smith</td> <td>1</td> </tr> ................... </tbody> </table> Click me! And: $(document).ready(function() { var i = 5; $("#sortme").tablesorter({ sortList: [[2,0]] }); $("#test").click(function() { $("#sortme tbody").append('<tr><td>NEW</td><td>NEW</td><td>'+(i++)+'</td></tr>'); $("#sortme").trigger("update"); var s = [[2,0]]; $("#sortme").trigger("sorton",[s]); return false; }); }); Problem is the appended row stays at top, why? See example: http://jsfiddle.net/jmU3Z/8/
In case anyone else stumbles across this. By the time the "sorton" event is handled the DOM hasn't been assigned the table.config.parsers. The "sorton" event handling needs to be wrapped in a 1 millisecond timeout. Replace the existing "sorton" bind in jquery.tablesorter.js (line ~803) with the following: }).bind("sorton", function (e, list) { var me = this; setTimeout(function () { $(this).trigger("sortStart"); config.sortList = list; // update and store the sortlist var sortList = config.sortList; // update header count index updateHeaderSortCount(me, sortList); // set css for headers setHeadersCss(me, $headers, sortList, sortCSS); // sort the table and append it to the dom appendToTable(me, multisort(me, sortList, cache)); }, 1);
You're problem is the [s]. You're sort parameter is already an array, just pass it the var not the var in an array. $("#sortme").trigger("sorton",s); Works for me, FF4.