How to send email with row data

How to send email with row data

kalvinmandkalvinmand Posts: 2Questions: 1Answers: 0
edited August 2023 in Free community support

Link to test case:
Debugger code (debug.datatables.net):

$('#emailLink').on('click', function (event) {
        event.preventDefault();
      var email = 'test@gmail.com';
      var subject = 'test';
      var tableHeader = ['Clip Name', 'Game ID', 'Play ID', 'Creation Time','Pre Signed URL'];
      var emailBody = new Array();
      $('#dataTable tbody').on('click', 'tr', function(){
        var rowData = (table.row(this).data());
        $.each(rowData, function(key, value) {
          /*alert(tableHeader[key] + ':' + value);*/
          emailBody.push(tableHeader[key] + ':' + value);
        });
        cosole.log(emailBody);/*returns an array*/
      });
      alert(emailBody);/*returns an empty array*/
      window.location = 'mailto:' + email + '?subject=' + subject + '&body=' + emailBody;
    });

Error messages shown: Empty array variable
Description of problem: Getting an empty array when passing to email body

Edited by Kevin: Syntax highlighting. Details on how to highlight code using markdown can be found in this guide

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,336Questions: 26Answers: 4,951
    Answer ✓

    Line 13 should give an error as cosole is not valid.

    Line 15 shows an empty array because the code in the click event, lines 7-14 wont; execute until you click a row. This click event won't be created until you click the #emailLink button.

    The Select extension would work well for this. With it you can select the rows to email and when you click the #emailLink button it can email to the selected rows. See this example to learn how to get the select rows. Use rows().every() with the { selected: true } selector-modifier to loop through all the selected rows. This will replace lines 7-14.

    Kevin

  • kalvinmandkalvinmand Posts: 2Questions: 1Answers: 0
    edited August 2023

    Thanks for the hint. Below is the working code

    $('#example tbody').on('click', 'tr', function() {
            if ($(this).hasClass('selected')) {
                $(this).removeClass('selected');
            } else {
                table.$('tr.selected').removeClass('selected');
                $(this).addClass('selected');
            }
        });
    

    And you can pass the selected row to a button or anything else using below

    var selectedRow = table.row('.selected').data();
    
Sign In or Register to comment.