5

I am developing a table that will contain all our users which can also be changed by clicking the tablerow and editing the data in a form that will open once the click was performed.

If i have all the users loaded at page load, my code works fine.

Once i change my datatable to load the users at datatable initialisation it will only work on the first page.

If i uncomment the lower part of my ready(function()) and delete fnInitComplete it wont even work on the first page.

Here is the relevant part of my code:

        $(document).ready(function(){
            tbl = $('#nutzer').dataTable( {
                "bJQueryUI": true,
                "sScrollX": "100%",
                "bProcessing": true,
                "bServerSide": true,
                "iDisplayLength": 10,
                "sAjaxSource": "xhr.php",
                "sPaginationType": "full_numbers",
                "fnInitComplete": function() {
                    $('#nutzer tbody tr').on("click", function () {
                        aufklappen(this);
                    } );
                }
            } );

            $( "#create-user" ).button().click(function() {
                $( "#dialog-form" ).dialog( "open" );
            });
//            $('#nutzer tbody tr').on("click", function () {
//                aufklappen(this);
//            } );
        });

        function aufklappen(row) {
            if ( tbl.fnIsOpen(row) ) {
                tbl.fnClose(row);
            } else {
                set = tbl.fnSettings().aoOpenRows[0];
                (set != null) ? (tbl.fnClose(set.nParent)) : null;
                $.post("benutzerBearbeiten.php", { 
                    funktion : "benutzerDaten",
                    id : $(row).children( "td:first-child" ).text()
                }, function(data){
                    tbl.fnOpen( row, data);
                    $( "#deaktivieren").button().click(function(e){
                        e.preventDefault();
                        deaktivieren();
                    });
                    $( "#speichern").button().click(function(e){
                        e.preventDefault();
                        speichern();
                    });
                }
            ) };
        }

After page load or page change through the datatables pagination i can manualy call

$('#nutzer tbody tr').on('click', function () {
    aufklappen(this);
} );

and the click function gets bound to the tr's perfectly.

Seems to me that the elements created by datatables-plugin are not getting up the dom to the on() handler that i defined but i cant figure out why.


EDIT

Utilising "The System Restart"s answer i ended up deleting the fnInitComplete part and add

"asStripeClasses": [ "odd nutzer_tr", "even nutzer_tr"]

to the initialisation part and

$("body").delegate(".nutzer_tr", "click", function () {
    aufklappen(this);
});

to the ready(function()). The additional class nutzer_tr is to prevent the opened tablerow from closing.

1 Answer 1

23

I think you need live event:

$('body').on('click', '#nutzer tbody tr', function () {
    aufklappen(this);
});

or can use delegate()

$('body').delegate('#nutzer tbody tr', 'click', function () {
    aufklappen(this);
});
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much for this. It solved my problem and stopped me from pulling my hair out.
Dont know why, can not see why, but this worked over $('#nutzer tbody tr').click(function(){});
Thanks, this is a general issue which most of unorganized JS educated developers have to face in, now I can resolve this issue in anywhere else where it occurs!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.