How can the contents in input fields be copied when using Buttons?

How can the contents in input fields be copied when using Buttons?

cms1cms1 Posts: 11Questions: 4Answers: 0

Hello,

Please see example below:

http://live.datatables.net/vigokehu/1/edit

I found this thread from 2016 but the solution no longer seems to work.

This question has accepted answers - jump to:

Answers

  • kthorngrenkthorngren Posts: 20,141Questions: 26Answers: 4,736
    Answer ✓

    Using the suggestion Allan provided in the thread you linked I came up with this example that uses the node parameter:
    http://live.datatables.net/vigokehu/3/edit

    Kevin

  • cms1cms1 Posts: 11Questions: 4Answers: 0

    Thank you sir!

  • cms1cms1 Posts: 11Questions: 4Answers: 0
    edited November 2022

    Hello,

    Demo: http://live.datatables.net/mepaheko/1/edit

    I'm trying to use the same solution above with an excelHtml5 Button which creates an excel spreadsheet from multiple tables.

    Problem: I'm getting XML parsing errors when <input /> fields are on more than 1 table. (It seems to work with input fields only on the first table)

    How to replicate:
    1) Click the 'Excel' button above the first table

    Question: How do I have the custom function for table 1's button also find the input.val for the other tables with input fields?

    Any help or direction is appreciated

  • kthorngrenkthorngren Posts: 20,141Questions: 26Answers: 4,736
    Answer ✓

    I updated this loop in the getTableData( function to check for input elements.

        // Loop through each row to append to sheet.    
        table.rows().every( function ( rowIdx, tableLoop, rowLoop ) {
          var data = this.data();
          
          // See if there is an input to get the value
          $(this.node()).find('td').each(function( index ) {
            var input = $( this ).find('input');
            if (input !== undefined) {
              // If input is defined get the value and replace the corresponding data array element
              data[index] = $(input).val();
            }
          });
    

    If there is an input then get the value and replace the corresponding data array element with the value.

    http://live.datatables.net/sozuxona/1/edit

    Kevin

  • cms1cms1 Posts: 11Questions: 4Answers: 0

    Thank you once again Kevin

  • cms1cms1 Posts: 11Questions: 4Answers: 0

    First tab works -- why are excel tabs 2+ returning undefined?

  • kthorngrenkthorngren Posts: 20,141Questions: 26Answers: 4,736
    Answer ✓

    The if statement is likely wrong:

    if (input !== undefined)
    

    I don't have time now but you can use the browser's debugger to see what the value of this statement is when there is no input:

    var input = $( this ).find('input');
    

    You may need to check for length > 0 or something like that.

    Kevin

  • cms1cms1 Posts: 11Questions: 4Answers: 0

    Success!

    if (input.length > 0 && input !== undefined)

Sign In or Register to comment.