when you use jquery.tablesorter, and you manually update the table html, for example, append a tr element to the body
$("table tbody").append("<tr><td class='center'>value1</td><td>value2</td></tr>");
you can call
$('table').trigger("update");
and all works ok, now, i'v been asked to use jquery.datatables, and i'm trying to do the same but no result, i'm thinking to use the fnAddData function, but, what about if a need a css class in a td for styling like in the example above??, exist somthing in DataTables like the update of tablesorter??
need some help here
After a lots of test, this was my final solution to what i was trying to do, first, append the html to the table tbody, then call fnDestroy and dataTable again, this do exactly what i wanted
var a = $("table").dataTable();
$("table tbody").append("<tr><td class='center'>value1</td><td>value2</td></tr>");
a.fnDestroy();
a.dataTable();
hope its help someone else in the future
Related
I have a HTML table in my web-application to which I allow my users to dynamically add rows using the onclick event.
However when I try to get the HTML contents of this table, (which is my objective) using either :
$("#table1").html()
or
var x = document.getElementById("table1").innerHTML;
I get only the initial table, and not the rows that were added dynamically.
Interestingly in jQuery when I use
$("#nutritiontable ").text()
I get details of the dynamic rows also, but obviously not the html tags for which I have use. I want both.
Want to know if I missed something, or if anyone knows of any other way to get the entire HTML table with the dynamic content and with the html tags intact.
If you add rows by
$("#table1").append("<tr><td>something</td></tr>")
Then
$("#table1").html()
Will give you everything.
You can use append() function in jQuery.
$("#tableid").append('<tr><td>Row text</td></tr>');
I am trying to understand how to ensure my code runs after an external JS file has finished running.
If you need a specific implementation, you can look at this SO question, which I am trying to help answer: How to get the last table row no matter the sort?
TDLR: The script found in bootstrap-sortable.js runs a table sort. After this table sort is complete; I want to make it so that I can run a snippet, which will add a simple CSS class to the last element in the freshly sorted table. The adding of the class can easily be achieved by this JQuery snippet:
var lastRow = $(this).closest("table").find("tbody tr:last");
if(!lastRow.hasClass("dropup")){
// Removing dropup class from the row which owned it
$(this).closest("table").find("tbody tr.dropup").removeClass("dropup");
// Adding dropup class to the current last row
lastRow.addClass("dropup");
}
I would like to know:
Is it possible to run my script after the external script is done running?
If not, can you explain why?
I have already considering modifying bootstrap-sortable.js to add my script to it, is this the best recommendable approach?
Bonus round! (only if you feel you need the challenge).
Is there a better, do-it-yourself, solution for sorting the table other than using bootstrap-sortable.js for the linked question?
Thanks everyone!
I want to thank Dave Newton for leading me to the answer to this question which is quite simple.
JSFiddle
$(document).ready(function() {
$("#MyTable").on('sorted', function(){
var lastRow = $("#MyTable").find("tbody tr:last");
// Removing dropup class from the row which owned it
$("#MyTable").find("tbody tr.dropup").removeClass("dropup");
// Adding dropup class to the current last row
lastRow.addClass("dropup");
});
});
This is awesome, simple and lightweight, it also adheres to the linked question. Thanks Dave!
I have a problem with Jquery Tablesorter where I am adding data dynamically to a pre-existing table.
According to the docs, after adding content, all that needs to be done is for $("table").trigger("update") to tbe run to tell tablesorter than new content has been added, however in reality this appears not to work.
See http://jsfiddle.net/7Wurn/9/ for an example. Clicking on the HTML table headers will order the columns, but after adding a new row the new row does not get sorted.
It seems like something minor is wrong, as the example used on the Tablesorter website uses almost identical code to the code I've added on JSFiddle. The only difference is that the $("table").trigger("update"); is run as part of a callback after an Ajax call.
Any ideas what is wrong?
I also faced the same problem when using datatables in jQuery. When you are appending it to the table you are not appending the data to same tbody element. Instead your data is being added to a new tbody element. The table format should be<thead></thead> and then <tbody></tbody>.
Do something like this:
$("#tableId tbody").append(someContent);
I have updated your fiddle http://jsfiddle.net/7Wurn/68/. Also please use jquery 1.9.1. Its not working in 2.0b1. Don't know why
I have been playing with this and found that it's related to how, in the DOM, a tbody tag is being rendered.
I got it to work for the first entry by doing the following:
$("table tbody:first").before('<tr><td>Aaphod</td><td>Beeblebrox</td> <td>28</td> <td>$9.99</td> <td>20%</td><td>jul 6, 2006 8:14 am</td></tr>');
That's because it sticks it into the first tbody, and tablesorter recognizes it. However, subsequent additions cause it to be inserted as a new tbody tag.
I'm going to keep playing with it and will update if I get something!
im having a firefox issue where i dont see the wood for the trees
using ajax i get html source from a php script
this html code contains a tag and within the tbody some more tr/td's
now i want to append this tbody plaincode to an existing table. but there is one more condition: the table is part of a form and thus contains checkboxe's and drop down's. if i would use table.innerHTML += content; firefox reloads the table and reset's all elements within it which isnt very userfriendly as id like to have
what i have is this
// content equals transport.responseText from ajax request
function appendToTable(content){
var wrapper = document.createElement('table');
wrapper.innerHTML = content;
wrapper.setAttribute('id', 'wrappid');
wrapper.style.display = 'none';
document.body.appendChild(wrapper);
// get the parsed element - well it should be
wrapper = document.getElementById('wrappid');
// the destination table
table = document.getElementById('tableid');
// firebug prints a table element - seems right
console.log(wrapper);
// firebug prints the content ive inserted - seems right
console.log(wrapper.innerHTML);
var i = 0;
// childNodes is iterated 2 times, both are textnode's
// the second one seems to be a simple '\n'
for(i=0;i<wrapper.childNodes.length;i++){
// firebug prints 'undefined' - wth!??
console.log(wrapper.childNodes[i].innerHTML);
// firebug prints a textnode element - <TextNode textContent=" ">
console.log(wrapper.childNodes[i]);
table.appendChild(wrapper.childNodes[i]);
}
// WEIRD: firebug has no problems showing the 'wrappid' table and its contents in the html view - which seems there are the elements i want and not textelements
}
either this is so trivial that i dont see the problem OR
its a corner case and i hope someone here has that much of expirience to give an advice on this - anyone can imagine why i get textnodes and not the finally parsed dom elements i expect?
btw: btw i cant give a full example cause i cant write a smaller non working piece of code
its one of those bugs that occure in the wild and not in my testset
thx all
You are probably running into a Firefox quirk of following the W3C spec. In the spec the whitespace between tags are "text" nodes instead of elements. These TextNodes are returned in childNodes. This other answer describes a workaround. Also Using something like JQuery makes this much easier.
I would expect this behavior in any browser as the += operand overwrites what is already in the table by definition. Two solutions:
Instead of receiving HTML code from your PHP file, have the PHP generate a list of items to add to the table. Comma/tab separated, whatever. Then use Table.addRow(), Row.addCell() and cell.innerHTML to add the items to the table. This is the way I would suggest doing it, no point in creating GUI elements in two separate files.
The other solution is to save all the form data that's already been entered to local JavaScript variables, append the table, and then reload the data into the form fields.
Well, returning a JSON object with the new data seems like the best option. Then, you can synthesize the extra table elements by using it.
In case one is forced to get plain HTML as response, it is possible to use var foo = document.createElement('div');, for example, and then do foo.innerHTML = responseText;. This creates an element that is not appended to anything, yet hosts the parsed HTML response.
Then, you can drill down the foo element, get the elements that you need and append them to the table in a DOM-friendly fashion.
Edit:
Well, I think I see your point now.
wrapper is a table element itself. The nodes reside under the tbody, a child of wrapper which is its lastChild (or you can access it via its tBodies[0] member, in Firefox).
Then, using the tBody element, I think that you would be able to get what you want.
BTW, You do not need to append the wrapper to the document before appending its children to the table, so no need to hide it etc.
What is the best plain javascript way of inserting X rows into a table in IE.
The table html looks like this:
<table><tbody id='tb'><tr><td>1</td><td>2</td></tr></tbody></table>
What I need to do, is drop the old body, and insert a new one with 1000 rows. I have my 1000 rows as a javascript string variable.
The problem is that table in IE has no innerHTML function. I've seen lots of hacks to do it, but I want to see your best one.
Note: using jquery or any other framework does not count.
Here's a great article by the guy who implemented IE's innerHTML= on how he got IE to do tbody.innerHTML="<tr>...":
At first, I thought that IE was not
capable of performing the redraw for
modified tables with innerHTML, but
then I remembered that I was
responsible for this limitation!
Incidentally the trick he uses is basically how all the frameworks do it for table/tbody elements.
Edit: #mkoryak, your comment tells me you have zero imagination and don't deserve an answer. But I'll humor you anyway. Your points:
> he is not inserting what i need
Wha? He is inserting rows (that he has as an html string) into a table element.
> he also uses an extra hidden element
The point of that element was to illustrate that all IE needs is a "context". You could use an element created on the fly instead (document.createElement('div')).
> and also the article is old
I'm never helping you again ;)
But seriously, if you want to see how others have implemented it, take a look at the jQuery source for jQuery.clean(), or Prototype's Element._insertionTranslations.
Do as jQuery does it, eg. add <table> and </table> around it, insert into document and move the nodes you want to where you want them and ditch the temp-element.
the code ended up being this:
if($.support.scriptEval){
//browser needs to support evaluating scripts as they are inserted into document
var temp = document.createElement('div');
temp.innerHTML = "<table><tbody id='"+bodyId +"'>"+html;
var tb = $body[0];
tb.parentNode.replaceChild(temp.firstChild.firstChild, tb);
temp = null;
$body= $("#" + bodyId);
} else {
//this way manually evaluates each inserted script
$body.html(html);
}
Things that beed to exist beforehand: a table that has a body with id of 'bodyId'. $body is a global variable (or the function has a closure on it), and there is a bit of jquery in there too, because IE does not evalute scripts that are inserted into the html on the fly.
I had the same problem (as do lots of other people) and after a lot of playing around here's what I got to work.
You have to make a tr via document.createelement ('tr') then make a td, the same way.
appendchild the td to the tr, appendchild the tr to tbody (not table) THEN you can innerhtml the td you created and it will work.
This was ie8 I was using. Basically the table structure has to be made with createelement but the rest of it can be innerHTMLed. I was doing this watching in the IE8 debugger and it would say it would add it (if I did tr.innerhtml="blah") and give no error, but it wouldn't display, and the html dom view showed a very broken table (no td ever showed up, but the /td did)
So when finally I did the tr AND td by createelement calls, it created a correct looking dom and drew the page correctly.