I have an initial page (program_finder.asp) that makes an AJAX call for some results:
$('#filtersForm').submit(function(){
$('#resultsPane').load('program_finder_backend.asp',$('#filtersForm').serializeArray());
return false;
This works just fine. I submit parameters, the backend page runs some SQL, and dumps to my front end resultsPane area just fine. The returned info is a table.
What I want to do is make the table of returned information sortable by column. I like this function and want to use it:
http://www.kryogenix.org/code/browser/sorttable/
For a a table with a class of sortable, the user can click on the column heads to sort the content. I think that is user friendly. it works fine is a static page.
I load that with
script src="sorttable.js"></script
in the head of initial document.
I am pretty sure the problem is that the returned table contents don't exist in the DOM on initial page load and therefore sorttable.js can't find them. I'm pretty sure I need a .live() or a .on() or a .delegate() someplace to make the returned table results visible to initial page, but I don't know how to do that.
I think I want something like:
$('table.sortable').on('click', function(){
// but here I don't know how to call sorttable();
});
If this isn't clear I will add more informaton.
EDIT: I should have added that the sorttable documentation says this:
Sorting a table added after page load
Once you've added a new table to the page at runtime (for example, by
doing an Ajax request to get the content, or by dynamically creating
it with JavaScript), get a reference to it (possibly with var
newTableObject = document.getElementById(idOfTheTableIJustAdded) or
similar), then do this:
sorttable.makeSortable(newTableObject);
but that doesn't help me any. I don't understand it.
EDIT#2:
Putting this up here to make it easier to read. I tried:
$('#filtersForm').submit(function(){
$('#resultsPane').load('program_finder_backend.asp', $('#filtersForm').serializeArray(), function() {
sorttable.makeSortable($("#resultsPane").find("table")[0]);
return false});
});
per Dogbert with no luck. That transfers away from the initial page directly to the backend page.
EDIT#3
Adding in Adeneo's code from the referenced fiddle:
$('#filtersForm').submit(function(e){
e.preventDefault();
$('#resultsPane').load('program_finder_backend.asp', $('#filtersForm').serializeArray(), function() {
sorttable.makeSortable($(this).find("table").get(0));
});
});
This seems to work.I would have never figured that out. Thanks to Dogbert as well.