get data for selected row for TableTool button

get data for selected row for TableTool button

mmontoyammontoya Posts: 84Questions: 27Answers: 4

I have some data coming from the Editor that are not displayed in the dataTable. I have a tableTool button that needs to get two of those data elements for the selected row (I only have single-select enabled).

Currently I have two hidden input boxes and am storing the values there. It works for the most part but if the user unselects the row without selecting a new one, the previously selected values still stay in the input boxes. I'm thinking there has to be a better way that will get the selected row data in the button click function.

          $('#referralsTable tbody').on( 'click', 'td', function () {
              var tr = $(this).closest('tr');
              var row = referralsTable.row( tr );
              //alert(row.data().Referrals.ReferralID);
                  $("#SelectedReferralID").val(row.data().Referrals.ReferralID);
                  $("#SelectedCustomerID").val(row.data().Referrals.CustomerID);
          } );
var referralsTableTools = new $.fn.dataTable.TableTools( referralsTable, {
                    sRowSelect: "os",
                    aButtons: [
                        { sExtends: "editor_create", sButtonText: "New Referral", editor: referralsEditor },
                        { sExtends: "editor_remove", editor: referralsEditor },
                        {
                            sExtends: 'select_single',
                            sButtonClass: 'marginLeft',
                            sButtonText: 'Make Appointment',
                            fnClick: function () {
                              if ( $("#SelectedReferralID").val() !='') {
                                //if selected referral is already a customer, then just open customerDetail
                                //otherwise, insert Customer record, then open customerDetail
                                if ( $("#SelectedCustomerID").val() != 0) {
                                    window.location.href = ('customerDetail.php?id='+ $("#SelectedCustomerID").val());
                                } else {
                                        $.ajax({
                                        type: "POST",
                                        url: "lib/makeCustomer.php",
                                        data: "id="+$("#SelectedReferralID").val(),
                                        cache: false,
                                        success: function(result){
                                            window.location.href = ('customerDetail.php?id='+ result);
                                        }
                                        });
                                }                                

                              } else {
                                alert('You must select someone first');
                              }
                            }
                        }                        
                    ]
                } );
          $( referralsTableTools.fnContainer() ).insertBefore( '#referralsTable_filter' );     

This question has an accepted answers - jump to answer

Answers

  • mmontoyammontoya Posts: 84Questions: 27Answers: 4

    Anyone?? :)

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin

    In the TableTools fnClick you should be able to use the fnGetSelectedData API method. Simply this.fnGetSelectedData() will give you the data for the selected rows in an array.

    Allan

  • mmontoyammontoya Posts: 84Questions: 27Answers: 4
    edited August 2015

    I try this and get an error: Uncaught TypeError: Cannot read property 'CustomerID' of undefined

    this code works:

    $('#table tbody').on( 'click', 'td', function () {
            var tr = $(this).closest('tr');
            var row = table.row( tr );
     
                //tr.addClass( 'selected' );
                $("#selectedID").val(row.data().Customers.CustomerID);
      } );
    

    but not this:

    ( table, {
                        sRowSelect: "os",
                        aButtons: [
                            //{ sExtends: "editor_create", sButtonText: "New Customer" },
                            {
                                sExtends: 'select_single',
                                sButtonClass: 'marginLeft',
                                sButtonText: 'View Customer Info',
                                fnClick: function () {
    
                                  var oTT = TableTools.fnGetInstance( 'table' );
                                  var aData = oTT.fnGetSelectedData();
                                  alert(aData.Customers.CustomerID);
                                  window.location.href = ('customerDetail.php?id='+ aData.Customers.CustomerID);
    
    
  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    Answer ✓

    oTT.fnGetSelectedData();

    This returns an array. So you would need to use aData[0]... to get the data for the first row.

    Allan

  • mmontoyammontoya Posts: 84Questions: 27Answers: 4

    worked perfectly..

    Thank you for your assistance.

This discussion has been closed.