Retrieving Mongodb data in front end by using Ajax Get Method - html

I have inserted a data in mongodb and used nodejs for writting API, need to retrieve those data in front-end using jquery. I have inserted 3 rows of data in mongodb.I have used below code to get data and it is working fine, but it is hardcoded. I want it to happen manually using jquery. Please help to solve this.
$.ajax({
dataType:"json",
url: "/purchaser/purchasersList",
type:"GET",
global:false,
async:false,
success: function(response){
console.log("response is:",response);
document.getElementById("userName").innerHTML = (response[0].userName);
document.getElementById("email_id").innerHTML=(response[0].email_id);
document.getElementById("address").innerHTML=(response[0].address);
document.getElementById("phoneNumber").innerHTML=(response[0].phoneNumber);
//2nd row data
document.getElementById("userName1").innerHTML = (response[1].userName);
document.getElementById("email_id1").innerHTML=(response[1].email_id);
document.getElementById("address1").innerHTML=(response[1].address);
document.getElementById("phoneNumber1").innerHTML=(response[1].phoneNumber);
//3rd row data
document.getElementById("userName2").innerHTML = (response[2].userName);
document.getElementById("email_id2").innerHTML = (response[2].email_id);
document.getElementById("address2").innerHTML = (response[2].address);
document.getElementById("phoneNumber2").innerHTML =(response[2].phoneNumber);
},
error: function (jqXHR, textStatus, errorThrown) { // error callback
console.log("Error Response jqXHR is:" + jqXHR);e
<table class = table2>
<tr>
<th style="text-align:center">SL.No</th>
<th style="text-align:center">Purchaser Name</th>
<th style="text-align:center">Email</th>
<th style="text-align:center">Address</th>
<th style="text-align:center">Phone No</th>
</tr>
<tr>
<td height="50">1</td>
<td height="50" id="userName"></td>
<td height="50" id="email_id"></td>
<td height="50" id="address"></td>
<td height="50" id="phoneNumber"></td>
<td height="50">Active</td>
</tr>
<tr>
..
</tr>

If you can change the id to class then I surges you try something like this:
$.each(response,function(i) {
var tr = $('.table2 tr').eq((i+1));
$(tr).find(".userName").text(response[i].userName)
$(tr).find(".email_id").text(response[i].email_id)
$(tr).find(".address").text(response[i].address)
$(tr).find(".phoneNumber").text(response[i].phoneNumber)
})
Note I can't test it since I dont have your response.
Full code
$.ajax({
dataType: "json",
url: "/purchaser/purchasersList",
type: "GET",
global: false,
async: false,
success: function(response) {
$.each(response,function(i) {
var tr = $('.table2 tr').eq((i+1));
$(tr).find(".userName").text(response[i].userName)
$(tr).find(".email_id").text(response[i].email_id)
$(tr).find(".address").text(response[i].address)
$(tr).find(".phoneNumber").text(response[i].phoneNumber)
})
},
error: function(jqXHR, textStatus, errorThrown) { // error callback
console.log("Error Response jqXHR is:" + jqXHR);
e
<table class=table2>
<tr>
<th style="text-align:center">SL.No</th>
<th style="text-align:center">Purchaser Name</th>
<th style="text-align:center">Email</th>
<th style="text-align:center">Address</th>
<th style="text-align:center">Phone No</th>
</tr>
<tr>
<td height="50">1</td>
<td height="50" class="userName"></td>
<td height="50" class="email_id"></td>
<td height="50" class="address"></td>
<td height="50" class="phoneNumber"></td>
<td height="50">Active</td>
</tr>
<tr>
..
</tr>

Related

How to get ID or Key from Firebase realtime

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);
});
});

calling html id then class

Is it possible to call an html class with specific id, because for now I have same functionality in my class then I need to pull it through class + id to be specific.
I have button that populates data to my table:
<button type="button" onclick="loadData()">Get Data</button>
and I have two tables with the same class name:
<table class="sortable" id="1">
<thead>
<tr>
<th>Last Name</th>
<th>Age</th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td></td>
</tr>
</tbody>
</table>
<table class="sortable" id="2">
<thead>
<tr>
<th>First Name</th>
<th>Age</th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td></td>
</tr>
</tbody>
</table>
My aim is to update only the first table which has the id of 1. because right now when I clicked the button both of them will update since they have the same class name. Is it possible if they can Identify by class name + id?
here is my update:
function loadData() {
$.ajax({
url: '/Home/ReadDB',
type: 'GET',
dataType: 'json',
success: function (data) {
var row = '';
$.each(data, function (i, item) {
row += '<tr><td>' + item.LName + '</td><td>' + item.age
+ '</td><td>' + '<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal"> Add </button >' + '</td></tr>';
});
console.log(row)
$('.sortable tbody').html(row); // --This one overrides my previous result and want to identify by id
},
error: function (jqXhr, textStatus, errorThrown) {
alert(errorThrown);
}
});
}
First change your ID name please read this answer https://stackoverflow.com/a/70586/2724173
and then concatenate them with like this
$('.sortable#IDname tbody').html(row);
Try this
$.('#1.sortable tbody').html(row);
Simple:
var firstTable = $('.sortable[id="1"]');
console.log(firstTable);

Method 'load' does not work bootstrap-table

I'm trying to load data and popular a table bootstrap-table through an ajax call, but it does not work with the load method. Trying the data method works normally.
the method is not starting, it also does not display that no result was found
Table
<table id="table-anexo" class="table table-striped table-hover display" style="width:100%">
<thead>
<tr>
<th data-field="id"><span>DOCUMENTO</span></th>
<th data-field="tipo"><span>TIPO</span></th>
<th data-field="dt_anexo"><span>DATA ANEXO</span></th>
<!--<th data-field="acao" data-formatter="" data-events="" ><span>AÇÃO</span></th>-->
</tr>
</thead>
</table>
Function AJAX
function get_anexo_ajax(metodo, editando_id) {
var ticket_id = editando_id;
$.ajax({
type: 'POST',
data: {ticket_id : ticket_id},
url: 'ticket/get_anexo_all',
success: (function (data) {
console.log(data.anexo);
//$('#table-anexo').bootstrapTable({ data: data.anexo });
//$('#table-anexo').bootstrapTable('refresh')
$('#table-anexo').bootstrapTable('load', data.anexo);
}),
});
}
Data JSON
{"anexo":[{"id":"1","tipo":"jpg","dt_anexo":"2018-08-01 11:09:28","mensagem_id":"2","ticket_id":"1"}]}
I think data is the invalid format. And Set data-toggle="table" on a normal table. I tested, it run Ok. You can try:
<table id="table-anexo" data-toggle="table" class="table table-striped table-hover display" style="width:100%">
var obj = JSON.parse(data);
$('#table-anexo').bootstrapTable('load', obj.anexo);

How to make draggable column in HTML5 with 5 seconds refresh rate?

I am making a website which is loading data from ajax webservice call and populating it in html tables and this data gets refreshed in every 5 seconds. I have achieved this using lazy loading in Backbone JS. So when javascript timer fires in every 5 seconds it makes ajax call and it reloads the template which contains the table thus showing new data.
this.metrices.fetch({
url : (isLocal) ? ('js/jsons/agent.json') : (prev_this.url + '/agentstat'),
data: JSON.stringify(param),
type: "POST",
dataType: "JSON",
contentType: "application/json",
})
.done(function() {
});
this.$("#agentMetrics").html(this.agentTemplate({
agents: this.metrices.toJSON()
}));
And the #agentMetrics template is like this
<table class="table_class js-table-deskop" id="agent" style = "width:100%">
<tr class="">
<th class="js-table-agent js-agentPref-0 js-agentPref-0">Names</th>
<th class="js-table-agent js-agentPref-1">State</th>
<th class="js-table-agent js-agentPref-2">Skill</th>
<th class="js-table-agent js-agentPref-8">Center</th>
<th class="js-table-agent js-agentPref-9">Lan Id</th>
<th class="js-table-agent js-agentPref-10">Login Id</th>
<th class="js-table-agent js-agentPref-11">Manager</th>
<th class="js-table-agent js-agentPref-12">Site</th>
<th class="js-table-agent js-agentPref-13">Team Leader</th>
<th class="js-table-agent js-agentPref-14">Workgroup</th>
</tr>
{{#each agents}}
<tr >
<td class="th1 js-agentPref-0">{{name}}</td>
<td class="js-alert js-agentPref-1 js-state-agent" >{{state}}</td>
<td class="js-alert js-agentPref-2 js-skill-agent">{{dispSkill}}</td>
<td class="js-alert js-agentPref-8">{{center}}</td>
<td class="js-alert js-agentPref-9 js-lanid-agent">{{lanId}}</td>
<td class="js-alert js-agentPref-10">{{loginId}}</td>
<td class="js-alert js-agentPref-11">{{manager}}</td>
<td class="js-alert js-agentPref-12">{{site}}</td>
<td class="js-alert js-agentPref-13">{{teamLead}}</td>
<td class="js-alert js-agentPref-14">{{workGroup}}</td>
</tr>
{{/each}}
</table>
Now the problem is that I have a new requirement where I have to make these columns resizable and swappable i.e their width could be changed by dragging and can columns can be swapped like it can be done in Adobe flex.
This is where it gets tricky as what ever I used since the template gets refreshed in 5 seconds so it is not able to hold this selection is there any plugin to achieve this which supports retention of this preference dragging or swapping. Or can I some how prevent loading of template, any suggession
Use a data binding library like rivets.js.
Once you bind your view with rivets, changes in the model/collection will be updated in DOM by rivets without replacing the entire DOM.
If new records are added, new DOM element will be inserted in the respective position without altering the existing ones.
Below is a small demo taken from another answer of mine:
rivets.adapters[':'] = {
observe: function(obj, keypath, callback) {
obj.on('change:' + keypath, callback)
},
unobserve: function(obj, keypath, callback) {
obj.off('change:' + keypath, callback)
},
get: function(obj, keypath) {
return obj.get(keypath)
},
set: function(obj, keypath, value) {
obj.set(keypath, value)
}
}
var data = [{
name: "john",
age: 10
}, {
name: "joseph",
age: 11
}, {
name: "joy",
age: 12
}]
var userCollection = new Backbone.Collection(data);
var View = Backbone.View.extend({
initialize: function(options) {
this.render()
},
render: function() {
rivets.bind(this.el, {
users: this.collection
});
}
});
var userView = new View({
el: '#user-list',
collection: userCollection
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.2.3/backbone-min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/rivets/0.8.1/rivets.bundled.min.js"></script>
<ul id='user-list'>
<li rv-each-user="users.models">
<input type="text" rv-value="user:age"/>
<span>{user:name}</span><span> {user:age}</span>
</li>
</ul>

Inserting data into database by WEB API in angular JS

I am new into angular JS and web api world. Recently facing difficulty in sending data into database through web api and angular. I am able to load json data from webapi.Any help will be highly appreciated.
My html:
<div class="container-fluid" ng-controller="SubmitDataCtrl">
<div class="container text-center" style="margin-bottom:5px">
<div id="divTables">
<table id="tbl_SMDetails" class="display responsive nowrap aleft" cellspacing="0" width="100%">
<thead>
<tr>
<th class="all">Name</th>
<th class="all">CTSID</th>
<th class="all">FDID</th>
<th class="all">Name of the Project</th>
<th class="all">Effort Hour</th>
<th class="all">Month</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="showData in loadData">
<td>{{showData.EmpName}}</td>
<td>{{showData.EmpCognizantID}}</td>
<td>{{showData.FDID}}</td>
<td><input id="Text1" type="text" />{{showData.projectName}}</td>
<td><input id="Text1" type="text" />{{showData.effortHour}}</td>
<!--<td>{{date | date:'MM-dd-yyyy}}</td>-->
<td>{{showData.date}}</td>
</tr>
</tbody>
</table>
</div>
</div>
<div>
<a ng-href='#SubmitData' class="btn btn-default" ng-click='go()'>Submit</a><span></span>
<a ng-href='#SubmitData' class="btn btn-default" ng-click=''>Reset</a>
</div>
</div>
My service page looks like:
var request = $http({
method: method_args,
url: myConfig.ServiceURL + url,
data: JSON.stringify(input_data),
Accept: 'application/json',
headers: {'Content-Type':'application/json'},
xhrFields: {
withCredentials: true
}
});
}
and my controller is
App.controller("SubmitDataCtrl", function($scope, ajaxService) {
//login user
ajaxService.MakeServiceCall("employee/GetTeamMember? superVisiorCogniID=256826", "GET", "")
.then(function effortData(data)
{
// alert(data);
$scope.date = new Date();
$scope.loadData = data;
showData = $scope.loadData;
for (var i = 0; i < showData.length; i++)
{
//alert(showData[i].EmpName + showData[i].EmpCognizantID + showData[i].FDID);
}});
$scope.go = function()
{
//alert('clicked');
ajaxService.MakeServiceCall("effort/SaveTeamEfforts?hourLoadDate=08/20/2015&hourLoadByCogniID=256826&forMonth=July", "POST", Employee).then(function save() {
var Employee =
{
"EmpCogniID": showData[i].EmpCognizantID,
"FDTimeCardHour": showData[i].effortHour,
"HourLoadDate": 08/11/2015,
"HourLoad`enter code here`By": "256826",`enter code here`
"ForMonth": "july"
}
}).success(function (Employee)
{
$scope.showData[i] = Employee;
});
alert(Employee); }});
It's hard to answer without knowing what the API POST call should do.
Can you confirm that the POST you are making to effort/saveTeamEfforts works by executing it using a REST tester in the browser?
if that is working, you should add an .error function to your API call and see what error is returned as dan has sugested above
L