Start Building Professional
Web Apps Today


 
Categories Question details Back To List
Question  posted by cheryl tisland on Nov 09, 2009 18:58
open dhtmlx forum
grid insert/update refresh xml

Hi,
I am using the data processor to insert or update and on insert I need to refresh the grid in order to have the id of the record that was inserted in case there is a subsequent update. Problem is that I cannot use the id that the grid sends to me since the table that I am inserting into has it's own id scheme. I actually need to have the id that is created server side for the record. So, what I am thinking is to somehow do an afterInsert javascript function that refreshes the grid fro the inquiry_customer_info_contactdetail_xml.php. Is this possible?

my grid is set up like this:

mygrid = new dhtmlXGridObject('contact_detail');
mygrid.setImagePath("images/gridimages/");
mygrid.setHeader("UserID,Active,Contact Name, Phone, Partner Location, Comments, Email Address, Title,2nd Phone, Fax, Position, Description, Birthday, Greeting");
mygrid.setInitWidths("100,50,100,100,100,100,100,100,100,100,100,100,100,100");
mygrid.setColumnIds("userid, active, contactname,phone,partnerlocation,comments,emailaddress,title,2ndphone,fax,position,description,birthday,greeting");
mygrid.setColAlign("left,left,left,left,left,left,left,left,left,left,left,left,left,left")
mygrid.setSkin("light");
mygrid.setColSorting("str,str,str,str,str,str,str,str,str,str,str,str,str,str");
mygrid.setColTypes("ed,ed,ed,ed,combo,ed,ed,ed,ed,ed,combo,ed,ed,combo");
    mygrid.enablePaging(true,5,5,"pagingArea",true,"recinfoArea");
//mygrid.attachEvent("onRowSelect",doOnRowSelected);
//combo = mygrid.getColumnCombo(0);
//combo.enableFilteringMode(true);
//combo.loadXML("functions/buildCombos.php?comboType=c_greeting&orgfilter=false&selectedID=1000000");
    mygrid.init();
    mygrid.enableSmartRendering(true);
    mygrid.loadXML("inquiry_customer_info_contactdetail_xml.php?c_bpartner_id=<?php echo $c_bpartner_id;?>");
    var dp = new dataProcessor("php/update.php?c_bpartner_id=<?php echo $c_bpartner_id;?>");
    dp.setTransactionMode("GET",true);
    dp.setUpdateMode("row");
    dp.setVerificator(1,checkRowForInsert);

    //dp.enableDataNames(true);
    dp.init(mygrid);


my update.php looks like this:

$newId = $_REQUEST["ids"];
$mode = $_REQUEST[$newId."_"."!nativeeditor_status"]; //get request mode
$rowId = $_REQUEST[$newId."_"."gr_id"]; //id or row which was updated
$customerid=$_REQUEST['c_bpartner_id'];
$userid=$_REQUEST[$newId."_"."c0"];
$contactname=$_REQUEST[$newId."_"."c2"];
$contactemail=$_REQUEST[$newId."_"."c6"];
$contactphone=$_REQUEST[$newId."_"."c3"];



function insert_row(){
    global $newId;
    global $customerid, $userid, $contactname, $contactemail, $contactphone;
    $query="insert into users (customerid, name,email,phone) values('".$customerid."', '".$contactname."','".$contactemail."','".$contactphone."')";
    $db->query($query);
    return "inserted";
}
function update_row(){
    global $newId;
    global $customerid, $userid, $contactname, $contactemail, $contactphone;
    $query="update users set customerid='".$customerid."', name='".$contactname."', email='".$contactemail."',phone='".$contactphone."' where userid=".$userid)";
    $db->query($query);
    return "updated";

}
function delete_row(){
    return "deleted";
}
//include XML Header (as response will be in xml format)
header("Content-type: text/xml");
//encoding may differ in your case
echo('<?xml version="1.0" encoding="UTF-8"?>');



switch($mode){
case "inserted":
//row adding request
$action = insert_row();
break;
case "deleted":
//row deleting request
$action = delete_row();
break;
default:
//row updating request
$action = update_row();
break;
}


//output update results
echo "<data>";
echo "<action type='".$action."' sid='".$rowId."' tid='".$newId."'/>";
echo "</data>";

?>
Answer posted by Stanislav (support) on Nov 10, 2009 01:45
a) The dataprocessor allows to update the ID of newly updated row ( it is a common use-case ) 


$query="insert into ... 
$db->query($query);
$newId = $db->get_new_id(); // get new ID for the record
return "inserted";

This change will be enough to force  data update on client side. 

b) Technically, if you need to call something after inserting it can be done as

dp.attachEvent("onAfterUpdate",function(sid,action,tid){
    if (action == "insert" ){
             do_something();
    }

    return true;
})