3.2.0

Save the grid data at one time

How could I click on a "Save" button, and save all the grid data at one time?

Please advise...

Kevin
June 29,
please an example...
or the complete data cannot be recovered?



I am in phase of evaluation to buy this product.
Andres M. (chile)
February 7,
Im sure u will buy AW, is the best visual components for web :-)

Well, do u wanna save the grid data in server? Clicking in save button?!

U can create a XML Document and populate using grid data or something (dont do that) like this:

obj = new AW.UI.Grid;
// blah blah blah

// save grid procedure
var data = ""; // we need stringbuffer, lol ;-)
for(var i=0;i<obj.getRowCount();i++) {
  for(var j=0;j<obj.getColumnCount();j++) {
        if(j > 0) data += "&";
        data += "line"+i+"-column"+j+"="+obj.getCellValue(j, i);
   }
}

// create httpRequest
// send data to server.


Maybe it can help u.
Cya.
os: BUY AW, help Alex :)
Pc (from Brazil)
February 7,
Why buy a geeky product?

I have spent ages just trying to find someway of writing the modified data back to a database.

I can get data and display it in a sortable table without using activewidgets to do it !!

We need a PRACTICAL example of how to step through the rows and columns and save the data!!

Mike
February 14,
Lol...
Cant u see that? Its easy man, just iterate over all rows-columns and save the data, why cant u do that?!?

Use the SEARCH button in forum and u will find a lot of stuffs.
Pc (from Brazil)
February 14,
OK, so if its that easy... how about an example?

mike
February 20,
I agree with mike that finding usefull things and putting them together take ages. Here is how I save my grid data to the server in a CSV file:
(text in french, sorry, but easy to understand)

1/ put a save button somewhere in your page (this button call the savegrid() function):

<button id='savebutton' disabled onclick='savegrid();' type='button' title="Sauvegarde les modifications sur le serveur distant">Sauver</button>


2/ adapt this code for your needs: this saves grid data back to server (the savegrid() function calls the processgrid() function which actually scans the grid row by row and column by column to get the data):

function savegrid() {
// prompt user to confirm changes
var name=confirm("Confirmer la sauvegarde des modifications\neffectuées sur ces données ?");
if (name==false)
return
else {

// populate content variable that will be POSTed
var content=processgrid();

var newrequest = new AW.HTTP.Request;

// process_grid.php is the file on the server which will process
// the collected data we are about to send; see code below
newrequest.setURL("process_grid.php");
// data will be sent by POST method
newrequest.setRequestMethod("POST");
// "filename" is the string name of the variable which will store the name of the data file in the process_grid.php file; see code below
// myselectedvalue is a javascript variable which stores the name of the file the data will be saved in on the server
newrequest.setParameter("filename", myselectedvalue);
// "content" is the string name of the variable which will store the data in the process_grid.php file; see code below
// content is a javascript variable that stores the data themselves
newrequest.setParameter("content", content);
newrequest.request();

// doc is a javascript variable that stores the response from the server (sent back by process_grid.php); see code below
// populate doc
var doc="NORESPONSE";
newrequest.response = function(doc) {

// process response from server
if (doc == "SUCCESS") {
document.getElementById("savebutton").disabled=true;
alert("Données de\n"+myselectedvalue + "\nsauvegardées avec succès");
}
// optionnaly put here the other responses from the server (see process_grid.php)
}
}
}

// export current grid data to a javascript variable named content
// note that data will be saved in a CSV file on the server, so
// I save my data as CSV data ie: "data1","data2", etc..
// You will need to change that according to your needs.
function processgrid() {
// applique des index le cas échéant
var testIndices = obj.getRowIndices();
if (testIndices.length == 0) {
var rows = [];
for (i=0; i < obj.getRowCount(); i++) {
rows.push(i)
}
obj.setRowIndices(rows);
}

var columns = obj.getColumnCount();
var rows = obj.getRowCount();
// be sure that content is empty
content='';

for(var row=0; row<rows; row++){
var currrow='';
var currcel='';
for(var col=0; col<columns; col++){

// get the data from CellText not CellData as CellData does not work (yet)
currcel=obj.getCellText(col, row); // pour l'instant, seulement getCellText (tester nouvelle version avec getCellData)

// this code put a ' in empty cell; change that if you want to
if (currcel=="'" || currcel=="")
currcel='""';
else
currcel='"' + currcel + '"';

// build the CSV data format
if (currrow=='')
currrow=currcel;
else
currrow=currrow + ',' + currcel;
}
content = content + currrow + '\n';
}
return content;
}


3/ save the code below in a file named process_grid.php in the same folder

<?php
// ensure that headers are sent
if (!headers_sent()) {
// write headers
// keep the charset at utf-8 even if your others pages are not utf-8
header("Content-Type: text/html; charset=utf-8");
header("Cache-Control: no-store, no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
}

// keep these utf8_decode if your pages are not utf-8
$filename = utf8_decode($_POST['filename']);
$content = utf8_decode(stripslashes($_POST['content']));
$datefilename = utf8_decode($_POST['datefilename']);
$datecontent = utf8_decode(stripslashes($_POST['datecontent']));

// update data file
if (file_exists($filename))
copy($filename, $filename .".bak"); // backup file

if (!is_writable($filename)) {
if (file_exists($filename .".bak")) {
copy($filename .".bak", $filename); // rename backup file
}
echo "NOTWRITABLE";
exit;
}

if (!$fp = @fopen($filename, "w")) {
copy($filename .".bak", $filename);
echo "NOTOPENED";
exit;
}

if (fwrite($fp, $content) === false) {

// ferme le fichier
fclose($fp);
copy($filename .".bak", $filename);
echo "NOTWRITED";
exit;
}

// tout est ok
// ferme le fichier
fclose($fp);

exit;

?>

As you can see, there are other responses from the process_grid.php file so when things screw up you could use them to report to user.
Note that a backup of the data is done and used in these cases, so no data are lost (except your changes of course).

These codes took me much time to gather and understand (I just begin
in web scripting). Now enjoys.
Hope this helps.

Bye,
TMTisFree
TMTisFree
February 20,
I agree with mike that finding usefull things and putting them together take ages. Here is how I save my grid data to the server in a CSV file:
(text in french, sorry, but easy to understand)

1/ put a save button somewhere in your page (this button call the savegrid() function):

<button id='savebutton' disabled onclick='savegrid();' type='button' title="Sauvegarde les modifications sur le serveur distant">Sauver</button>


2/ adapt this code for your needs: this saves grid data back to server (the savegrid() function calls the processgrid() function which actually scans the grid row by row and column by column to get the data):

function savegrid() {
// prompt user to confirm changes
var name=confirm("Confirmer la sauvegarde des modifications\neffectuées sur ces données ?");
if (name==false)
return
else {

// populate content variable that will be POSTed
var content=processgrid();

var newrequest = new AW.HTTP.Request;

// process_grid.php is the file on the server which will process
// the collected data we are about to send; see code below
newrequest.setURL("process_grid.php");
// data will be sent by POST method
newrequest.setRequestMethod("POST");
// "filename" is the string name of the variable which will store the name of the data file in the process_grid.php file; see code below
// myselectedvalue is a javascript variable which stores the name of the file the data will be saved in on the server
newrequest.setParameter("filename", myselectedvalue);
// "content" is the string name of the variable which will store the data in the process_grid.php file; see code below
// content is a javascript variable that stores the data themselves
newrequest.setParameter("content", content);
newrequest.request();

// doc is a javascript variable that stores the response from the server (sent back by process_grid.php); see code below
// populate doc
var doc="NORESPONSE";
newrequest.response = function(doc) {

// process response from server
if (doc == "SUCCESS") {
document.getElementById("savebutton").disabled=true;
alert("Données de\n"+myselectedvalue + "\nsauvegardées avec succès");
}
// optionnaly put here the other responses from the server (see process_grid.php)
}
}
}

// export current grid data to a javascript variable named content
// note that data will be saved in a CSV file on the server, so
// I save my data as CSV data ie: "data1","data2", etc..
// You will need to change that according to your needs.
function processgrid() {
// applique des index le cas échéant
var testIndices = obj.getRowIndices();
if (testIndices.length == 0) {
var rows = [];
for (i=0; i < obj.getRowCount(); i++) {
rows.push(i)
}
obj.setRowIndices(rows);
}

var columns = obj.getColumnCount();
var rows = obj.getRowCount();
// be sure that content is empty
content='';

for(var row=0; row<rows; row++){
var currrow='';
var currcel='';
for(var col=0; col<columns; col++){

// get the data from CellText not CellData as CellData does not work (yet)
currcel=obj.getCellText(col, row); // pour l'instant, seulement getCellText (tester nouvelle version avec getCellData)

// this code put a ' in empty cell; change that if you want to
if (currcel=="'" || currcel=="")
currcel='""';
else
currcel='"' + currcel + '"';

// build the CSV data format
if (currrow=='')
currrow=currcel;
else
currrow=currrow + ',' + currcel;
}
content = content + currrow + '\n';
}
return content;
}


3/ save the code below in a file named process_grid.php in the same folder

<?php
// ensure that headers are sent
if (!headers_sent()) {
// write headers
// keep the charset at utf-8 even if your others pages are not utf-8
header("Content-Type: text/html; charset=utf-8");
header("Cache-Control: no-store, no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
}

// keep these utf8_decode if your pages are not utf-8
$filename = utf8_decode($_POST['filename']);
$content = utf8_decode(stripslashes($_POST['content']));
$datefilename = utf8_decode($_POST['datefilename']);
$datecontent = utf8_decode(stripslashes($_POST['datecontent']));

// update data file
if (file_exists($filename))
copy($filename, $filename .".bak"); // backup file

if (!is_writable($filename)) {
if (file_exists($filename .".bak")) {
copy($filename .".bak", $filename); // rename backup file
}
echo "NOTWRITABLE";
exit;
}

if (!$fp = @fopen($filename, "w")) {
copy($filename .".bak", $filename);
echo "NOTOPENED";
exit;
}

if (fwrite($fp, $content) === false) {

// ferme le fichier
fclose($fp);
copy($filename .".bak", $filename);
echo "NOTWRITED";
exit;
}

// tout est ok
// ferme le fichier
fclose($fp);

exit;

?>

As you can see, there are other responses from the process_grid.php file so when things screw up you could use them to report to user.
Note that a backup of the data is done and used in these cases, so no data are lost (except your changes of course).

These codes took me much time to gather and understand (I just begin
in web scripting). Now enjoys.
Hope this helps.

Bye,
TMTisFree
TMTisFree
February 20,
Sorry for double posts, connexion resets in between.

I forgot to remove these 2 lines in process_grid.php:
$datefilename = utf8_decode($_POST['datefilename']);
$datecontent = utf8_decode(stripslashes($_POST['datecontent']));

Bye,
TMTisFree
TMTisFree
February 20,

This topic is archived.

See also:


Back to support forum