I have tried everything i know and can't get an alterate tablerow class to work , without having all the rows duplicated or having a bunch of dummy rows inserted
Here is what i have
<script>
fetch("https://www63.myfantasyleague.com/2021/export?TYPE=league&L=43570&JSON=1").then(
res => {
res.json().then(
data => {
console.log(data.league.franchises.franchise);
if (data.league.franchises.franchise.length > 0) {
var temp = "";
temp += "<table id='league_emails'>";
temp += "<tbody>";
temp += "<tr><th>Franchise</th><th>Owner Name</th><th>Email</th></tr>";
data.league.franchises.franchise.forEach((itemData) => {
for (var i = 0; i < data.league.franchises.franchise.length; i++) {
if (i % 2)
temp += '<tr class="eventablerow">';
else
temp += '<tr class="oddtablerow">';
}
temp += "<td>" + itemData.name + "</td>";
temp += "<td>" + itemData.owner_name + "</td>";
temp += "<td>" + itemData.email + "</td>";
});
temp += "</tbody>";
temp += "</table>";
document.getElementsByClassName('commish_league_safe')[0].innerHTML = temp;
}
}
)
}
)
</script>
<div class="commish_league_safe"></div>
The issue lays here and i have tried a dozen things and wrapped it different ways
for (var i = 0; i < data.league.franchises.franchise.length; i++) {
if (i % 2)
temp += '<tr class="eventablerow">';
else
temp += '<tr class="oddtablerow">';
}
<script>
fetch(https://www63.myfantasyleague.com/2021/export?TYPE=league&L=43570&JSON=1).then(
res => {
res.json().then(
data => {
console.log(data.league.franchises.franchise);
if (data.league.franchises.franchise.length > 0) {
var temp = "";
temp += "<table id='league_emails'>";
temp += "<tbody>";
temp += "<tr><th>Franchise</th><th>Owner Name</th><th>Email</th></tr>";
data.league.franchises.franchise.forEach((itemData,i) => {
/* for (var i = 0; i < data.league.franchises.franchise.length; i++) { */
if (i % 2)
temp += '<tr class="eventablerow">';
else
temp += '<tr class="oddtablerow">';
/* } */
temp += "<td>" + itemData.name + "</td>";
temp += "<td>" + itemData.owner_name + "</td>";
temp += "<td>" + itemData.email + "</td>";
temp += "</tr>";
});
temp += "</tbody>";
temp += "</table>";
document.getElementsByClassName('commish_league_safe')[0].innerHTML = temp;
}
}
)
}
)
</script>
<div class="commish_league_safe"></div>
Just before ending the loop close the tr tag.
temp += "" + itemData.email + ""; after this line just add closing tag to append that to temp variable.
Related
I am trying to draw the 3rd and 4th triangle in the following picture with JS in HTML. I made two functions for the first two triangles, and I think I just need to twist the for loops a little bit for the 3rd and 4th ones but I don't know how to do it.
Here are the codes for the first two ones.
function draw1() {
let mytable = "<table>"
for (let i = 1; i < 6; i++) {
mytable += "<tr>";
mytable += "<td>" + "*".repeat(i) +"</td>";
}
mytable += "</tr>"
document.write(mytable)
}
draw1()
function draw2() {
let mytable = "<table>"
for (let i = 5; i > 0; i--) {
mytable += "<tr>";
mytable += "<td>" + "*".repeat(i) +"</td>";
}
mytable += "</tr>"
document.write(mytable);
}
draw2()
In order to rotate your * arrows, you don't have to redo the js. You can simply use the css direction: rtl rule for the table that your js creates. Like this:
table {
direction: rtl;
}
function draw1() {
let mytable = "<table>"
for (let i = 1; i < 6; i++) {
mytable += "<tr>";
mytable += "<td>" + "*".repeat(i) +"</td>";
}
mytable += "</tr>"
document.write(mytable);
}
draw1();
function draw2() {
let mytable = "<table>"
for (let i = 5; i > 0; i--) {
mytable += "<tr>";
mytable += "<td>" + "*".repeat(i) +"</td>";
}
mytable += "</tr>"
document.write(mytable);
}
draw2();
table {
direction: rtl;
}
You need add spaces before you add *. Since you are rendering HTML use for space character. Use 2 of them to appropriately cover area in loop.
function draw1() {
let mytable = "<table>";
for (let i = 1; i < 6; i++) {
mytable += "<tr>";
mytable += "<td>" + "*".repeat(i) +"</td>";
}
mytable += "</tr>";
document.write(mytable);
}
draw1()
function draw2() {
let mytable = "<table>";
for (let i = 5; i > 0; i--) {
mytable += "<tr>";
mytable += "<td>" + "*".repeat(i) +"</td>";
}
mytable += "</tr>";
document.write(mytable);
}
draw2();
function draw3() {
let mytable = "<table>";
for (let i = 1; i < 6; i++) {
mytable += "<tr>";
mytable += "<td>" + " ".repeat(5-i) + "*".repeat(i) +"</td>";
}
mytable += "</tr>";
document.write(mytable);
}
draw3();
function draw4() {
let mytable = "<table>";
for (let i = 5; i > 0; i--) {
mytable += "<tr>";
mytable += "<td>" + " ".repeat(5-i) + "*".repeat(i) +"</td>";
}
mytable += "</tr>";
document.write(mytable);
}
draw4();
Use the code below to create the pattern.
let n = 5; // you can take input from prompt or change the value
let string = "";
for (let i = 1; i <= n; i++) {
// printing spaces
for (let j = 0; j < n - i; j++) {
string += " ";
}
// printing star
for (let k = 0; k < i; k++) {
string += "*";
}
string += "\n";
}
console.log(string);
Here is code for the second pattern.
let n = 5; // you can take input from prompt or change the value
let string = "";
for (let i = 0; i < n; i++) {
// printing star
for (let k = 0; k < n - i; k++) {
string += "*";
}
string += "\n";
}
console.log(string);
I am trying to create a data table with my dataset and I want to reinitialize the same table for the new data. But it's getting appended to the same table rather than re-initialising it. This is my code
function showTab(name,data){
trheader = "";
trvalues = "";
trcontent = "";
trfoot = "";
table_id = "";
trheader += "<thead>";
for (i = 0; i < data[0].length; i++){
trheader += "<th>" + data[0][i]+ "</th>";
}
trheader += "</thead>";
if(data[1].length > 0){
for (i = 0; i < data[1].length; i++){
trvalues += "<tbody>"
trvalues += "<tr>"
trvalues += "<td class='center'>" + (i + 1) + "</td>";
trvalues += "<td class='center'>" + data[1][i][0] + "</td>";
for (j = 0; j < 4; j++ ){
trvalues += "<td class='center'>" + data[1][i][1][j].toLocaleString() + "</td>";
}
for(j = 4; j < 7; j++){
var patt = /^-/;
var color = data[1][i][1][j].toString();
if (color.match(patt)){
trvalues += "<td class='center redCell'>" + data[1][i][1][j].toFixed(2) + '%'+ "</td>";
}
else if(color == '0'){
trvalues += "<td></td>"
}
else{
trvalues += "<td class='center greenCell'>" + data[1][i][1][j].toFixed(2) + '%' + "</td>";
}
}
trvalues += "</tr>"
trvalues += "</tbody>"
}
}
if (data[2].length > 0)
{
trfoot += "<tfoot>"
trfoot += "<tr>"
trfoot += "<td></td>"
trfoot += "<td>Total</td>"
for (i = 0; i < 4; i++){
trfoot += "<td class='center'>" + data[2][i].toLocaleString() + "</td>";
}
for( i = 4; i < 7; i ++){
var patt = /^-/;
var color = data[2][i].toString();
if (color.match(patt)){
trfoot += "<td class='center redCell'>" + data[2][i].toFixed(2) + '%'+ "</td>";
}
else if(color == '0'){
trfoot += "<td></td>"
}
else{
trfoot += "<td class='center greenCell'>" + data[2][i].toFixed(2) + '%' + "</td>";
}
}
trfoot += "</tr>"
trfoot += "</tfoot>"
}
trcontent = trheader + trvalues + trfoot;
table = "#"+name;
$(table).html(trcontent);
$(table).DataTable({
"scrollY": "400px",
"scrollCollapse": true,
"paging": false,
"searching": false,
"destroy": true
});
// new addition code
var seen = {};
$('table tr').each(function() {
var txt = $(this).text();
if (seen[txt])
$(this).remove();
else
seen[txt] = true;
});
If I use $(table).html it will only load the last value that is trfooter. I have tried setting bDestroy for the table but nothing helps. Please help. Thanks.
This is my table skeleton
<table border = "1px"cellpadding="0" cellspacing="1" width="100%" id= <%= table&.table_name %> style="background: none repeat scroll 0% 0%;font-size:12px;">
</table>
You can destroy and re-declare your datatable to load the latest content
// First Declaration
var table = $('#myTable').DataTable();
$('#submit').on( 'click', function () {
$.getJSON( 'newTable', null, function ( json ) {
// Destroy
table.destroy();
$('#myTable').empty(); // empty in case the columns change
// ReDraw
table = $('#myTable').DataTable( {
columns: json.columns,
data: json.rows
} );
} );
} );
For more reference : https://datatables.net/reference/api/destroy()
I am trying to create dynamic table in js.
var tableBody = document.createElement('TBODY');
table.appendChild(tableBody);
for (var i = 0; i < results.rows.length; i++)
{
var tr = document.createElement('TR');
tableBody.appendChild(tr);
for(var j = 0; j < 7; j++)
{
var td = document.createElement('TD');
td.width = '75';
td.appendChild(document.createTextNode(results.rows.item(i).medicine_name));
tr.appendChild(td);
td.appendChild(document.createTextNode(results.rows.item(i).tm_1));
tr.appendChild(td);
td.appendChild(document.createTextNode(results.rows.item(i).tm_2));
tr.appendChild(td);
td.appendChild(document.createTextNode(results.rows.item(i).tm_3));
tr.appendChild(td);
td.appendChild(document.createTextNode(results.rows.item(i).dosage));
tr.appendChild(td);
td.appendChild(document.createTextNode(results.rows.item(i).diagnosis));
tr.appendChild(td);
td.appendChild(document.createTextNode(results.rows.item(i).instructions));
tr.appendChild(td);
}
}
myTableDiv.appendChild(table);
When I execute this code, 7 rows will be created with all the items together. I want to insert each of the data in seperate columns. How can I do it? I want medicine_name to be in one column. tm_1 to be in another and so on.
**The rows will be decided based on results.rows.length value and the number of columns is 7
Totally you have 7 columns and 7 rows.So in first text node td.appendChild(document.createTextNode(results.rows.item((i*7) + 0).medicine_name)); And in second text node use
td.appendChild(document.createTextNode(results.rows.item((i*7) + 1).tm_1)); and so on...
for (var i = 0; i < results.length; i++) {
var html = "";
html += "<tr>";
html += "<td>" + results[i].MedicineName + "</td>";
html += "<td>" + results[i].tm_1+ "</td>";
html += "<td>" + results[i].tm_2+ "</td>";
html += "<td>" + results[i].dosage+ "</td>";
html += "<td>" + results[i].diagnosis+ "</td>";
html += "<td>" + results[i].instructions+ "</td>";
html += "</tr>";
$('#myTableDiv').append(html);
}
<script>
$(document).ready(function () {
var results = getResults();
createTable(results);
});
function createTable(results) {
for (var i = 0; i < results.length; i++) {
var html = "";
html += "<tr>";
html += "<td>" + results[i].MedicineName + "</td>";
html += "<td>" + results[i].tm_1 + "</td>";
html += "<td>" + results[i].tm_2 + "</td>";
html += "<td>" + results[i].dosage + "</td>";
html += "<td>" + results[i].diagnosis + "</td>";
html += "<td>" + results[i].instructions + "</td>";
html += "</tr>";
$('#myTableDiv').append(html);
}
}
function getResults() {
var Results = new Array(0);
for (var j = 0; j < 4; j++) {
var data = new medicine;
data.MedicineName = "Medicine" + j;
data.tm_1 = "tm1" + j;
data.tm_2 = "tm2" + j;
data.dosage = "tm3" + j;
data.diagnosis = "tm4" + j;
data.instructions = "tm5" + j;
Results.push(data);
}
return Results;
};
function medicine() {
var MedicineName = new String();
var tm_1 = new String();
var tm_2 = new String();
var dosage = new String();
var diagnosis = new String();
var instructions = new String();
}
</script>
I am reading data from an XML file then displaying it in a table in Javascript. However, only the first row displays well and the rest of the rows dont as follows:
results = "<table class= \"table table-condensed table-hover table-bordered table-striped\">";
results += "<caption>Payment History</caption>";
results += "<thead>";
results += "<tr>";
results += "<th>User</th>";
results += "<th>Video Name</th>";
results += "<th>Payment Date</th>";
results += "<th>Time</th>";
results += "</tr>";
results += "</thead>";
results += "<tbody>";
for (var index = 0; index < items.length; index++)
{
var deviceNumElement = items[index].getElementsByTagName("devicenumber")[0];
var VideoNumElement = items[index].getElementsByTagName("videonumber")[0];
var dateElement = items[index].getElementsByTagName("paymentdate")[0];
var timeElement = items[index].getElementsByTagName("paymenttime")[0];
if (deviceNumElement && VideoNumElement && dateElement && timeElement)
{
deviceNum[index] = deviceNumElement.firstChild.data;
videoNum[index] = VideoNumElement.firstChild.data;
paidDate[index] = dateElement.firstChild.data;
paidTime[index] = timeElement.firstChild.data;
results += "<tr>";
results += "<td>"+ deviceNum[index] + "</td>";
results += "<td>" + videoNum[index] + "</td>";
results += "<td>" + paidDate[index] + "</td>";
results += "<td>" + paidTime[index] + "</td>";
results += "</tr>";
}
results += " </tbody>";
results += " </table>";
alert(results);
var div = document.getElementById("paymentDetails");
div.innerHTML = results;
}
The data gets displayed as follows:
Payment History
User VideoName Payment Date Time
43CA3KZXYQGBK Animal Series 14-01-2014 14:12:20
43CA3KZXYQGBKAnimalSeries10-01-201415:40:12
43CA3KZXYQGBKAnimalSeries10-01-201403:21:15
43CA3KZXYQGBKAnimalSeries10-01-201416:39:28
The XML Data:
<xml>
<payments>
<devicenumber>43CA3KZXYQGBK</devicenumber>
<videonumber>1234567</videonumber>
<paymentdate>2014-01-11</paymentdate>
<paymenttime>19:38:19</paymenttime>
</payments>
<payments>
<devicenumber>43CA3KZXYQGBK</devicenumber>
<videonumber>1234567</videonumber>
<paymentdate>2014-01-10</paymentdate>
<paymenttime>19:38:19</paymenttime>
</payments>
<payments>
<devicenumber>43CA3KZXYQGBK</devicenumber>
<videonumber>1234567</videonumber>
<paymentdate>2014-01-01</paymentdate>
<paymenttime>19:38:19</paymenttime>
</payments>
<payments>
<devicenumber>43CA3KZXYQGBK</devicenumber>
<videonumber>1234567</videonumber>
<paymentdate>2014-01-09</paymentdate>
<paymenttime>19:38:19</paymenttime>
</payments>
</xml>
What could i be doing wrong?
The closing tag for table is in the for-loop. You need to put it outside the for-loop:-
for (var index = 0; index < items.length; index++)
{
var deviceNumElement = items[index].getElementsByTagName("devicenumber")[0];
var VideoNumElement = items[index].getElementsByTagName("videonumber")[0];
var dateElement = items[index].getElementsByTagName("paymentdate")[0];
var timeElement = items[index].getElementsByTagName("paymenttime")[0];
if (deviceNumElement && VideoNumElement && dateElement && timeElement)
{
deviceNum[index] = deviceNumElement.firstChild.data;
videoNum[index] = VideoNumElement.firstChild.data;
paidDate[index] = dateElement.firstChild.data;
paidTime[index] = timeElement.firstChild.data;
results += "<tr>";
results += "<td>"+ deviceNum[index] + "</td>";
results += "<td>" + videoNum[index] + "</td>";
results += "<td>" + paidDate[index] + "</td>";
results += "<td>" + paidTime[index] + "</td>";
results += "</tr>";
}
}
results += " </tbody>";
results += " </table>";
alert(results);
var div = document.getElementById("paymentDetails");
div.innerHTML = results;
This is a rather simple question, I am having problems inserting data from Javascript into an HTML table.
Here is an excerpt of my JavaScript:
UPDATED - I got rid of the two loops and simplified it into one, however there is still a problem..
for (index = 0; index < enteredStrings.length; index++) {
output.innerHTML += "<td>" + enteredStrings[index] + "</td>"
+ "<td>" + enteredStringsTwo[index] + "</td>";
nameCounter++;
total.innerHTML = "Total: " + nameCounter;
}
And here is an except of my HTML page:
<table id="nameTable">
<tr>
<th>First</th><th>Last</th>
</tr>
Updated Picture:
Try this (edited):
var tableContent = '<tr>';
for (index = 0; index < enteredStrings.length; index++) {
tableContent += "<td>" + enteredStrings[index] + "</td>";
nameCounter++; // I don't know if this should be there,
// logically the counter should be incremented here as well?
total.innerHTML = "Total: " + nameCounter;
}
tableContent += '</tr><tr>';
for (index = 0; index < enteredStringsTwo.length; index++) {
tableContent += "<td>" + enteredStringsTwo[index] + "</td>";
nameCounter++;
total.innerHTML = "Total: " + nameCounter;
}
tableContent += '</tr>';
output.innerHTML += tableContent;
Edit2 (for updated question code):
var tableContent = '<tr>';
for (index = 0; index < enteredStrings.length; index++) {
tableContent += "<td>" + enteredStrings[index] + "</td>"
+ "<td>" + enteredStringsTwo[index] + "</td>";
nameCounter++;
total.innerHTML = "Total: " + nameCounter;
}
tableContent += '</tr>';
output.innerHTML += tableContent;
Edit3 (after looking at the code sent in email):
var tableContent = "";
for (index = 0; index < enteredStrings.length; index++) {
tableContent += "<tr><td>" + enteredStrings[index] + "</td>"
+ "<td>" + enteredStringsTwo[index] + "</td></tr>";
nameCounter++;
total.innerHTML = "Total: " + nameCounter;
}
output.innerHTML = tableContent;
instead of closing the td you are opening new ones
try
for (index = 0; index < enteredStrings.length; index++) {
output.innerHTML += "<td>" + enteredStrings[index] + "</td>";
total.innerHTML = "Total: " + nameCounter;
}
for (index = 0; index < enteredStringsTwo.length; index++) {
output.innerHTML += "<td>" + enteredStringsTwo[index] + "</td>";
nameCounter++;
total.innerHTML = "Total: " + nameCounter;
}
UPDATE:
you are appending the html to the table instead of the row.
in this case, the browser created a row for you automatically after the each td is appended.
With slight modifications in your code,
var outputTbl = document.getElementById('nameTable');
var output = document.createElement("tr");
outputTbl.appendChild(output);
for (index = 0; index < enteredStrings.length; index++) {
output.innerHTML += "<td>" + enteredStrings[index] + "</td>";
total.innerHTML = "Total: " + nameCounter;
}
for (index = 0; index < enteredStringsTwo.length; index++) {
output.innerHTML += "<td>" + enteredStringsTwo[index] + "</td>";
nameCounter++;
total.innerHTML = "Total: " + nameCounter;
}
If you need to add inner html code here.
<table id="nameTable" style="width:300px;">
<tr>
<th>First</th><th>Last</th>
</tr>
</table>
You can use Jnerator in this case.
If this is your data:
var data = [
{ first: 'Cole', last: 'Alan'},
{ first: 'Michael', last: 'Scott'}
]
You can add them to you table in next way:
for(var i=0; i<data.length; i++) {
var item = data[i];
var row = $j.tr({ child:[$j.td(item.first), $j.td(item.last)] });
nameTable.appendChild(row.dom());
}
nameTotal.innerHTML = 'Total: ' + data.length;
This is example.