I have two tables in my html. The first is loaded on page load, the second is generated on button click.
My sortable function does not apply to the generated table.
Can you explain why?
Here is my Javascript some.js:
// sort all kinds of tables
$(function(){
$('table').sortable();
});
// generate new table
$(document).on('click','#test',function(){
let str = "<table>
<tr>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>4</td>
</tr>
</table>"
let content = $.parseHTML(str)
$('#main').html(content);
})
And here the actual html file index.html:
<!DOCTYPE html>
<html>
<head>
<script defer src="jquery/3.5.1/jquery-3.5.1.min.js"></script>
<script defer src="jquery/jquery-ui-1.13.1.js"></script>
<script defer src="some.js"></script>
</head>
<body>
<div id="main">
<table>
<tr>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>4</td>
</tr>
</table>
<input type="button" id="test" value="Generate Table" onclick="">
</div>
</body>
</html>
The generated table does not exist in the DOM in the ready event when you call $('table').sortable(); The table is added to the DOM later as response to the click event. You should call $('table').sortable(); after you add the table to the dom
This should work:
$(document).on('click','#test',function(){
let str = "<table>
<tr>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>4</td>
</tr>
</table>"
let content = $.parseHTML(str)
$('#main').html(content);
$('#main table').sortable();
})
I am trying to highlight a table row if it has a tick (check mark) in one of the td’s. I am using the jQuery code below, but it will not find a td with a html symbol such as a tick (check mark). It makes no difference if I use .text() or .html(). The code works as expected if I use any other criteria such as text or numbers, but not with html symbols. Is there away round this?
$('#farm td').filter(
function(t) {
if ($(this).text() == "✓") {
$(this).closest('tr').css('background-color', 'Yellow');
return;
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="farm" border="1">
<tr>
<th>One</th>
<th>Two</th>
</tr>
<tr>
<td>Cat</td>
<td>Duck</td>
</tr>
<tr>
<td>Pig</td>
<td>✕</td>
</tr>
<tr>
<td>✓</td>
<td>Bull</td>
</tr>
<tr>
<td>8</td>
<td>10</td>
</tr>
</table>
You just need to check for the actual character (✓). jQuery acts on rendered HTML, not markup. I determined this by setting a breakpoint on the line with text() in it and looking at the values that came through.
Also:
.each() makes more sense to me here
no need to return anything in the function
no need to pass in anything (t)
console logs are far nicer than alerts for debugging
$('#farm td').each(function() {
if ($(this).text() == "✓") {
$(this).closest('tr').css('background-color', 'Yellow');
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="farm" border="1">
<tr>
<th>One</th>
<th>Two</th>
</tr>
<tr>
<td>Cat</td>
<td>Duck</td>
</tr>
<tr>
<td>Pig</td>
<td>✕</td>
</tr>
<tr>
<td>✓</td>
<td>Bull</td>
</tr>
<tr>
<td>8</td>
<td>10</td>
</tr>
</table>
Mohamed-Yousef suggested a great refinement using an internal selector:
$('#farm td:contains("✓")').closest('tr').css('background-color', 'Yellow');
It's a slightly different selector as it would also match ✓ blah, for example, but maybe it's useful in your case.
https://api.jquery.com/contains-selector/
I have this html that posts name of an artist to an api that I wrote in Perl which queries mysql database and outputs the result. I can post the artist to api and also the api grabs the data from database but its not diplaying the data back to my html page. Could you guys please help?
HTML script :
#!/usr/bin/perl -w
print "content-type:text/html; charset=utf-8\n\n";
print <<ENDTAG
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
</head>
<body>
<form>
</form>
<script language="JavaScript">
function showInput()
{
var artist = document.getElementById("user_input").value;
console.log(artist);
var api = "http://api/post";
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", api, true);
xmlhttp.setRequestHeader("Content-Type", "application/text; charset=UTF-8");
xmlhttp.setRequestHeader('x-auth-token', 'ooooo');
xmlhttp.onreadystatechange = function() {
console.log('readyStatechange: ' + xmlhttp.readyState);
if(xmlhttp.readyState === 4 && xmlhttp.status === 200)
{
var response = xmlhttp.responseText;
document.getElementById('display').innerHTML = response;
}
else
{
//alert ("Something Went Wrong");
// console.error(xmlhttp.status);
}
}
console.log('Before open: ' + xmlhttp.readyState);
xmlhttp.send(artist);
}
</script>
</body>
<form method="POST" action="">
<label><b>Enter Artist</b></label>
<input type="text" name="message" id="user_input">
</form>
<input type="submit" onclick="showInput();">
<br>
<label>Your input: </label>
<p><span id="display"></span></p>
</html>
ENDTAG
The perl api returned following :
Opened database successfully
ARTIST is : john denver
Content-Type: text/html; charset=ISO-8859-1
Content-Type: text/html
<p>The database contains the following:</p>
<table cols=3 border=1>
<tr>
<th>Title</th>
<th>Year</th>
<th>Album</th>
</tr><tr>
<td>Downhill Stuff</td>
<td>1979</td>
</tr>
<tr>
<td>Dreamland Express</td>
<td>1985</td>
</tr>
<tr>
<td>Love Is The Master</td>
<td>1986</td>
</tr>
<tr>
<td>Windsong</td>
<td>1975</td>
</tr>
<tr>
<td>Catch Another Butterfly</td>
<td>1969</td>
</tr>
<tr>
<td>Cowboy's Delight</td>
<td>1975</td>
</tr>
<tr>
<td>How Can I Leave You Again</td>
<td>1977</td>
</tr>
<tr>
<td>Love Again</td>
<td>1986</td>
</tr>
<tr>
<td>Sail Away Home</td>
<td>1970</td>
</tr>
<tr>
<td>Sweet Melinda</td>
<td>1979</td>
</tr>
<tr>
<td>Daydream</td>
<td>1969</td>
</tr>
<tr>
<td>Gimme Your Love</td>
<td>1985</td>
</tr>
<tr>
<td>Hold On Tightly</td>
<td>1234</td>
</tr>
<tr>
<td>I Can't Escape</td>
<td>1986</td>
</tr>
<tr>
<td>It's A Possibility</td>
<td>1986</td>
</tr>
<tr>
<td>Let Us Begin (What Are We Maki</td>
<td>1986</td>
</tr>
<tr>
<td>Love Again</td>
<td>1986</td>
</tr>
<tr>
<td>Love Is The Master</td>
<td>1986</td>
</tr>
<tr>
<td>One World</td>
<td>1986</td>
</tr>
<tr>
<td>To The Wild Country</td>
<td>1977</td>
</tr>
<tr>
<td>Dreams</td>
<td>1982</td>
</tr>
<tr>
<td>Forest Lawn</td>
<td>1970</td>
</tr>
<tr>
<td>Got My Heart Set On You</td>
<td>1985</td>
</tr>
<tr>
<td>Tradewinds</td>
<td>1977</td>
</tr>
<tr>
<td>Circus</td>
<td>1969</td>
</tr>
<tr>
<td>Eagles & Horses</td>
<td>1990</td>
</tr>
<tr>
<td>Flight (The Higher We Fly)</td>
<td>1983</td>
</tr>
<tr>
<td>Life Is So Good</td>
<td>1979</td>
</tr>
<tr>
<td>The Ballad Of St. Anne's Reel</td>
<td>1980</td>
</tr>
<tr>
<td>Hold On To Me</td>
<td>1991</td>
</tr>
<tr>
<td>The Harder They Fall</td>
<td>1985</td>
</tr>
<tr>
<td>What One Man Can Do</td>
<td>1982</td>
</tr>
<tr>
<td>Johnny B. Goode</td>
<td>1979</td>
</tr>
<tr>
<td>Shanghai Breezes</td>
<td>1982</td>
</tr>
<tr>
<td>Take Me Home, Country Roads</td>
<td>1971</td>
</tr>
<tr>
<td>Wrangell Mountain Song</td>
<td>1980</td>
</tr>
<tr>
<td>A Wild Heart Looking For A Home</td>
<td>1985</td>
</tr>
<tr>
<td>Wild Montana Skies</td>
<td>1983</td>
</tr>
<tr>
<td>Heart To Heart</td>
<td>1982</td>
</tr>
<tr>
<td>Relatively Speaking</td>
<td>1982</td>
</tr>
<tr>
<td>Gospel Changes</td>
<td>1971</td>
</tr>
<tr>
<td>Around And Around</td>
<td>1971</td>
</tr>
<tr>
<td>Druthers</td>
<td>1977</td>
</tr>
<tr>
<td>Garden Song</td>
<td>1979</td>
</tr>
<tr>
I did look around for similar posts and I did find some but couldnt figure out where Im going wrong.
I looked at your code and found that you made a number of mistakes: in form itself and submission part (onsubmi=....), in javascript processing submission (was corrected - return false, please adjust api to your server).
I did not replicate database but used my own script with your data from the table, you will find it bellow.
I hope that you will find it useful, although I think that AJAX would be a better option
Main file with perl code
#!/usr/bin/perl
use strict;
use warnings;
use feature 'say';
print "content-type:text/html; charset=utf-8\n\n";
print <<ENDTAG
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<link rel="stylesheet" href="/styles.css">
</head>
<body>
<script language="JavaScript">
function showInput()
{
var artist = document.getElementById("user_input").value;
console.log(artist);
//alert(artist);
var api = "http://localhost/cgi-bin/api_perl.pl";
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", api, true);
xmlhttp.setRequestHeader("Content-Type", "application/text; charset=UTF-8");
xmlhttp.setRequestHeader('x-auth-token', 'ooooo');
xmlhttp.onreadystatechange = function() {
console.log('readyStatechange: ' + xmlhttp.readyState);
if(xmlhttp.readyState === 4 && xmlhttp.status === 200)
{
var response = xmlhttp.responseText;
document.getElementById('display').innerHTML = response;
} else {
//alert ("Something Went Wrong");
//console.error(xmlhttp.status);
}
}
console.log('Before open: ' + xmlhttp.readyState);
xmlhttp.send(artist);
return false;
}
</script>
<form method="POST" onsubmit="return showInput();">
<label><b>Enter Artist</b></label>
<input type="text" name="message" id="user_input">
<input type="submit">
</form>
<br>
<label>Your input: </label>
<p><span id="display"></span></p>
</body>
</html>
ENDTAG
File api_perl.pl
##!/usr/bin/perl
use strict;
use warnings;
use feature 'say';
say "Content-Type: text/html\n";
say "
<p>The database contains the following:</p>
<table cols=3 border=1>
<tr>
<th>Title</th>
<th>Year</th>
<th>Album</th>
</tr>
";
while(<DATA>) {
next if /^\s*$/;
chomp;
my($song,$year,$album) = split ',';
say "<tr><td>$song</td><td>$year</td><td>$album</td></tr>";
}
say "
</table>
";
__DATA__
Downhill Stuff,1979,Album 1
Dreamland Express,1985,Album 2
Love Is The Master,1986,Album 1
Windsong,1975,Album 1
Catch Another Butterfly,1969,Album 2
Cowboy's Delight,1975,Album 1
How Can I Leave You Again,1977,Album 1
Love Again,1986,Album 1
Sail Away Home,1970,Album 1
Sweet Melinda,1979,Album 1
Daydream,1969,Album 1
Gimme Your Love,1985,Album 1
Hold On Tightly,1234,Album 1
I Can't Escape,1986,Album 1
It's A Possibility,1986,Album 1
Let Us Begin (What Are We Maki),1986,Album 1
Love Again,1986,Album 1
Love Is The Master,1986,Album 1
One World,1986,Album 1
To The Wild Country,1977,Album 1
Dreams,1982,Album 1
Forest Lawn,1970,Album 1
Got My Heart Set On You,1985,Album 1
Tradewinds,1977,Album 1
Circus,1969,Album 1
Eagles & Horses,1990,Album 1
Flight (The Higher We Fly),1983,Album 1
Life Is So Good,1979,Album 1
The Ballad Of St. Anne's Reel,1980,Album 1
Hold On To Me,1991,Album 1
The Harder They Fall,1985,Album 1
What One Man Can Do,1982,Album 1
Johnny B. Goode,1979,Album 1
Shanghai Breezes,1982,Album 1
Take Me Home - Country Roads,1971,Album 1
Wrangell Mountain Song,1980,Album 1
A Wild Heart Looking For A Home,1985,Album 1
Wild Montana Skies,1983,Album 1
Heart To Heart,1982,Album 1
Relatively Speaking,1982,Album 1
Gospel Changes,1971,Album 1
Around And Around,1971,Album 1
Druthers,1977,Album 1
Garden Song,1979,Album 1
File styles.css (put into root web directory or edit <link rel='stylesheet' href='/styles.css'> and point href to proper location of styles.css file -- edit main perl file with form)
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 4px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
I have a bit of a unique situation, I am hoping knockout js provides a way to accomplish this.
I have the following structure:
Order = function() {
var self = this;
self.Name = 'default';
}
Customer = function() {
var self = this;
self.Name = 'default';
self.Orders = [];
}
I have the following table
<table>
<thead>
<tr>
<th>Customer Name</th>
</tr>
</thead>
<tbody data-bind="foreach: CustomerArray">
<tr>
<td data-bind="text: Name"></td>
</tr>
</tbody>
</table>
So this is great, it gives me a list of all my customer names.
Now for step two, I MUST format the table in a way that it lists. Order Name, then Customer Name at the bottom:
Customer Name (TH LABEL)
Order1
Order2
Order3
Smith, Frank
I came up with the idea of nesting my order array by including a tbody inside of each customer iteration, but I don't like this approach since the column width's from order to customer won't align since they are different tables.
Does anyone have any good ways to solve my unusual problem?
Thank you!
You could use "containerless control flow syntax" (Note 4 on the foreach docs) to render a TD for each order, then the customer, without a containing element:
<table>
<thead>
<tr>
<th>Customer Name</th>
</tr>
</thead>
<tbody data-bind="foreach: CustomerArray">
<!-- ko foreach: Orders -->
<tr>
<td data-bind="text: OrderDetails"></td>
</tr>
<!-- /ko -->
<tr>
<td data-bind="text: Name"></td>
</tr>
</tbody>
</table>
The commented block creates a binding scope just like the one on TBODY, but without the containing element.
This should work :
<table>
<thead>
<tr>
<th>Customer Name</th>
</tr>
</thead>
<tbody data-bind="foreach: CustomerArray">
<!-- ko foreach: Orders -->
<tr>
<td data-bind="text: Name"></td>
</tr>
<!-- /ko -->
<tr>
<td data-bind="text: Name"></td>
</tr>
</tbody>
</table>
I hope it helps.
I don't know HTML (HORRIBLY EMBARRASSED but didn't ever have the need to). I am pretty perspicacious when it comes to UNIX however I am horribly confused with this assignment I have. I know what I need to do but am having the hardest time ever getting started.
I have the following files in my hwk12 directory:
roster.html
roster.txt
sample.html
sample.txt
The following is the content of the roster.html file:
<html>
<body>
<table border=2>
<tr><th>Name</th><th>Username</th><th>Email</th></tr>
<tr>
<td>Nikhil Banerjee</td>
<td>nbanerje</td>
<td>zetapsi796#hotmail.com</td>
</tr>
<tr>
<td>Jeff Nazarian</td>
<td>jnazaria</td>
<td>jeff.nazarian#asu.edu</td>
</tr>
<tr>
<td>Anna Melzer</td>
<td>amelzer</td>
<td>anna.melzer#asu.edu</td>
</tr>
<tr>
<td>Jose Garcia</td>
<td>jgarcia</td>
<td>garcia-j#msn.com</td>
</tr>
<tr>
<td>Jillian Testa</td>
<td>jtesta</td>
<td>jillian.testa#asu.edu</td>
</tr>
<tr>
<td>Clayton Lengelzigich</td>
<td>clengelz</td>
<td><a href="mailto:clayton.lengel-zigich#asu.edu">clayton.lengel-
zigich#asu.edu</a></td>
</tr>
<tr>
<td>Ashley Bennett</td>
<td>abennett</td>
<td>ashley.bennett#asu.edu</td>
</tr>
<tr>
<td>Ann Frost</td>
<td>afrost</td>
<td>ann.frost#asu.edu</td>
</tr>
<tr>
<td>Timothy Whipple</td>
<td>twhipple</td>
<td>tweed#asu.edu</td>
</tr>
<tr>
<td>Wei Shen</td>
<td>wshen</td>
<td>shenwei58#hotmail.com</td>
</tr>
<tr>
<td>Cari Mahon</td>
<td>cmahon</td>
<td>cari.mahon#asu.edu</td>
</tr>
<tr>
<td>Alberto Salas</td>
<td>asalas</td>
<td>alberto2504#msn.com</td>
</tr>
<tr>
<td>Dorothy Haskett</td>
<td>dhaskett</td>
<td>dorothy.haskett#asu.edu</td>
</tr>
<tr>
<td>Criss Bradbury</td>
<td>cbradbur</td>
<td>crissbradbury#hotmaiil.com</td>
</tr>
<tr>
<td>Steve Ellermann</td>
<td>sellerma</td>
<td>cis494#ellermann.com</td>
</tr>
<tr>
<td>Zewdie Bekele</td>
<td>zbekele</td>
<td>zewdiea#aol.com</td>
</tr>
<tr>
<td>Frederic Diziere</td>
<td>fdiziere</td>
<td>fsd#asu.edu</td>
</tr>
<tr>
<td>Matt Bowes</td>
<td>mbowes</td>
<td>matt.bowes#asu.edu</td>
</tr>
<tr>
<td>Jasen Meece</td>
<td>jmeece</td>
<td>jasen.meece#sun.com</td>
</tr>
<tr>
<td>Aaron Carpenter</td>
<td>acarpent</td>
<td>aaron.carpenter#asu.edu</td>
</tr>
<tr>
<td>Binqin Xi</td>
<td>bxi</td>
<td>binqin.xi#asu.edu</td>
</tr>
<tr>
<td>Yinting Chan</td>
<td>ychan</td>
<td>yin.chen#asu.edu</td>
</tr>
<tr>
<td>Michael Evans</td>
<td>mevans</td>
<td>michael.evans#asu.edu</td>
</tr>
<tr>
<td>Herman Beringer</td>
<td>hberinge</td>
<td>jber#cox.net</td>
</tr>
<tr>
<td>Andrew Jolley</td>
<td>ajolley</td>
<td>andrew#andrewjolley.com</td>
</tr>
<tr>
<td>Michael Raby</td>
<td>mraby</td>
<td>mike1071#yahoo.com</td>
</tr>
<tr>
<td>Hajar Alaoui</td>
<td>halaoui</td>
<td>hajar6#hotmail.com</td>
</tr>
<tr>
<td>Anne Lemar</td>
<td>alemar</td>
<td>anne.lemar#asu.edu</td>
</tr>
<tr>
<td>Russell Crotts</td>
<td>rcrotts</td>
<td>Russell.Crotts#asu.edu</td>
</tr>
<tr>
<td>Dan Mazzola</td>
<td>dmazzola</td>
<td>dan.mazzola#sun.com</td>
</tr>
<tr>
<td>Bill Boyton</td>
<td>bboyton</td>
<td>boytonb#earthlink.net</td>
</tr>
</table>
</body>
</html>
The following is the content of the roster.txt file:
Whipple Timothy tweed#asu.edu Shen Wei shenwei58#hotmail.com
Mahon Cari cari.mahon#asu.edu Salas Alberto alberto2504#msn.com
Haskett Dorothy dorothy.haskett#asu.edu Bradbury Criss
crissbradbury#hotmaiil.com Ellermann Steve
cis494#ellermann.com Bekele Zewdie zewdiea#aol.com Diziere Frederic
fsd#asu.edu Bowes Matt matt.bowes#asu.edu Meece Jasen
jasen.meece#sun.com Carpenter Aaron aaron.carpenter#asu.edu
Xi Binqin binqin.xi#asu.edu Chan Yinting yin.chen#asu.edu
Evans Michael michael.evans#asu.edu Beringer Herman
jber#cox.net Jolley Andrew andrew#andrewjolley.com Raby Michael
mike1071#yahoo.com Alaoui Hajar hajar6#hotmail.com Lemar Anne
anne.lemar#asu.edu Crotts Russell Russell.Crotts#asu.edu Mazzola Dan
dan.mazzola#sun.com Boyton Bill boytonb#earthlink.net
The following is the content of the sample.html file:
<html>
<body>
<table border=2>
<tr><th>Name</th><th>Username</th><th>Email</th></tr>
<tr>
<td>Michael Raby</td>
<td>mraby</td>
<td>mike1071#yahoo.com</td>
</tr>
<tr>
<td>Hajar Alaoui</td>
<td>halaoui</td>
<td>hajar6#hotmail.com</td>
</tr>
<tr>
<td>Anne Lemar</td>
<td>alemar</td>
<td>anne.lemar#asu.edu</td>
</tr>
<tr>
<td>Russell Crotts</td>
<td>rcrotts</td>
<td>Russell.Crotts#asu.edu</td>
</tr>
<tr>
<td>Dan Mazzola</td>
<td>dmazzola</td>
<td>dan.mazzola#sun.com</td>
</tr>
<tr>
<td>Bill Boyton</td>
<td>bboyton</td>
<td>boytonb#earthlink.net</td>
</tr>
</table>
</body>
</html>
The following is the content of the sample.txt file:
Raby Michael mike1071#yahoo.com
Alaoui Hajar hajar6#hotmail.com
Lemar Anne anne.lemar#asu.edu
Crotts Russell Russell.Crotts#asu.edu
Mazzola Dan dan.mazzola#sun.com
Boyton Bill boytonb#earthlink.net
I'm not asking for someone to do this for me because I LOVE UNIX and I want to learn it myself. Everytime I look at this HTML code I am confusing the #$$#& out of myself. I need help getting started.
The homework prompt is the following:
You are to write a nawk(1) script called ~/hwk12/mk_html.awk that converts a text file (sample.txt and roster.txt) to an html page that a web browser can read. I have given you the output in the file sample.html which is reproduced below (notice how each level of indentation is two spaces deep):
Again, I don't want someone to do this for me. Im just confused as to how data in the text file will append to the HTML table without the actual HTML code. Can someone please help me get started?
Looks like you'll need to define the necessary HTML tags within your script. The meat of the html file will be these lines:
<tr>
<td>$first $last</td>
<td>$username</td>
<td>$email</td>
</tr>
These tags define a table row. You can parse the variables from the text files with awk and use them to fill in the html. The other html markup can be copy-pasted as static text into the output html file.
Edit: You can do this to grab the first and last name and print to the html file.
last = $1
first = $2
print " <tr>"
print " <td>" first " " last "</td>"
print " </tr>"
You just need to expand that to get the email and username.