Adding extra line to table for Adsense code

Adding extra line to table for Adsense code

markjomarkjo Posts: 66Questions: 0Answers: 0
edited August 2012 in DataTables 1.9
I need to put Google Adsense code inside my table's center. (for example if table has 10 rows, I need to put after 5th row)
Because of Google TOS I will put code after document ready and won't update adsense code.

Regarding to this: http://www.datatables.net/forums/discussion/433/ajax-reload/p1
Best option seems like adding to "fnDrawCallback" function.

My tables has 10 columns. Regarding to previous topic I tried to put google code after 3rd row.
[code]"fnDrawCallback": function () {
var gnTr = document.createElement( 'tr' );
var nCell = document.createElement( 'td' );
nCell.colSpan = 0;
nCell.innerHTML = "google code";
gnTr.appendChild( nCell );

var nTrs = $('#mytable tbody tr');
nTrs[3].parentNode.insertBefore( gnTr, nTrs[3] );
}[/code]

But I got this error:
"Datatables warning (table id = 'mytable') : Requested unknown parameter '1' from the data source for row 3.

And my table didn't paginated.

1) How can I fix this?
2) Is there a better way for this situation since August 2009's discussion.

Thank you in advance.

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    fnDrawCallback is still the way to dynamically insert rows, but it shouldn't be causing the error you are getting. If you disable your function, does it then work? Are you inserting a row before initialising the DataTable?

    A link would be very useful.

    Allan
  • markjomarkjo Posts: 66Questions: 0Answers: 0
    If I disable adding a row to table, everything works fine.

    I'm trying to add row to my table while initializing it, it is in table's constructor.

    On document load I create table and I put "fnDrawCallback":updateTableFuncts(1), inside my table constructor. "updateTableFuncts" includes table related function and "addNewLine" function as well.
    I need to call "addNewLine" function every time table is redrawed.

    Although "unknown parameter" dialog, my code is inserted after the 3rd row successfully.
    But I couldn't understand what did I di wrong for error dialog.

    I put my table into debug page:
    http://debug.datatables.net/ikagew

    Thank you for interest
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Unfortunately due to the error you are getting, the debugger isn't much help. I would actually need to see the page to be able to offer much help I think.

    Allan
  • markjomarkjo Posts: 66Questions: 0Answers: 0
    I created this test page:
    http://tiny.cc/wg5giw
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    [code]
    "fnDrawCallback": updateTableFuncts(1),
    [/code]

    You are assigning the result of running that function to your fnDrawCallback, and you are not returning a function. So updateTableFuncts is running before you initialise the table, inserting a row, and thus DataTables gives you the error.

    Just add a small anon function:

    [code]
    "fnDrawCallback": function () { updateTableFuncts(1) },
    [/code]

    Allan
  • markjomarkjo Posts: 66Questions: 0Answers: 0
    That worked great.
    Thank you Allan for your help and quick response about my problem! :)
  • markjomarkjo Posts: 66Questions: 0Answers: 0
    Hello Allan,
    I use the solution above. But it seems like I need to put adsense code inside table while constructing the table.
    If I add Adsense code after [code]$(document).ready()[/code], ads doesn't appear right because DOM is already loaded. I need to put that javascript snippet while creating the datatable.
    Is there such an approach?
    Thank you very much
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Can you not just put the code in before you initialise the DataTable?

    Allan
  • markjomarkjo Posts: 66Questions: 0Answers: 0
    edited August 2012
    For example inside "tbody" of the table I put "adsense code" inside 5th row and I didn't put any more data to this row. I also wrote colspan="100" to cover all columns.

    [code]echo '';
    .....
    if ($rowNo == 5) {
    echo "";
    echo 'google_ad_client = "ca-pub-12345"; /* my advertisement */ google_ad_slot = "12345"; google_ad_width = 728; google_ad_height = 90; ';
    echo "";
    }
    ....
    ...
    echo '';[/code]

    But when I run datatable code for the table I got this error:
    [quote]"DataTables warning (table id='mytable'): Requested unknown parameter '0' from the data source for row 5[/quote]
  • markjomarkjo Posts: 66Questions: 0Answers: 0
    I suppose it is because of I use colspan and make that row one long column.
    Is it so?
  • markjomarkjo Posts: 66Questions: 0Answers: 0
    Anybody has comments?
    Thank you
  • markjomarkjo Posts: 66Questions: 0Answers: 0
    When I remove colspan and change HTML code like below, datatables works fine.
    But other rows' columns are narrow, but Adsense image is far more wider. (For example column size changes from 50px to 728px)
    So table becomes unreadable.

    It seems like when I use colspan I can't use datatables, when I create each columns for Adsense code columns width corrupts :))

    echo '';
    .....
    if ($rowNo == 5) {
    echo "";
    echo "";
    echo "";
    echo 'google_ad_client = "ca-pub-12345"; /* my advertisement */ google_ad_slot = "12345"; google_ad_width = 728; google_ad_height = 90; ';
    echo "";
    echo "";
    echo "";
    echo "";
    echo "";
    echo "";
    echo "";
    }
    ....
    ...
    echo '';
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    > I suppose it is because of I use colspan and make that row one long column.

    No absolutely not. DataTables does not support rowspan or colspan in the TBODY. You can inject elements into the table using the DOM after the data has been gathered by DataTables, which is the method proposed above with fnDrawCallback, but you absolutely cannot have rowspan or colspan in the TBODY when the table is initialised.

    Allan
  • markjomarkjo Posts: 66Questions: 0Answers: 0
    Adding data after loading DOM is not suitable for me. I need something that works while loading document. Another constraint is datatables doesn't support colspan.

    I think I need such a solution.
    Let the size of tbody as 20 rows.
    I need to put this code after 10th row while constructing tbody. And this will create 11th row.
    Is it possible to datatables to disregard specific rows.

    For example in this situation datatables will sort all rows except taking 11th row into consideration.
    Is it possible to implement such solution with a plugin?
    Thank you
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Currently no - as I say, DataTables does not support rowspan or colspan. The closest you would get at the moment is to have a script before you initialise DataTables which will remove the 'extra' row, and then use fnDrawCallback to add it back into the table. That would work okay, but it is a bit of a kludge.

    Allan
This discussion has been closed.