I have a situation in which my Jquery Ajax script generates HTML table. And another script is meant to filter the table column by providing a dropdown comprising of unique values in that particular column.
If i have static content in html page the filter script works fine. But is not able to read table content when it is generated via Ajax that is during runtime.
Any idea what could be the reason. I also tried to align script in order.
My Ajax script is here:-
$(document).ready(function() {
$("#getResults").click(function(){
bug = $("#bug").val();
priority = $("#priority").val();
component = $("#component").val();
fixVersion = $("#fixVersion").val();
dateType = $("#dateType").val();
fromDate = $("#dp2").val();
toDate = $("#dp3").val();
$("#query").empty();
$("tbody").empty();
$.post("getRefineSearchResultsPath", {bug:bug,priority:priority,component:component,
fixVersion:fixVersion,dateType:dateType,fromDate:fromDate,toDate:toDate },
function(data) {
// setting value for csv report button
//clear the value attribute for button first
$("#query_csv").removeAttr("value");
//setting new value to "value" attribute of the csv button
$("#query_csv").attr("value", function(){
return $(data).find("query").text();
});
$("#query").append("<p class='text-success'>Query<legend></legend><small>" +$(data).find("query").text() +"</small></p>");
var count = 1;
$(data).find("issue").each(function(){
var $issue = $(this);
var value = "<tr>";
value += "<td>" +count +"</td>";
value += "<td>" +$issue.find('issueKey').text() +"</td>";
value += "<td>" +$issue.find('type').text() +"</td>";
value += "<td><div id='list' class='summary'>" +$issue.find('summary').text() +"</div></td>";
value += "<td><div id='list' class='mousescroll'>" +$issue.find('description').text() +"</div></td>";
value += "<td>" +$issue.find('priority').text() +"</td>";
value += "<td>" +$issue.find('component').text() +"</td>";
value += "<td>" +$issue.find('status').text() +"</td>";
value += "<td>" +$issue.find('fixVersion').text() +"</td>";
value += "<td>" +$issue.find('resolution').text() +"</td>";
value += "<td>" +$issue.find('created').text() +"</td>";
value += "<td>" +$issue.find('updated').text() +"</td>";
value += "<td>" +$issue.find('os').text() +"</td>";
value += "<td>" +$issue.find('frequency').text() +"</td>";
value += "<td>";
var number_of_attachement = $issue.find('attachment').size();
if(number_of_attachement > 0){
value += "<div id='list' class='attachment'>";
value += "<ul class='unstyled'>";
$issue.find('attachment').each(function(){
var $attachment = $(this);
value += "<li>";
value += "<a href='#' onclick='document.f1.attachmentName.value='" +$attachment.find('attachmentName').text();
value += "';document.f1.issueKey.value='"+$attachment.find('attachmentissueKey').text();
value += "';document.f1.digest.value='"+$attachment.find('attachmentdigest').text();
value += "';document.f1.submit();'>"+$attachment.find('attachmentName').text();
value += "</a>";
value += "</li>";
value += "<br>";
});
value +="</ul>";
value +="</div>";
}
value += "</td>";
value += "</tr>";
$("tbody").append(value);
count++;
});
});
});
});
And my script to filter table is here, I got this script from this link http://www.javascripttoolbox.com/lib/table/
My JSP page is here:-
<html>
<body>
<table class="table table-bordered table-condensed table-hover example sort01 table-autosort:0 table-autofilter table-autopage:10 table-page-number:t1page table-page-count:t1pages table-filtered-rowcount:t1filtercount table-rowcount:t1allcount">
<thead >
<tr>
<th class="table-sortable:numeric" Style="width:3%;">No.</th>
<th class="table-sortable:default" Style="width:5.5%;">Issue Key <br>
</th>
<th>Type</th>
<th Style="text-align: center;">Summary</th>
<th Style="text-align: center;">Description</th>
<th class="table-filterable table-sortable:default" id ="priorityColumn" Style="width:5%">Priority</th>
<th class="table-filterable table-sortable:default" >Component</th>
<th class="table-filterable table-sortable:default" Style="width:5%">Status</th>
<th class="table-filterable table-sortable:default">Fix Version</th>
<th class="table-filterable table-sortable:default" Style="width:6%">Resolution</th>
<th>Created</th>
<th>Updated</th>
<th>OS</th>
<th>Frequency</th>
<th>Attachments</th>
</tr>
</thead>
<tbody>
</tbody>
<tfoot>
<tr>
<td class="table-page:previous" style="cursor:pointer;"><img src="table/icons/previous.gif" alt="Previous"><small>Prev</small></td>
<td colspan="13" style="text-align:center;">Page <span id="t1page"></span> of <span id="t1pages"></span></td>
<td class="table-page:next" style="cursor:pointer;">Next <img src="table/icons/next.gif" alt="Previous"></td>
</tr>
<tr Style="background-color: #dddddd">
<td colspan="15"><span id="t1filtercount"></span> of <span id="t1allcount"></span> rows match filter(s)</td>
</tr>
<tr class="text-success">
<td colspan="15">Total Results : ${count}</td>
</tr>
</tfoot>
</table>
</body>
</html>
The problem is likely that the filter script reads the intial table data on page load and stores a big object which is then what gets filtered. You AJAX is visibly changing the html but not the data object.
A cursory glance and search of the text in the support documentation didn't reveal any support for AJAX for me. It also doesn't appear to include destroy methods so you could reinitialize after the AJAX. The API for the script doesn't appear very robust either.
I would suggest you look for an integrated table system that supports ajax like datatables.js or jQgrid. Both remain in active development and are widely used
Related
Im trying to add input where you can select different options for a location. my issue is that its messing up the table and it just does not see me new input option that i've added. I can list other files or pictures if needed.
var items = 0;
function addItem() {
items++;
var html = "<tr>";
html += "<td>" + items + "</td>";
html += "<td><input type='number' name='idNumber[]'></td>";
html += "<td><input type='text' name='itemName[]'></td>";
html += "<td><input type='number' name='itemQuantity[]'></td>";
html += "<td><select name='itemLocation[]' id='location'><option value="Shop" name="Shop">Shop</option></select></td>";
html += "<td><input type='text' name='itemIndex[]'></td>";
html += "<td><button type='button' onclick='deleteRow(this);'>Delete</button></td>"
html += "</tr>";
Just trying to change to location button to a multiple choice selection
you have double quotes inside your string which is breaking your template
to avoid this kind of error you should use template literals its fully supported
Note it is backtick (`) not singlequote (')
see example
var value = "new entry"
var tr = `
<tr>
<td>${value}</td>
<td>${value}</td>
<td>${value}</td>
<td>${value}</td>
<td>${value}</td>
</tr>
`
I am trying to retrieve data from my firebase realtime database, and then append them onto my HTML table. However, I don't have any ideas on how to create the HTML table that is selectable and submit what I selected. Here is how my database tree looks like.
Here is the HTML table I made.
<table class="hotel-list" id="Marriott-hotel-list">
<thead class="thead-inverse">
<tr>
<th>Hotel Name</th>
<th>Hotel Location</th>
<th>Cheap Room's Price</th>
<th>Amount of Cheap Room</th>
<th>Expensive Room's Price</th>
<th>Amount of Cheap Room</th>
</tr>
</thead>
<tbody>
<tr id="tr">
<td id="Hotel-Name"></td>
<td id="Hotel-location"></td>
<td id="Cheap-Rooms-Price"></td>
<td id="Amount-of-Cheap-Room"></td>
<td id="Expensive-Rooms-Price"></td>
<td id="Amount-of-Cheap-Room"></td>
</tr>
</tbody>
</table>
and here is the js that I used to try to append data onto the table.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js">
var database = firebase.database();
database.ref('projectTable/hotelTable/Hilton/').orderByChild('hotel_name').once('value', function (snapshot) {
if (snapshot.exists()) {
var content = '';
snapshot.forEach(function (data) {
var val = data.val();
content += '<tr>';
content += '<td>' + val.hotel_name + '</td>';
content += '<td>' + val.Location + '</td>';
content += '<td>' + val.cheap_room_price + '</td>';
content += '<td>' + val.max_amount_cheap_room + '</td>';
content += '<td>' + val.expensive_room_price + '</td>';
content += '<td>' + val.max_amount_expensive_room + '</td>';
content += '</tr>';
});
$('#Marriott-hotel-list').append(content);
}
});
</script>
I try to put the hotel name, Location, cheap_room_price, max_amount_cheap_room, expensive_room_price, max_amount_expensive_room, these values from the database onto the table.
But nothing shows on the web page and also I realize the table I made has no selectable feature.
Can any expert point out my error and help me fix it in Javascript? Thank you and much appreciation!
I'm getting JSon data from my server and building a table with this data. The last column of the table is a button that will grab the fields from the selected row to populate another fields, but it is returning: ReferenceError: response.DATA is not defined.
response = JSON.parse(response);
$('.myDiv').empty();
// Header
var table = '<table class="table table-striped"><thead><tr><th>First Name</th><th>Last Name</th><th>City</th><th>State</th><th>ZIP</th><th>Action</th></tr></thead><tbody>';
var i;
for(i=0; i<response.ROWCOUNT; i++){
table += '<tr>';
table += '<td>' + response.DATA.PROVIDERFIRSTNAME[i] + '</td>';
table += '<td>' + response.DATA.PROVIDERLASTNAME[i] + '</td>';
table += '<td>' + response.DATA.PROVIDERCITY[i] + '</td>';
table += '<td>' + response.DATA.PROVIDERSTATE[i] + '</td>';
table += '<td>' + response.DATA.PROVIDERPOSTALCODE[i] + '</td>';
table += '<td><input type="button" class="btn btn-primary" value="select" onClick="setData(response.DATA, i);" /></td>';
table += '</tr>';
}
table += '</tbody></table>';
$('.myDiv').append(table);
My setData function:
function setData(data, pos){
console.debug(data.PROVIDERFIRTNAME[pos]);
}
You're using an inline handler, and an inline handler can only reference global variables. Attach the listener properly using Javascript instead, so that it can reference the variables (most importantly, response and i) properly. Also make sure i is block scoped with let, rather than function-scoped.
You can also consider using template literals to make the code a lot more readable:
// Header
const $table = $('<table class="table table-striped"><thead><tr><th>First Name</th><th>Last Name</th><th>City</th><th>State</th><th>ZIP</th><th>Action</th></tr></thead><tbody></tbody>');
// make sure to use "let i" here
for (let i = 0; i < response.ROWCOUNT; i++) {
const $row = $(`
<tr>
<td>${response.DATA.PROVIDERFIRSTNAME[i]}</td>
<td>${response.DATA.PROVIDERLASTNAME[i]}</td>
<td>${response.DATA.PROVIDERCITY[i]}</td>
<td>${response.DATA.PROVIDERSTATE[i]}</td>
<td>${response.DATA.PROVIDERPOSTALCODE[i]}</td>
<td>${response.DATA.PROVIDERPOSTALCODE[i]}</td>
<td><input type="button" class="btn btn-primary" value="select"/></td>
</tr>
`);
$row.find('input').on('click', () => {
console.debug(response.DATA.PROVIDERFIRSTNAME[i]);
});
$table.find('tbody').append($row);
}
$('.myDiv').append(table);
Spelling matters - make sure to use PROVIDERFIRSTNAME instead of PROVIDERFIRTNAME.
Inline handlers are pretty universally considered to be poor practice - best to avoid them whenever possible.
I'm trying to develop a simple way to turn a .json file into an HTML table using javascript.
I've tried using a file upload input, text input, html form, as well as appending data from one .json file to another empty container .json file. The main thing(s) I'm trying to fix are located on lines 3, 5, and 17.
What I want to happen is the user inputs the name of the .json file via the text field (or just uploads the file), and the program turns it into an html table, but I can't figure out how to use a variable to implement that.
<script>
$(function() {
var whatname = document.getElementById("whatnametext")
var people = [];
$.getJSON(whatname, function(data) {
$.each(data.person, function(i, f) {
var tblRow = "<tr>" + "<td>" + f.firstName + "</td>" +
"<td>" + f.lastName + "</td>" + "<td>" + f.job + "</td>" + "<td>" + f.roll + "</td>" + "</tr>"
$(tblRow).appendTo("#userdata tbody");
});
});
});
</script>
</head>
<body>
<input type="text" id="whatnametext">
<div class="wrapper">
<div class="profile">
<table id= "userdata" border="2">
<thead>
<th>First Name</th>
<th>Last Name</th>
<th>Email Address</th>
<th>City</th>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>```
I am trying to create a table that has some dynamic data and some server controls. I need server controls so that they will be accessed in code behind. I am creating table in JQuery. My code is as below.
function createLeaveDetails(formattedArray) {
var leaveTable = "<table class='table table-striped table-hover table-bordered' style='width: 435px'>";
leaveTable += "<tr> <th>Date</th> <th>Full Day</th> <th>Half Day</th> </tr>";
for (i = 0; i < formattedArray.length; i++) {
leaveTable += "<tr> <td>" + formattedArray[i] + "</td> <td><asp:RadioButton ID='radHalf" + i + " runat='server' GroupName='grp" + i + "' /></td>";
leaveTable += "<td><asp:RadioButton ID='radFull" + i + " runat='server' GroupName='grp" + i + "' /></td> </tr>";
}
leaveTable = +"</table>"
$("#tblLeaveDetails").append();
}
but at run-time its giving The server tag is not well formed. error on line
leaveTable += "<tr> <td>" + formattedArray[i] + "</td> <td><asp:RadioButton ID='radHalf" + i + " runat='server' GroupName='grp" + i + "' /></td>";
First of all, you cannot create "server" controls from client side (i.e. JavaScript / Jquery). As the Browser does not understand the server tag. The Asp View engine render that server control to the HTML control accordingly. So all you can do is you create basic HTML elements from the client side scripting.
Create
<input type='radio'></input>
instead of
<asp:RadioButton />
As you need the value at the server you can create an Hidden Field and put the selected value of the input radio in that hidden field from JavaScript / Jquery. as follows
$('#yourRadiobuttonlist').change(function(){
yourHiddenField.value = $('#yourRadiobuttonlist :selected').val();
});
And at server side you can access the Hidden Field value for the desired selected value of the radio button.
Hope it helps....