How do I save all values of my table to a file

How do I save all values of my table to a file

This question has an accepted answers - jump to answer

Answers

  • drfunkdrfunk Posts: 58Questions: 8Answers: 0

    that is good however I need to save the data to a file on my Linux server

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Couldn't the script that sends the data to DataTables also be used to make a copy on the server when prompted? It's not something DataTables provides out of the box, I'm afraid.

    Colin

  • drfunkdrfunk Posts: 58Questions: 8Answers: 0

    it does that however if someone deletes a row or adds a row I need to have that saved as well

  • kthorngrenkthorngren Posts: 21,336Questions: 26Answers: 4,951
    edited February 2022

    You can use the Editor to update the server via ajax. Or you will need to create your own code to send any updates to the server. The specifics of how to do this are dependent upon how you need to handle the updates.

    Lets take adding a row. The Editor process is to use a form input, submit the data to the server via ajax and the server's response will contain the row data as it was added. The Editor will then use something like row.add() to add the returned row data to the Datatable. You can do something similar with an ajax request then use the success function to row.add() with the returned row data.

    Kevin

  • drfunkdrfunk Posts: 58Questions: 8Answers: 0

    Hi Kevin,

    yea I basically tried the ajax route here is my code:

    $(document).ready(function() {
    $('#saveTab').click(function() {
    var tbl = document.getElementById("ncs");
    var i = 1;
    var txt = '';
    $('#ncs tr').each(function(){
    var val1 = $(this).find('div.mod').html();
    var val2 = $(this).find('div.port').html();
    if (val1 && val2) {
    txt += val1 + "|" + val2 + "<br>";
    //alert(val1+' '+val2);
    }
    });
    alert(txt);
    $.ajax({url: "updateDB.cgi?Data=" + txt, success: function(result){
    alert(result);
    }});
    location.reload(true);
    } );
    } );

    which calls another cgi script that writes the data. this works, however it only captures the first page of data.
    Example:

    let's say I have 300 rows of data and I have show rows on 100 then I get 100 rows of data. if it is set to 20 then I get only 20 rows of data

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

    jQuery and Javascript can only find the elements shown on the page. Datatables removes the rows not displayed form the DOM. You can use rows().data() to get all the table data. You may need to chain toArray() to make the result a plain Javascript array to send the data via ajax.

    Kevin

  • drfunkdrfunk Posts: 58Questions: 8Answers: 0

    gave it a shot. but not working

    http://live.datatables.net/niradika/4/edit

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

    You are getting this error:

    Uncaught ReferenceError: table is not defined

    You haven't defined the table variable.

    Since you have multiple tables you will need to indicate which of the tables to get the data from using the table() API. Like this:
    http://live.datatables.net/niradika/5/edit

    Note that I moved your code inside document.ready(). I mentioned this before and fixed one of your other examples - you need to make sure to have unique ids for the tables. I fixed it again in this example.

    Kevin

  • drfunkdrfunk Posts: 58Questions: 8Answers: 0

    I'm sorry Kevin I posted the wrong html example

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

    here is what I tried to gat all the values from the 2 columns

  • kthorngrenkthorngren Posts: 21,336Questions: 26Answers: 4,951
    edited February 2022

    What are the steps to see the problem you are having? What are you expecting to happen instead?

    Kevin

  • drfunkdrfunk Posts: 58Questions: 8Answers: 0

    there is a save button at the bottom of the page. which when clicked I would like to get all the table values into var txt that I will send to a cgi script that will be entered into an oracle DB. Right now just saving to a flat file. I have the updateDB.cgi script written and it was receiving data but just the data visible in the DOM.

    Here is code snippet:

    backup the modlist file

    cp $rptdir/mod.list $rptdir/backup/mod.list.$filedate1 2>&1;

    Get the input

    $buffer = $ENV{'QUERY_STRING'};
    if ($buffer) {
    &mesg("Buffer: [$buffer]\n",3);
    @pairs = split(/&/, $buffer);
    }

    foreach my $pair (@pairs) {
    ($name, $value) = split(/=/, $pair);

    # Un-Webify plus signs and %-encoding
    $value =~ tr/+/ /;
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
    
    $name =~ tr/+/ /;
    $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
    &mesg("Name: [$name]   Value: [$value]\n",3);
    $FORM{$name} = $value;
    

    }
    &mesg("Value: [$value]\n",3);

    C3850-NM-2-10G|4<br>C3KX-NM-1G|4<br>C6840-X-LE-40G|42<br>

    (@DATA) = split(/<br>/,$value);
    foreach $line (@DATA) {
    &mesg("Line: [$line]\n",3);
    push @NF, "$line\n";
    }

    &write_file("/var/www/cgi-bin/billing/MOD","mod.list");

    print "Content-type: text/html\n\n";
    print "Success\n";
    print "Data Saved";

    &Connect_DB($dbiname,$dbuser,$dbpass);

    &update_DB($dbname,$sql_query);

    &write_log if $logging;

    &Disconnect_DB();

    $tdate=&get_time();
    $stop = time();
    my $runtime = (($stop - $start) / 60);
    &mesg("########## Finished $tdate ########## [$rc]\n",2);
    &mesg("########## Runtime: $runtime Minutes ##########\n",2);
    &write_log if $logging;
    exit;

    here was the code that worked for the visible page only:

    $(document).ready(function() {
    $('#saveTabsav').click(function() {
    var tbl = document.getElementById("modport");
    var i = 1;
    var txt = '';
    $('#modport tr').each(function(){
    var val1 = $(this).find('div.mod').html();
    var val2 = $(this).find('div.port').html();
    if (val1 && val2) {
    txt += val1 + "|" + val2 + "<br>";
    //alert(val1+' '+val2);
    }
    });
    alert(txt);
    $.ajax({url: "updateDB.cgi?Data=" + txt, success: function(result){
    alert(result);
    }});
    location.reload(true);
    } );
    } );

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

    You have:

    $("#saveTab").on("submit", function (event) {
    

    submit won't work for this case since the button is not part of a form. Use click instead. Use the same rows().data code to get the data in the click event. Like this:
    http://live.datatables.net/cenipami/2/edit

    Kevin

  • drfunkdrfunk Posts: 58Questions: 8Answers: 0

    Hi Kevin,

    Thanks that fixed it

Sign In or Register to comment.