I have an html table and I want to hyperlink the entries in one column.
fiddle example here: http://jsfiddle.net/hohenheim/uF4H7/5/ - note once you click on any bar in the graph, there will be an onclick event to show the table.
On the table I want to be able link from clicking on any adId. ie once an adId is clicked it should go to 'www.url.com/adId#. Can this be done in html/js? I've tried creating the link and appending it but haven't had much success. Do I need to refactor how I append entries to the table? Or is it possible rather to create an on-click event on the table?
here is the code on how the table is generated:
function appendAds(date1) {
nonUTCdate = new Date(date1);
UTCdate = new Date(nonUTCdate.getUTCFullYear(), nonUTCdate.getUTCMonth(), nonUTCdate.getUTCDate());
$('#tableTitle').html("<u>Info for " + timeConverter(UTCdate) + ":</u>");
$("#table").find("tr:gt(0)").remove();
$("#table tr").show();
for (var i = 0; i < preGroupData.length; i++) {
if ((date1 >= preGroupData[i].startDate) && (date1 <= preGroupData[i].endDate)) {;
$('#table').append('<tr><td align="center">' + timeConverter(preGroupData[i].startDate) + '</td><td align="center">' + timeConverter(preGroupData[i].endDate) + '</td><td align="center">' + preGroupData[i].adId + '</td><td align="center">' + preGroupData[i].impressions + '</td></tr>');
}
}
}
If I'm understanding you correctly, you can simply turn the column with preGroupData[i].adId into a link to 'www.url.com/#adId' by adding the anchor text to the html string you are inserting into the html.
replace
<td align="center">' + preGroupData[i].adId + '</td>
in your string with:
<td align="center><a href='www.url.com/#" + preGroupData[i].adId +"'>"
+ preGroupData[i].adId + "</a></td>"
the click handler:
$('tr td:nth-child(3)').click(function(){
window.location.href = $(this).text()
}
As an HTTP redirect (back button will not work )
window.location.replace("http://www.google.com");
like if you click on a link (it will be saved in the session history, so back button will work as expected)
window.location.href = "http://www.google.com";
If you save the cell to a variable, you can easily add an "on click" event, like this:
function appendAds(date1) {
nonUTCdate = new Date(date1);
UTCdate = new Date(nonUTCdate.getUTCFullYear(), nonUTCdate.getUTCMonth(), nonUTCdate.getUTCDate());
$('#tableTitle').html("<u>Info for " + timeConverter(UTCdate) + ":</u>");
$("#table").find("tr:gt(0)").remove();
$("#table tr").show();
for (var i = 0; i < preGroupData.length; i++) {
if ((date1 >= preGroupData[i].startDate) && (date1 <= preGroupData[i].endDate)) {;
var row = $("<tr />")
.append("<td align='center'>" + timeConverter(preGroupData[i].startDate) + "</td>")
.append("<td align='center'>" + timeConverter(preGroupData[i].endDate) + "</td>");
var adIdCell = $("<td align='center'>" + preGroupData[i].adId + "</td>").appendTo(row);
row.append("<td align='center'>" + preGroupData[i].impressions + "</td>");
row.appendTo("#table");
adIdCell.on("click", function() {
location.href = "./#" + preGroupData[i].adId;
});
}
}
}
Related
I have a table with the list of orders in my ecommerce site. I would like another line to appear with the order details by clicking on each line.
I don't understand how to write the function.
Here is how i wrote the table row:
$("#tbody").append("<tr id='riga" + i + "'><th scope='row'>" + idordine
+ "</th><td>" + data + "</td><td>" + prezzoTotale
+ "€</td><td> <button id='button_" + i
+ "')>Dettagli</button></tr><tr id='orderdetail_" + i
+ "' style='display:none;'>"
+ "<th scope='row'> ciao </th><td>ciao2</td><td>ciao2</td><td>ciao2</td></tr>"
);
and the function i wrote is:
for (var i = 0; i < ordini.length; i++) {
$("#button_" + i).click(function(i) {
$("#orderdetail_" + i).toggle("slow");
});
}
the variable i in $("#orderdetail_"+i) doesn't work. How can i do it?
Thanks all.
There's no need to link button_i with orderdetail_i, you can use relative navigation, in this case a relatively simple:
$(this).closest("tr").next()
To facilitate the event handler, I've added togglebutton class to each button and used that in a single selector.
Updated code:
// setup some example data
for (var i = 1;i<10; ++i) {
$("tbody").append("<tr id='riga" + i + "'><th scope='row'>" + "idordine"
+ "</th><td>" + "data" + "</td><td>" + "prezzoTotale"
+ "€</td><td> <button class='togglebutton' id='button_" + i
+ "')>Dettagli</button></tr><tr id='orderdetail_" + i
+ "' style='display:none;'>"
+ "<th scope='row'> ciao </th><td>ciao" + i + "</td><td>ciao" + i + "</td><td>ciao" + i + "</td></tr>"
);
}
// handle toggle
$("button.togglebutton").click(function() {
$(this).closest("tr").next().toggle();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tbody>
</tbody>
<table>
I have a .php file where I am using both HTML and JavaScript to display items from my database. I have a JavaScript append function that is creating cards where each item is display. On my cards, I have a button that will expand the card to show product history. Some products have more history than others so the expansion needs to be dynamic. The historical data is being pulled from database and is initially in a php array. I originally was going to institute php into the javascript append function but I could not figure out how to set the JavaScript index variable 'I' to my php index. So I want to just stay with JavaScript. But I don't know how to write a loop in the middle of this append function that will loop through the historical array and populate the expansion. Below is what I am attempting. I took out a lot of the lines in the append function but you can see what I am trying to do.
function get_products() {
clear_cards();
$.each(productNumbers,
function(i, value) {
$('.main_card_shell').append(
"<div class='card_content card_style' id='card" + i + "'>" +
"<div id='card_tab2" + i + "' class='tabcontent' data-tab='tab-name2'>" +
"<div class='details_tables'>" +
"<table>" +
"<tr>" +
"<th>Item Type</th>" +
"<th>Painted</th>" +
"<th>Last Sold" +
"<a id='_close_tab" + i + "' class='tablinks tab_override' onclick=\"openCity(event,'card_tab4" + i + "')\">" +
"<i class='large angle up icon'></i>" +
"</a>" +
"</th>" +
"</tr>" +
"<tr>" +
var itemdatesplit = itemdate[i].split("$$");
var itemtypesplit = itermtype[i].split("$$");
var itemsplit = item[i].split("$$");
var arraylength = itemsplit.length;
var counter = 0;
while(counter < arraylength)
{
+ "<td>" + itemtypesplit[counter] + "</td>" +
+ "<td>" + itemdatesplit[counter] + "</td>" +
counter = counter + 1;
}
+
"</tr>" +
"</table>" +
"</div>" +
"</div>" +
Please help. I had it working with PHP inserted in, but I just couldn't figure out how to set it to a PHP variable.
Place this code into a function:
function getSomething(i) {
var html = '';
var itemdatesplit = itemdate[i].split("$$");
var itemtypesplit = itermtype[i].split("$$");
var itemsplit = item[i].split("$$");
var arraylength = itemsplit.length;
var counter = 0;
while(counter < arraylength) {
html += "<td>" + itemtypesplit[counter] + "</td>";
html += "<td>" + itemdatesplit[counter] + "</td>";
counter = counter + 1;
}
return html;
}
And then use it in your HTML building block:
'<some html>' + getSomething(i) + '<some other html>'
function ReceiveServerData(retValue) {
var val = retValue.split('#')
var ch1= val[1].split(',');
if (val[0] == 1) {
var tab = "";
//codes
var row = ch1.split("¥");
for (n = 0; n <= row.length - 1; n++) {
var col = row[n].split("µ");
tab += "<td style='width:5%;text-align:left'><a href onclick ='Documents(" + col[0] + "," + i + ")' style='text-align:left;'>Download Attachment </td>";
//codes
}
}
function Documents(ID,i) {
//codes
}
it doesn't seem to work correctly though. Is there a better way to do this?
change this:
tab += "<td style='width:5%;text-align:left'><a href onclick ='Documents(" + col[0] + "," + i + ")' style='text-align:left;'>Download Attachment </td>";
to this:
tab += "<td style='width:5%;text-align:left'><a href='#' onclick='Documents(" + col[0] + "," + i + ")' style='text-align:left;'>Download Attachment </td>";
tips:
1. You should give a "#" to href else it will go refresh your page.
You should connect the string between "onclick" and "=" to make html template reasonable.
I used AJAX to dynamically create the HTML but I've encountered a problem
<script>
function page_loaded(){
jQuery.ajax({
method: "GET",
url: "get_data_dashboard.php",
success: function(data){
var markers = JSON.parse(data);
for(var i = 0; i < markers.length; i++){
var m = markers[i];
var markerHTML = "<div class='marker'>" +
"<span id='naziv'>Naziv zahtjeva: " + m.naziv + "</span></br>" +
"<span id='ulica'>Ulica: " + m.ulica + "</span></br>" +
"<p id='opis'>Opis:</br>" + m.opis + "</p></br>" +
"<span id='email'>Email: " + m.email + "</span></br>" +
"<img id='slika' src='" + m.link_slike + "' />" + "</br>" +
"<textarea rows='5' cols='30' maxlength='500' id='t" + m.marker_id + "' placeholder='Komentar'>" + "</textarea></br>"
+ "<div class='buttons'><a href='odobri_prijavu.php?id=" + m.marker_id + "'>Odobri</a>" +
"<a href='izbrisi_prijavu.php?id=" + m.marker_id + "'>Izbriši</a>" + "</div>" +
"</div><hr>";
$('#content').append(markerHTML);
}
}
})
}
$(document).ready(page_loaded());
</script>
I tried to use buttons first instead of anchor tags but I couldn't figure how to add event handlers to dynamically created buttons that will post a request via AJAX to some php script with the proper id as the value and the value of the textarea. So I used the anchor tag and I was able to send the id, but I can't send the value of the textarea because I don't know how to reference it and even if I referenced it, it will be NULL because its value is set to the anchor tag at the very beginning and I want to type in text in the textarea.
Instead of listening to individual "elements", you can actually listen to a parent of a specific element (You'll need to supply another parameter to on()). A popular pattern is to listen to "body" (because body is a parent to all, technically), but any non-dynamic parent element will work! Here's an example:
//notice the second parameter supplied
$("body").on("click", ".my-dynamic-element", function(e){
//awesome code that makes the world a better place goes here
//this code triggers when .my-dynamic-element is clicked, wootz
});
Event delegation is your friend.
I don`t see any actions that actually do event handling, but a simple solution would be something like:
$(document).on('click', '.your-button-class', function(){
// do your thing
});
<script>
function page_loaded(){
jQuery.ajax({
method: "GET",
url: "get_data_dashboard.php",
success: function(data){
var markers = JSON.parse(data);
for(var i = 0; i < markers.length; i++){
var m = markers[i];
var markerHTML = "<div class='marker'>" +
"<span id='naziv'>Naziv zahtjeva: " + m.naziv + "</span></br>" +
"<span id='ulica'>Ulica: " + m.ulica + "</span></br>" +
"<p id='opis'>Opis:</br>" + m.opis + "</p></br>" +
"<span id='email'>Email: " + m.email + "</span></br>" +
"<img id='slika' src='" + m.link_slike + "' />" + "</br>" +
"<textarea rows='5' cols='30' maxlength='500' id='t" + m.marker_id + "' placeholder='Komentar'>" + "</textarea></br>"
+ "<div class='buttons'>Odobri" +
"<a href='izbrisi_prijavu.php?id=" + m.marker_id + "'>Izbriši</a>" + "</div>" +
"</div><hr>";
$('#content').append(markerHTML);
}
}
})
}
$(document).ready(page_loaded());
function clickHandler(id){
$('#'+id) // selects the button
$('#t'+id) // selects the text area
}
</script>
I am adding rows to a table dynamically using the following function. Upon a row click the table disappears and another div is generated and populated dynamically. It was all working perfectly till 2 hours ago, when suddenly without me changing any code it stopped. I am not sure if the following code is sufficient enough for someone to give me an answer but does anyone see any particular reason this will stop working all of the sudden ?
var setupProjectListTable = function (ipProject, table, tableContainer) {
$('#' + table + ' tr').hover(function() {
$(this).addClass('hover');
}, function() {
$(this).removeClass('hover');
});
var title = ipProject.getTitle();
var desc = ipProject.getDesc();
var applicableDegrees = ipProject.getApplicableDegreeList();
var keywordList = ipProject.getKeywordList();
$('#' + table + ' tbody').append(
'<tr id = "' + title + '">' +
'<td>' + title + '</td>' +
'<td>' + desc + '</td>' +
'<td>' + applicableDegrees + '</td>' +
'<td>' + keywordList + '</td></tr>'
);
$("#" + title).click( function(e) {
e.preventDefault();
$('#' + tableContainer).hide();
generateProjectDetails(ipProject);
});
};
It's simple. click() Only affects existing elements but not added elements.
so use on():
$("tbody").on('click', '#' + title, function(e) {
e.preventDefault();
$('#' + tableContainer).hide();
generateProjectDetails(ipProject);
});