I get data from the user and put it into the table by collating, I want to show a random line from the table I want to delete the other rows
function bilgi(){
var random = Math.floor(Math.random() * (allobjs.length - 1) ) + 1;
if (allobjs[random].id != 'blank'){
allobjs[random].animate({fill: 'rgb(19, 167, 236)'}, 1000);
$(function() {
$.getJSON('/static/yer.json', function(data) {
var deger = $("input[name=deger]").val()
var al = deger.split(",")
$.each(data, function(i, f) {
if(f.plaka == random){
var tblRow = "<tr>" +
"<td>" + "<img class='aaa'src='/static/bayrak.jpg' alt='' />" + "</td>" +
"<td>" + deger + "</td>" +
"<td>" + f.yerler + "</td>" +
"<td>" + f.bolge + "</td>" +
"<td>" + f.ili + "</td>" +
"<td>" + f.teskilati + "</td>" +
"<td>" + f.acm + "</td>" +
"</tr>"
$("tbody").append(tblRow);
}
});
$("tbody tr").hide()
var toplam= $("tbody tr").size()
ratgel=Math.floor(Math.random() * toplam);
$("tbody tr").eq(ratgel).show(1000)
});
});
}}
In javascript add class NOSHOW to each tr you want to hide Then using css .NOSHOW{display:none;} If you want a complete solution show your html.
something like the following might work:
At the start of your function add the following:
var tr = getElementsByTagName('tr');
for(var i = 0; i < tr.length;i++){
tr[i].className += "noshow";
}
then in you html add:
<style>
.noshow{
display:none;
}
</style>
This should work as you then append the row you want to the end of the table.
Later, when you want to display the entire table again you can use:
element.classList.remove("noshow");
I have a code that automatically creates a table for me. It gathers information from a saved file, however, I have two of these files and currently can call one at the time. How can I possibly wrap this code in a function so that there are two calls on the same code with different information that will be put in it? So right now I am using getElementById on file "house-data" but I also want to have this same js on a file "senate-data"
I thought of creating some sort of if statement where if(you have one file): do this
else
do that. But this method doesnt work.
var table = "";
var cols = 1;
var members = data.results[0].members;
var rows = members.length;
table += "<tr>" +
"<th>" + "Full Name" + "</th>" +
"<th>" + "Party" + "</th>" +
"<th>" + "State" + "</th>" +
"<th>" + "Seniority"+ "</th>" +
"<th>" + "Total Votes"+ "</th>" + "</tr>";
for (var r = 0; r < rows; r++) {
table += "<tr>";
for (var c = 0; c < cols; c++) {
table +=
"<td>" + members[r].first_name +", "+
(members[r].middle_name || " ") +" "+
members[r].last_name + "</td>";
table += "<td>" + members[r].party + "</td>" + "<td>" + members[r].state + "</td>" + "<td>" + members[r].seniority + "</td>";
if (members[r].votes_with_party_pct === undefined) {
table += "<td>" + "-" + "</td>"
} else {
table += "<td>" + members[r].votes_with_party_pct + "%" + "</td>"
}
}
table += "<tr>";
}
document.getElementById("house-data").innerHTML = JSON.stringify(table);
function tableCreator(elementID, data) {
[...]
document.getElementById(elementID)[...]
}
above will work for you and keep in mind to check
var members = data.results[0].members;
is available before going to for loop
One more thing is here that if I use the alert() at end of the ajax code the code is working fine. here whatever the ajax response comes has to be appended to the existing table.
for (var i = 1; i < rows.length; i++) {
var cells = splitCSVtoCells(rows[i], ",");
var obj = {};
for (var j = 0; j < cells.length; j++) {
obj[first_Row_Cells[j]] = cells[j];
}
jsonArray.push(obj);
}
console.log(jsonArray);for (var i = 0; i < jsonArray.length-1; i++) {
html += "<tr id=\"rowitem" + i + "\"><td id=\"rownum"+i+ "\" style=\"display:none;\">" + i + "</td><td> " + jsonArray[i].Clientgroup + " </td>";
html += "<td>" + jsonArray[i].hostgroup + "</td>";
html += "<td>" + jsonArray[i].server + "</td>";
html += "<td>" + jsonArray[i].Group + "</td>";
html += "<td>" + jsonArray[i].user + "</td>";
html += "<td>" + jsonArray[i].ticket + "</td>";
html += "<td>" + jsonArray[i].requesttype + "</td>";
$.ajax({
url : "BulkValidateServlet",
type : "POST",
data : {clientgroup: jsonArray[i].Clientgroup, hostgroup: jsonArray[i].hostgroup, server: jsonArray[i].server
},success : function(response){
//alert("i in of ajax:"+i);
status = response;
if (status) {
//alert("outside if: " + status);
//$('<img src=\'images/check_mark.png\' height=\'20\' width=\'20\'/></td>').appendTo(tr);
html += "<td id=\"result"+ i + "\"><img src='images/check_mark.png' height='20' width='20'/></td>";
} else {
//alert("outside else: " + status);
//$('<img src=\'images/cross_mark.png\' height=\'20\' width=\'20\'/></td>').appendTo(tr);
html += "<td id=\"result"+ i + "\"><img src='images/cross_mark.png' height='20' width='20'/></td>";
}
console.log("Data: " + status);
// alert("Data: " + status );
//return status;
}
});
console.log("outside: " + status);
alert();
}
document.getElementById('tbodyLeads').innerHTML = html;
}
You can try adding
async: false
to your ajax attributes.
Refer to this link.
i have a board that contains 9 small boards and each small board contains 9 cells (Ultimate TicTacToe).
i'm trying to use the click function and print "x" on the clicked button but it doesnt change the text of the button and i dont have an idea why.
please help me.
here is the code:
<script>
var bigTable = "<table align='center' value='bigBoard' border='0' cellpadding='1' cellspacing='1'\><tbody>";
for (var k = 0; k < 9; k++)
{
if (k % 3 === 0)
{
bigTable += "<tr>";
}
bigTable += "<td>";
var mytable = "<table value='smallBoard'+k border='1' cellpadding='1' cellspacing='1'><tbody><tr>";
for (var j = 0; j < 3; j++)
{
for (var i = 0; i < 3; i++)
{
var position = +k + "," + j + "," + i;
mytable += "<td><button class='a' id=" + position + " type='submit' name=" + position + "></button</td>";
}
mytable += "</tr><tr>";
}
mytable += "</tr></tbody></table>";
bigTable += mytable + "</td>";
if (k % 3 === 2)
{
bigTable += "</tr>";
}
}
bigTable += "</tbody></table>";
document.write(bigTable);
</script>
<script>
$(document).ready(function() {
$(".a").click(function() {
var buttonPressed = $(this).attr("id");
jQuery.ajax({
data:buttonPressed,
url:"board",
success: function(responseText){
//alert("acac" + buttonPressed);
$("#" + buttonPressed).text(responseText);
}
});
});
});
</script>
This is what you are doing wrong, change this:
var position = +k + "," + j + "," + i;
to something like,
var position = +k + "-" + j + "-" + i;
Change the type of button from submit to button because you are calling Ajax request, not submitting the form.
I ran this jsfiddle and it works fine
Try it
http://jsfiddle.net/P8wrb/1/
Just declare a variable for your clicked button and then set the .text for it by reference
$(".a").click(function() {
var that = $(this);
....
that.text('X');
}
When I parse this object with the function below, the first value that I get in the table is "undefined". Any idea as to why this could be happening? Am I parsing incorrectly? Should I be using a different loop?
var students = [
{
fn : "Stone",
ln : "Carpenter",
scores : [61,99,73,68,80,62,176,78]
},
{
fn : "Samson",
ln : "Sears",
scores : [68,193,91,190,95,65,171,75]
},
{
fn : "Quin",
ln : "Morton",
scores : [79,95,161,92,182,163,198,182]
},
{
fn : "Qunitessa",
ln : "Hardy",
scores : [99,65,75,69,77,67,86,78]
},
{
fn : "Ashley",
ln : "England",
scores : [147,70,81,64,148,71,70,63]
},
{
fn : "Thaddeus",
ln : "Hutchinson",
scores : [99,190,188,185,160,88,89,76]
},
{
fn : "Yeo",
ln : "Hayes",
scores : [88,64,199,165,198,76,74,81]
},
{
fn : "Rylee",
ln : "Larson",
scores : [71,126,63,71,168,173,175,88]
}
];
function updateTable(obj) {
var rows,
len;
len = obj.length;
rows = "<table>"
rows += "<tbody>"
for (var i in obj) {
var scoreLen,
score,
sum;
scoreLen = obj[i].scores.length;
scores = obj[i].scores;
rows += "<tr>"
rows += "<td>" + obj[i].fn + "</td>"
rows += "<td>" + obj[i].ln + "</td>"
for(j=0; j<scoreLen; j+=1) {
rows += "<td>" + scores[j] + "</td>"
}
rows += "<td>" + arraySum(obj[i].scores) + "</td>"
rows += "</tr>"
}
document.getElementsByTagName('body')[0].innerHTML = rows
console.log(rows)
}
Below is the correct implementation of updateTable function. students is an array of objects, but you are trying to access it as an object of objects. That is why it is undefined.
var students = [{obj1:stuff}, {obj2:stuff}, ... ,{objn:stuff}]
students[0] returns {obj1:stuff}, students[1] returns {obj2:stuff} and so on.
function updateTable(arr) {
var rows,
len;
len = arr.length;
rows = "<table>"
rows += "<tbody>"
for (var i =0; i< len; i++) {
var scoreLen,
score,
sum;
scoreLen = arr[i].scores.length;
scores = arr[i].scores;
rows += "<tr>"
rows += "<td>" + arr[i].fn + "</td>"
rows += "<td>" + arr[i].ln + "</td>"
for(j=0; j<scoreLen; j+=1) {
rows += "<td>" + scores[j] + "</td>"
}
rows += "<td>" + arraySum(arr[i].scores) + "</td>"
rows += "</tr>"
}
document.getElementsByTagName('body')[0].innerHTML = rows
console.log(rows)
}
This will iterate over properties of the array other than the array elements like length. You should iterate using a for loop like this:
for(var i = 0; i < students.length; i++) {
...
}
When iterating over items in an array, never use for .. in. Instead, always use a numerical loop, such as:
for (var i=0, len=myArray.length; i<len; ++i){ … }
Or if you want to iterate from back to front with less typing:
for (var i=myArray.length;i--;){ … }