OnClick - Send to another page - Help

OnClick - Send to another page - Help

timcadieuxtimcadieux Posts: 76Questions: 22Answers: 0
edited June 2009 in General
Hi Folkz, I'm using this code which I found elsewhere in the Forums to allow myself to Click and open another page with the ID passed. It worked for me yesterday when I was using Server Side, but today, whenever I uncomment the commented lines below, it stops working? I can't figure it out?

Also, would it be possible to force it to open a target window, not the current?

Thx

[code]
var oTable;

$(document).ready(function() {
oTable = $('#example').dataTable( {
"bSortClasses": false,
"sDom": '<"top"i>rtlp<"clear">',
"oLanguage": {
"sSearch": "Search all columns:"
},
"sPaginationType": "full_numbers",
"aoColumns": [
/* ID */ { "bSearchable": false, "bVisible": false },
/* Surname */ null,
/* Given Name/Initial */ null,
/* Date of death */ null,
/* Rank */ null,
/* Service */ null,
/* Unit */ null,
/* Commemorated in
Country */ null,
/* Commemorated in
Cemetery/Memorial */ null
] ,
"bProcessing": true,
"sAjaxSource": 'json/' + sAlpha + '.txt',
"fnDrawCallback": function( oSettings ){
$('#example tbody tr').each( function() {
/* var iPos = oTable.fnGetPosition( this );
$(this ).click(function(){
window.location = "whatever.php?id="+oSettings.aoData[iPos]._aData[0];
});*/
});
}

} );[/code]

Replies

  • allanallan Posts: 63,489Questions: 1Answers: 10,470 Site admin
    Hi,

    Try this for your callback function:

    [code]
    "fnDrawCallback": function( oSettings ) {
    $('#example tbody tr').click( function() {
    var iPos = oTable.fnGetPosition( this );
    alert( oSettings.aoData[iPos]._aData[0] );
    //window.location = "whatever.php?id="+oSettings.aoData[iPos]._aData[0];
    } );
    }
    [/code]
    That seems to do the trick for me.

    Allan
  • timcadieuxtimcadieux Posts: 76Questions: 22Answers: 0
    Argh, I was sooo close!

    Thx

    Anyway I can make the url target a new page?
  • allanallan Posts: 63,489Questions: 1Answers: 10,470 Site admin
    You need to use window.open() - http://www.w3schools.com/HTMLDOM/met_win_open.asp

    Allan
  • timcadieuxtimcadieux Posts: 76Questions: 22Answers: 0
    Thanx, that all worked. Only problem, I found a bug in the Function listed above.

    I'll explain.

    Using JSON, if i click on any row, a new page opens, that's fine.

    If I type something in a filter and say get 5 rows, when i click on the first row, I get 5 windows that open, each with the ID of the first row.

    Any way to avoid this?
  • brianmmbrianmm Posts: 41Questions: 0Answers: 0
    fyi...

    Thanks for this one ... I have just chucked it into mine, I'm using Server-side pipelining, and upon filtering - and using window.open() - it only opens 1.
  • allanallan Posts: 63,489Questions: 1Answers: 10,470 Site admin
    Hi,

    We've talked about this over e-mail, but here is a quick summary of the particular issue here, for the benefit of others.

    That problem is that on each draw (ie. each latter press) fnDrawCallback is being run, and therefore adding another event handler to your TRs (Visual Event will show this). So what you want to do is use the fnInitComplete callback such that events are only added once. You'll also need to use fnGetNodes() in your event handler - see the post init demo here:

    Regards,
    Allan
This discussion has been closed.