Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add button to datatable row dynamically

I have a problem when adding a custom bit of HTML to a datatable cell on loading.

on clicking the tab to open the page, I call on a ajax function to fetch the data.

// CLICK on the tab to open the User Manager -> Load all records into DataTable
    $("#admin_details").click(function(){
        // Send through the function to perform in the $_GET variable
        $.ajax({
            url: './inc/AdminScripts.php?argument=loadAllRecords'
        })
            .done(function(html) {
                var t = $('#users_table').DataTable();
                t.clear();
                var obj = eval(html);

                // ADD acquired data items to the DataTable
                $.each(obj, function(key,value) {
                    t.row.add( [
                        value.edit,
                        value.name,
                        value.surname,
                        value.username,
                        value.email,
                        value.language,
                        value.securityQuestion,
                        value.securityAnswer,
                        value.administrator,
                        value.status,
                        value.id
                    ] ).draw();
                });
                addBellsWhistles();
            })
    });

This bit works fine and I get all the data I require. The problem now is that for the EDIT column I want to add a ICON BUTTON to click on to edit the record. I do so using a function addBellsWhistles like this

    function addBellsWhistles(){
        $('#users_table tr').children("td:nth-child(1)").append("<div class='col1d'><button class='editBut'><img src='img/property32.png'></button></div>");
    }

okay, this also work almost perfectly except for one thing. The data table only shows 10 records per page, so when I go to the 2nd or nth page the piece of custom added HTML is not visable.

I have tried the following on click of the paginate button (well quite a few things including the the class of the button, id of the button, the a tag of the button) but nothing works.

Im also using deligation in this case because of dynamically created data

e.g.

    $( ".dataTables_paginate a" ).on( 'click', '.paginate_button', function () {
        console.log('paging');
        $(".confirmUploadError .confirm_text").html("Loading records. Please wait.");
        $(".confirmUploadError").fadeIn(250).delay(300).fadeOut(650);
        addBellsWhistles();
    });

its not writing to console, its not calling the message pop-up and it is especially not calling my function (and I just named them separately for dramatic effect).

Anyone out there have a idea for me please?

PS here is the datatable paginate container as created by datatables

       <div class="dataTables_paginate paging_simple_numbers" id="users_table_paginate">
           <a class="paginate_button previous disabled" aria-controls="users_table" data-dt-idx="0" tabindex="0" id="users_table_previous">Previous</a>
               <span>
                   <a class="paginate_button current" aria-controls="users_table" data-dt-idx="1" tabindex="0">1</a>
                   <a class="paginate_button " aria-controls="users_table" data-dt-idx="2" tabindex="0">2</a>
               </span>
           <a class="paginate_button next" aria-controls="users_table" data-dt-idx="3" tabindex="0" id="users_table_next">Next</a>
       </div>
like image 450
morne Avatar asked Jul 29 '14 14:07

morne


1 Answers

Call the fnCreatedRow function, which fires after every row is created (callback). Call your method from there to do what is needed. In this case:

   $('#users_table').dataTable( {
       "fnCreatedRow": function( nRow, aData, iDataIndex ) {
           $('td:eq(0)', nRow).append("<div class='col1d'><button class='editBut'><img >src='img/property32.png'></button></div>");
       },
   });
like image 142
morne Avatar answered Oct 12 '22 03:10

morne