remove() and destroy() won't fully work- Also, can't create second table after remove()/destroy()
remove() and destroy() won't fully work- Also, can't create second table after remove()/destroy()
I would like to be able to create a second table after removing/destroying the first table.
The flow is Map Selection > Ajax > GeoServer > Feature Data Comes Back > DataTables table created
Then I have a "clearAll()" function where I remove the map selection and attempt to destroy the table in order for another selection to be made and another table to be created.
This removes styling but leaves the data: $("#example").DataTable().destroy();
This hide/removes the entries: $("#example").remove();
But the "Show entries" dropdown box is stil there.
However, using both of these together effectively clears everything of the screen.
NOW, if I try to make another map selection, and thus generate a new table I get this error:
TypeError: Cannot read property 'outerHTML' of null
at GenerateTable (index.html:174)
at HTMLInputElement.onclick (index.html:82)
I am able to select features like normal and everything works fine. But it's still showing as "null" despite the fact that I'm logging to the console all changes. It's relying on a layer to be active (that info get's sent to the ajax link), but despite the fact that I've verified via console that a layer is active, it's still showing as null. So, is there another way to go about this?
This question has accepted answers - jump to:
Answers
I think the first thing to do is if you're destroying the table and need the DOM elements to go also, pass true to the
destroy
function, this removes the DataTables as well as the DOM elements for it.Would I just set that up like this?
I'm not sure how else to set it to true. I tried that and it did not work.
EDIT... This does not actually work correctly.
I have it set up like this;
when a user turn on a map layer.. for instance a "Cities" layer.. i have that assigned to a variable called "activeOverlay".
User makes selection on the map and the table is created via an Ajax call to GeoServer.
Now, when that layer is toggled off, activeOverlay is set to "null" until another layer is turned on. For instance, "States" Layer... activeOverlay becomes "States".
BUT.. even though the selection/Ajax/GeoServer stuff still works and sees activeOverlay as "States" now... DataTables does not... It still has activeOverlay as "Cities" or "Null".
The activeOverlay variable IS global.
Nevermind.. I got it. Right at the point where you create the table, you need to add that in like so:
After I destroy one table and try to create another.. I get this error:
When you set activeOverlay to null, datatables still thinks there is a table as it has it's own internal structure for tables created with .DataTable(). Setting your activeOverlay to null does not destroy your datatable, it only means you've changed your reference to it.
(The the
destroy
option is not the same as thedestroy
api function)When you close the activeOverlay you need to also call
destroy
on the DataTable you created previously if it's no longer needed. Essentially, it sounds like you're losing track of tables that have been created and not cleaning up those that are done with before trying to replace them with a different table.This is my delete code... I thought that it does what you're suggesting already...?
This removes all data from the leaflet sidebar (where my table is placed)... BUT..
when I try to make a new table.. despite the fact that i turn activeOverlay on again, the table is always blank. It will not create a second table.
Do I have to do anything with the container? It's set up like this:
Hmm, when you call
$("#featureTable").remove()
the table node is no longer in the DOM, do you create a new#featureTable
node before you try to create a new datatable?I believe I do.. unless there is something I'm missing. The table are created after an ajax call to GeoServer. In the ajax portion.. there is the "success" inscructions like below:
The "handleJson" funtion includes (among other instructions) the code to create a table. It's the first thing that happens in the handleJson function:
I've placed console.log() code right inside the handleJson function and then right inside the table creation code (right under the (document).ready) portion just to make sure it was being hit each time. Console shows both portions of code are hit, but no new table is generated. :-(
$('#featureTable').DataTable(...)
says: Create a datatable on the node with the idfeatureTable
. This means the jQuery selector must find a node with that id. If you have called$("#featureTable").remove()
there will NOT be a node with that id, you've removed it from the DOM, no nodes from the selector == no datatable. You can check this by Inspect-ing the page to see the live DOM.You haven't shown where you create the #featureTable node again after calling .remove() on it.
It's created again when another ajax/geoserver call is made. Is that what you're referring to??
1)I press a button to clear features/table which calls remove().. DOM is cleared.
2) Another feature is selected on the map.. thus another ajax call which runs the handleJson function and another table is created with the #featureTable id.
*****It should be noted... as long as I do not press the clear button, new tables are created fine. They just replace the old tables. BUT, as soon as I call the remove() function, no more table will be created.
I'm sorry, I've become a bit lost now. calling remove() removes the element, do not remove the table element if you want to do $('#featureTable') again, with no results to the jQuery selector there are no elements to call .DataTable() on and no datatables will be created. This is a jQuery thing, not datatables.
If you must remove() the element with that id make sure you create a table with that id in the DOM before trying to call $('#featureTable').DataTables() so there is an element to create a datatable on. This is true whether you call
.remove()
ortable.destroy(true)
(passing true calls remove() without you having to do it yourself)The problem you're encountering does not appear to be with datatables but with DOM management and jquery selectors.
Also, if your table definition is exactly same each time it is used (except with different data) there's always
clear
androw.add
to change the content. Then you can always use jquery show/hide functions on your container to change the visibility of the table.Thanks for your help, i really do appreciate it. I'm a novice at JavaScript so this is sort of a trial by fire for me. Here is all my code pertaining to how I'm using these tables and their creation.
This all works fine as long as I don't selected a different Layer. If I make a selection in the "Cities" layer and then another in the "Cities" layer, it just creates a new table. BUT, if I change layers to "States" which has a different number of columns with different data in it it won't work. I need to completely remove the first "Cities" table and all associated information. That's why i was clearing and removing everything. However, when I do this and try to make a new selection I get this error:
Is there any way around this? So far, removing/destroying "featureTable" and then recreated it in the "handleJson" fucntion everytime has not worked.
I don't see where .remove() is being called in the code above.
(you may need to remove the table def from your html so your tableContainer is an empty node):
So, try changing the lines below
to this:
Thanks.
Sorry, I left out my remove code. I have already tried a combination of the following in~~~~ my clear all button:
So I put that code where you suggested (where the table is first created) and it throws this huge error:
I think it was confused trying to delete a table that did not exist when you attempt to build your first table. I then placed this code:
in in my "map.on('draw:deleted...) code which is basically my "clear all" button. I then get this after I clear and try to create a new table:
It acts as if things are not actually being cleared.
Oops.
Should have been:
Hmmm.. whacky. It just does not create a new table now when i place it in the "clear all". No warnings or anything thing logging to the console. Only when I try to place it on the sidebar do I get a warning:
I've got it right now so that as soon as a table is created the text is just added right on top of the for testing purposes. First table works fine... then I clear.. and I make selections again. No errors, but a new table does not show up. I think that sidebar error "...is not of type Node.." says whatever is actually being created again (if anything) is not a node.
I could try to put that code right in where the table is actually created, but I would need a way to check if a table already exists and use some logic to it how to proceed.
Does your table definition, columns section have the title option set for each column? Datatables has to have that if you are not added <thead><tr> with a <th> for each of your columns.
If you are just clearing out the rows but not the header, do your remove on the tbody instead of the table and leaving your headers intact.
The table are created dynamically each time based on what layers are active. There is the code that determines the column names (the title: i ) part:
I believe the destroy and remove commands will clear out everything from the DOM.
Here is something I created a while ago that uses a select box determine which table to display. It might be helpful
http://jsbin.com/gumijev/176/edit?html,js,output
Oh my god.... all I had to do was add this:
I have been staring at this for since yesterday! Everyone on this forum is so great. I would be totally SOL without your help.
I still get this error about appending the new table to Leaflet Sidebar however. That's another hurdle. There always seems to be one after the next in development....