I have table with empty body:
<table id="tablem" border="1" width="100">
<thead>
<tr>
<th style="width: 10%">PageName</th>
<th style="width: 5%">Lang</th>
<th style="width: 10%">ControlName</th>
<th style="width: 70%">ControlValue</th>
<th style="width: 5%">Edit</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
I fill body by AJAX-request, which gets data from the database, and this Ajax-function construct table's body with data:
function ForSearching(args, callback) {
$.ajax({
url: "/source_pages/SearchForMultilang.ashx",
type: "POST",
dataType: "json",
data: args,
}).success(function(data) {
var table = $("#tablem tbody");
$("#tablem tbody").empty();
callback(data);
$.each(data, function (idx, elem) {
table.append('<tr><td>' + elem.PageName + '</td><td>' + elem.Lang + '</td><td>' + elem.ControlName + '</td><td>' + elem.ControlValue + '</td><td><input type="button" id="btnedt" value="Edit" /></td></tr>');
});
});
}
The table is created, filled by data, and in the last column Ajax creates Edit-button.
When you click this button, textfield in every column in row must transform to input type=text with value from cell, so I can change value and after all save this changes in the selected row. And all this must work by ajax.
How to do this?
I recommend to put this code witch changed selector in click handler of this button
$('td').each(function(){
$(this).html($('<input>',{type:'text', value: $(this).html()}));
});
https://jsfiddle.net/urp83mtq/1/
EDIT: I just saw that you want have one button per row
$(':button').each(function(){
$(this).on('click',function(){
$(this).parent().parent().children().each(function(){
if($(this).children().length==0)
$(this).html($('<input>',{type:'text', value: $(this).html()}));
});
});
});
https://jsfiddle.net/urp83mtq/2/
Related
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>
I want to display data from XML file into a table, like:
<table>
<th>Head 1</th><th>Head 2</th><th>Head 3</th>
<tr><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td></tr>
</table>
All 3 columns of the table will be filled with the data from the XML file. I tried it but I'm able to see the only one data from the file but all rows must be filled with unique data. Also, once the JQUERY loads the data, 'table heading' doesn't display, instead the data from the file itself appears.
And when the "new XML" file will be added to the folder, the data from the older XML file should be disappeared and the data from the new file should be displayed on the screen.
Below I've added my code snippet.
HTML file
<div>
<table class="status_table">
<th class="table_heading">Heading 1</th>
<th class="table_heading" style="text-align:center; padding:30px;">Heading 2</th>
<th class="table_heading" style="text-align:center; padding:30px;">Heading 3</th>
<tr></tr>
</table>
</div>
JQUERY file
$('head').append('<link rel="stylesheet" href="style2.css" type="text/css" />');
$(document).ready(function(){
truckData();
fetch();
})
function fetch(){
setTimeout(function(){
truckData();
fetch();
},100)
}
function truckData(){
$.ajax({
url: "20200707083610.xml",
dataType: "",
success: function(data){
$("tr").children().remove();
$(data).find("DisplayData").each(function(){
var info = '<td class="table_content" style="color: #DB075C;">'+$(this).find("LaneName").text()+'<td class="table_content" style="color: #FFFF00;">'+$(this).find("PlateNumber").text()+'<td class="table_content" style="color: #3107DB;">'+$(this).find("BayName").text();
$("tr").append(info);
});
}
})
}
XML File
<DisplayRequestData>
<DisplayData displayPort="d-01">
<LaneName>Lane 1</LaneName>
<PlateNumber>7709</PlateNumber>
<BayName>ABCD 1</BayName>
</DisplayData>
<DisplayData displayPort="d-02">
<LaneName>Lane 2</LaneName>
<PlateNumber>5652</PlateNumber>
<BayName>XYZA 0012</BayName>
</DisplayData>
<DisplayData displayPort="d-03">
<LaneName>Lane 3</LaneName>
<PlateNumber>XR-20398</PlateNumber>
<BayName></BayName>
</DisplayData>
</DisplayRequestData>
You can use += to append new rows ,currently your code just append row in tr . Instead you can use tbody and append trs inside this tbody.Also , you can give dataType: "xml" in ajax call as you are returning xml file as response.
Demo Code :
//your xml
var data = '<DisplayRequestData><DisplayData displayPort="d-01"><LaneName>Lane 1</LaneName><PlateNumber>7709</PlateNumber><BayName>ABCD 1</BayName> </DisplayData><DisplayData displayPort="d-02"><LaneName>Lane 2</LaneName><PlateNumber>5652</PlateNumber><BayName>XYZA 0012</BayName></DisplayData> <DisplayData displayPort="d-03"><LaneName>Lane 3</LaneName> <PlateNumber>XR-20398</PlateNumber> <BayName></BayName></DisplayData></DisplayRequestData>'
var info = "";
//parse xml (this is not needed if you have specified response as xml )
$xml = $($.parseXML(data));
//loop through data
$xml.find("DisplayData").each(function() {
//append rows
info += '<tr><td class="table_content" style="color: #DB075C;">' + $(this).find("LaneName").text() + '<td class="table_content" style="color: #FFFF00;">' + $(this).find("PlateNumber").text() + '<td class="table_content" style="color: #3107DB;">' + $(this).find("BayName").text() + '</tr>';
});
//add rows inside tbody
$("table tbody").html(info);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
<table class="status_table">
<thead>
<th class="table_heading">LaneName</th>
<th class="table_heading" style="text-align:center;">PlateNumber</th>
<th class="table_heading" style="text-align:center; ">BayName</th>
</thead>
<tbody>
<tr></tr>
</tbody>
</table>
</div>
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);
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>
I'm trying to get a specific value from a table cell and pass is to a controller. But it doesn't seem to be working. I show you some of my codes:
This is in the controller:
def searchUser = {
String abc = request.getParameter("harrow")
println(abc)
}
This is in the html page:
<form>
<div style="height: 250px; overflow: scroll; width: 100%;">
<table id="normal">
<g:each in = "${result}">
<tr id="btn">
<td width=10% >${it.ID}</td>
<td width=25% id="harrow">${it.username}</td>
</tr>
</g:each>
</table>
</div>
<input type ="submit" name ="abc" id="opener">
</form>
EDIT
AJAX:
$("#edittable").on('click', function() {
$.ajax({
url: URL,
data: $(this).serialize(),
type: "POST",
success: function(html){
//do something with the `html` returned from the server here
$("#edit1").html(html).dialog("open")
},
error: function(jqXHR, textStatus, errorThrown){
alert('error: ' + textStatus + ': ' + errorThrown);
}
});
return false;//suppress natural form selection
});
I can get the value to pass to the controller, but right now it only retrieves the first row of values rather than the other. Is there something wrong with my AJAX codes? Thank you guys so much.
So to show details of the row, one of the approaches can be:
CONTROLLER METHODS
def rows = [
[id:1, name:'name1'],
[id:2, name:'name2'],
[id:3, name:'name3']
]
def show(){
[results:rows]
}
def showLine(Long id){
render rows.find {it.id == id }
}
VIEW
<html>
<head>
<g:javascript library="jquery" />
<r:layoutResources />
</head>
<body>
<table>
<thead>
<tr>
<th>Action</th>
<th>Id</th>
<th>Name</th>
</tr>
</thead>
<tbody>
<g:each in="${results}" status="i" var="r">
<tr>
<td><g:remoteLink value="Details" id="${r.id}" action="showLine" update="lineDetails">Details</g:remoteLink></td>
<td>${r.id}</td>
<td>${r.name}</td>
</tr>
</g:each>
</tbody>
</table>
<div id="lineDetails">
</div>
</body>
</html>
Basically you call showLine method using AJAX and passing row object id. Then you search for the object and render it back. Rendered data is put into div under the table. It's up to you if you use onclick, button or link in the table. It's also up to you how to show details on results page - use jquery dialog, or something else. Hope it helps