Multiple tables, unknown amount of tables, need specific table settings

Multiple tables, unknown amount of tables, need specific table settings

daan.timmerdaan.timmer Posts: 4Questions: 0Answers: 0
edited February 2010 in General
Hello DataTables developers and contributors and other peoples on here that help users.

I've found out about DataTables since yesterday after searching for an easy to use, full-featured table sorting/filtering/paging plugin with jQuery. I had found different of such plugins but none were either complete or didnt function well.

However, I sadly ran into a small, yet annoying problem that I don't really know how to circumvent (yet). (Note I am no hard-core jQuery expert either so I might be doing things in an awkward manner).

I am using a MVC framework with, of course, different views. I am using a general class based init to initialise tables for use with DataTables.
[code]$(document).ready(function(){
oDtbls = $("table.display").dataTable({
sPaginationType: "full_numbers",
iDisplayLength: 25});
};[/code]

Just so that I don't have to worry about javascript in any of my views.
However, at some pages I'll be using multiple tables. And some of them require specific settings.
For those I do have to use javascript in the view itself. So I thought, i'll just give the table an ID and use the jQuery-likeish method for setting attributes.
Ex:
[code]$("#selector").dataTables('option', 'iDisplayLength', 10);[/code]
However, that complains that I can't init dataTables more then once on a specific table.

So I tried an other aproach
[code]$("table.display").each(function(index){
$(this).attr(
"dtable",
o = $(this).dataTable({
sPaginationType: "full_numbers",
iDisplayLength: 25}));
});[/code]
(the o is there for debug purpose)
view specific:
[code]$("#selector").attr("dtable").fnSetColumnVis(2,false);[/code]
However, the object returned by attr("dtable") is an object but it doesn't appear to be *the* dataTable object thing. as would be expected.
So this doesn't work either.

I've searched these forums and found out that you have added an index-selector property somewhere in 2009. However I can't really use that approach either because I don't know (on the view itself) which index belongs to that table. More specificaly because one view can be (at moment it is not) assigned more then once to a template. so I can't hard-code an index value in the view.

I hope I am clear on what I would like to achief and I certainly hope I am trying in going in the right direction. As I said I am rather new to jQuery (all but new to javascript though).

Thanks in advance,

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    It's probably worth going through some of the examples for API interaction: http://datatables.net/examples/api/add_row.html . DataTables does not keep a reference to the object that was created during it's initialisation - so you need to do it (as shown in the demos). This is something I plan to address in the near future, but haven't had time recently.

    So you need to:

    [code]
    o = $().dataTable(...)
    o.fnSetColumnVis(...)
    [/code]
    Also note that DataTables uses only one parameter passed to it, so the syntax used here "dataTables('option', 'iDisplayLength', 10);" won't work at all.

    Allan
  • daan.timmerdaan.timmer Posts: 4Questions: 0Answers: 0
    Yeah, I've read through those tutorials/examples. I've got all those kind of things working, no problem.

    It is just that specific case of having one *normal* table and multiple non-normal tables.

    I've found a simple, yet, more-code-costing solution to the problem. (Which involves just copy-pasting the dataTables init phase for each *different* table. Which works.

    I just hoped there was a more simpler solution to the problem :-).

    I'll post some screenshots of what I am making, as soon as I got something that is nice and enough to be shown in a gallery. :-)

    Thanks for the input :-)
This discussion has been closed.