Issue with using connector for grid update

I am new to dhtmlx. I got almost everything working but using connector to update grid values in server is not working. I am not using autoupdate of Data processor [.setUpdateMode(“off”);] and hence insert/update is triggered manually.

I use a php file to generate the xml to display in the grid but I’m trying to use connector to update/insert the modified or created values in the grid. The values are displaying correctly but update does not work. It says in correct xml. The code for the php which uses the connector for update is given below:

....DB Connection function....
require("grid_connector.php");
$gridConn = new GridConnector($con,"MySQL");
if ($gridConn->is_select_mode())
	$gridConn->render_sql("select id,name,cof,start_mnth from wg_def where disabled=0","id","name,cof,start_mnth");
else
	$gridConn->render_table("wg_def","id","name,cof,start_mnth");

  function my_update($data){
		 $name=$data->get_value("name");
		 $id=$data->get_value("id");
		 $cof=$data->get_value("cof");
		 $sd=$data->get_value("start_mnth");
		 $gridConn->sql->query("UPDATE wg_def SET name='" . $name. "',cof=" . $cof . ",start_mnth=" . $sd . " where id=" . $id );
		 $data->success(); //mark operation as finished
  } ;
  function my_insert($data){
		 $name=$data->get_value("name");
		 $cof=$data->get_value("cof");
		 $sd=$data->get_value("start_mnth");
		 if(mysql_result(mysql_query("select count(*) from wg_def where name='". $name ."'"),0)==0){
			 $gridConn->sql->query("insert into wg_def(name,cof,start_mnth) values('" . $name . "'," . $cof . "," . $sd . ")");
			 $data->success(); //mark operation as finished
		 }else{
		 	$data->invalid();	
		 };
  } ;
  
  function canceldel($action){
  		$action->invalid(); //deletion not allowed
  };
  function validate($data){
             if ($data->get_value("name")=="")
                  $data->invalid();
   };

	$gridConn->event->attach("beforeUpdate","my_update");
	$gridConn->event->attach("beforeDelete","canceldel");
	$gridConn->event->attach("beforeProcessing","validate");
	$gridConn->event->attach("beforeInsert","my_insert");

Any idea guys where its going wrong?
Debugging using Firebug shows the following xml is sent to the above code during update/insert as below:

<?xml version='1.0' encoding='utf-8' ?><rows><row id='1'><cell><![CDATA[GTS]]></cell><cell><![CDATA[1]]></cell><cell><![CDATA[0]]></cell></row></rows>

I have no clue how to proceed from here. Any help is highly appreciated :exclamation:

Be sure that

a) dhtmlxconnector.js is included on the page and included after dhtmlxdataprocessor.js
b) you are not changing post mode of dataprocessor ( not using dp.setTransactionMode )

I tried it but no good :confused:

There are no error messages and the update fails too…
The DP code is given below:

	myDP = new dataProcessor("wg.php");
	myDP.setUpdateMode("off");
    myDP.init(mygrid1);
	myDP.setVerificator(0, v_wgn);
	myDP.styles = {
    updated: "font-style:italic; color:green;",
    inserted: "font-weight:bold; color:blue;",
    deleted: "font-weight:bold; color:red;",
    invalid: "color:orange; text-decoration:underline;",
    error: "color:red; text-decoration:underline;",
    clear: "font-weight:normal;text-decoration:none;"
};
	myDP.enableDataNames(true);
	myDP.attachEvent("onValidatationError", function(id, messages) {
    alert(messages.join("\n"));
    return false;
    //confirm block 
    });

Any suggestions ?

remove the next line

myDP.enableDataNames(true);
it changes the naming convention, and server side code can’t parse incoming data correctly.

if it still not help - add the one more line

myDP.setTransactionMode(“POST”, true)

Tried almost everything but still not working. When I click the save button the id of the new row is submitted to server but data does not go as per firebug (debugger in Firefox)

here is the full dhtml client side function

<script src="codebase/dhtmlxcommon.js" type="text/javascript"></script>


<link rel="stylesheet" type="text/css" href="codebase/dhtmlxlayout.css">
<link rel="stylesheet" type="text/css" href="codebase/skins/dhtmlxlayout_dhx_blue.css">
<script src="codebase/dhtmlxlayout.js"></script>

<script src="codebase/dhtmlxtoolbar.js"></script>
<link rel="stylesheet" type="text/css" href="codebase/skins/dhtmlxtoolbar_dhx_blue.css">


<script src="codebase/dhtmlxgrid.js" type="text/javascript"></script>
<script src="codebase/dhtmlxgridcell.js" type="text/javascript"></script>

<link rel="STYLESHEET" type="text/css" href="codebase/dhtmlxgrid.css">
<link rel="stylesheet" type="text/css" href="codebase/skins/dhtmlxgrid_dhx_blue.css">

<!-- The order of inclusion below is impt -->
<script  src="codebase/dhtmlxdataprocessor.js"></script>
<script src="codebase/dhtmlxcontainer.js"></script>

<script language="JavaScript">
<!--
var dhxLayout,dhxT1,dhxT2,mygrid1,mygrid2,myDP,wgrp;
wgrp='';
function setpage()
{
	dhxLayout = new dhtmlXLayoutObject(document.body, "2E","dhx_blue");
	dhxLayout.cells("a").setText("Workgroups");
	dhxLayout.cells("b").setText("Define WorkStatus Types");
	dhxT1 = dhxLayout.cells("a").attachToolbar();
	dhxT1.setSkin("dhx_blue");
	dhxT1.setIconsPath("./pics/");
	dhxT1.addButton("addw",1,"New","addwg.gif","addwg.gif");
	dhxT1.setItemToolTip("addw","Create new workgroup"); 
	dhxT1.addButton("savew",2,"Save","save.gif","saved.gif");
	dhxT1.setItemToolTip("savew","Save all workgroup changes"); 
	dhxT1.addSeparator("spa",3);
	dhxT1.addButton("openw",4,"Show Leave","infog.gif","infog.gif");
	dhxT1.setItemToolTip("savew","Show leave for selected workgroup");
	dhxT1.attachEvent("onClick", function(id){
        switch(id)
		{
			case 'addw' : adw();
						break;
			case 'savew': savw();
						break;
			case 'openw': openw();
						break;
		};	
	});
	dhxT2 = dhxLayout.cells("b").attachToolbar();
	dhxT2.setSkin("dhx_blue");
	dhxT2.setIconsPath("./pics/");
	dhxT2.addButton("addh",1,"New","addwg.gif","addwg.gif");
	dhxT1.attachEvent("onClick", function(id){
        switch(id)
		{
			case 'addh': addlv();
						break;
		};	
	
	});
	mygrid1=dhxLayout.cells("a").attachGrid(); //Workgroup listing
	mygrid2=dhxLayout.cells("b").attachGrid(); // leave definition
	mygrid1.setImagePath("codebase/imgs/");
	mygrid2.setImagePath("codebase/imgs/");
	mygrid1.setHeader("Name,Comp-Off Eligible,Year Starts On");
	mygrid1.setColAlign("left,center,left");
	mygrid1.setColSorting("str,na,str");
	mygrid1.setInitWidths("120,120,*");
	mygrid1.setColTypes("ed,ch,co");
	mygrid1.setSkin("light");
	var combo = mygrid1.getCombo(2);
	combo.put('0','Joining Date');
	combo.put('1','January');
	combo.put('2','February');
	combo.put('3','March');
	combo.put('4','April');
	combo.put('5','May');
	combo.put('6','June');
	combo.put('7','July');
	combo.put('8','August');
	combo.put('9','September');
	combo.put('10','October');
	combo.put('11','November');
	combo.put('12','December');
	myDP = new dataProcessor("wg.php");
	myDP.setDataColumns([true, true, true]);
	myDP.setTransactionMode("POST", true);
	myDP.setUpdateMode("off");
    myDP.init(mygrid1);
	myDP.setVerificator(0, v_wgn);
	myDP.styles = {
    updated: "font-style:italic; color:green;",
    inserted: "font-weight:bold; color:blue;",
    deleted: "font-weight:bold; color:red;",
    invalid: "color:orange; text-decoration:none;",
    error: "color:red; text-decoration:none;",
    clear: "font-weight:normal;text-decoration:none;"
};
	//myDP.enableDataNames(true);
	
	myDP.attachEvent("onValidatationError", function(id, messages) {
    alert(messages.join("\n"));
    return false;
    //confirm block 
    });

	mygrid1.init();
	mygrid1.loadXML("wgview.php");
	dhxLayout.cells("b").collapse();
	mygrid2.setHeader("Abbreviation,Name,Leave Counter,Quantity,Applicable for,Carry-Fwd p.a,Workgroup");
	mygrid2.setColTypes("ed,ed,co,ed,co,ed,ro");
	mygrid2.setSkin("light");
	mygrid2.setColAlign("center,left,center,center,left,center,left");
	mygrid2.setColSorting("str,str,str,int,str,int,str");
	mygrid2.init();
	mygrid2.loadXML("lwgview.php");
	var lc = mygrid2.getCombo(2);
	lc.put('0','No change');
	lc.put('-1','Decrease by 1');
	var sx = mygrid2.getCombo(4);
	sx.put('M','Male only');
	sx.put('F','Female only');
	sx.put('ALL','Everyone');
};

function v_wgn(value, id, ind) {
	rv=true;
	cnt=0;
    if (value == "" && ind==0) return "Please specify a Workgroup name. It cannot be null";
	mygrid1.forEachRow(function(rid){
		if(rid != id && mygrid1.cells(rid,0).getValue().toLowerCase()==value.toLowerCase()) rv="The Workgroup already exists and cannot be recreated" ;
	});	
    return rv;
};
function addlv()
{
	//add leave data
	if(wgrp=='') alert("Please select a Workgroup to add leave type");
	else mygrid2.addRow(mygrid2.uid(),['','','-1','1','ALL','0',wgrp]);
};
function adw()
{
	mygrid1.addRow(mygrid1.uid(),['','0','0']);
};
function openw()
{
	mygrid2.clearAll();
	i=mygrid1.getSelectedRowId();
	if(i==null)
	{
		alert("ERROR: No workgroup selected\nPlease select a workgroup to see leave details");
		return;
	}else
	{
		wgrp=mygrid1.cells(i,0).getValue();
		mygrid2.loadXML("lwgview.php?wid="+wgrp);
	};
	dhxLayout.cells("b").expand();
};
function savw()
{
	myDP.sendData();
};
-->
</script>
</head>

<body onLoad="setpage()">

…and the server side of the page where the grid data is submitted.

<?
require_once('auth.inc.php');
adminchk();
require("grid_connector.php");
$gridConn = new GridConnector($con,"MySQL");
if ($gridConn->is_select_mode())
	$gridConn->render_sql("select id,name,cof,start_mnth from wg_def where disabled=0","id","name,cof,start_mnth");
else
	$gridConn->render_table("wg_def","id","name,cof,start_mnth");

  function my_update($data){
		 $name=$data->get_value("name");
		 $id=$data->get_value("id");
		 $cof=$data->get_value("cof");
		 $sd=$data->get_value("start_mnth");
		 $gridConn->sql->query("UPDATE wg_def SET name='" . $name. "',cof=" . $cof . ",start_mnth=" . $sd . " where id=" . $id );
		 $data->success(); //mark operation as finished
  } ;
  function my_insert($data){
		 $name=$data->get_value("name");
		 $cof=$data->get_value("cof");
		 $sd=$data->get_value("start_mnth");
		 if(mysql_result(mysql_query("select count(*) from wg_def where name='". $name ."'"),0)==0){
			 $gridConn->sql->query("insert into wg_def(name,cof,start_mnth) values('" . $name . "'," . $cof . "," . $sd . ")");
			 $data->success(); //mark operation as finished
		 }else{
		 	$data->invalid();	
		 };
  } ;
  
  function canceldel($action){
  		$action->invalid(); //deletion not allowed
  };
  function validate($data){
             if ($data->get_value("name")=="")
                  $data->invalid();
   };

	$gridConn->event->attach("beforeUpdate","my_update");
	$gridConn->event->attach("beforeDelete","canceldel");
	$gridConn->event->attach("beforeProcessing","validate");
	$gridConn->event->attach("beforeInsert","my_insert");
?>

Thanks guys for all time and help. Grid looks very difficult when it comes to customized integration with server to me :unamused:

Normally you need not do anything complex to enable data saving

a) include
dhtmlxdataprocessor.js
dhtmlxconnector.js

b) add
myDP = new dataProcessor(“wg.php”);
myDP.init(mygrid1);

Your code is mostly correct ( you are not including connector, but using setTransactionMode which has the same effect )

Which version of grid and connector you are using?
In case of connector 1.x it is highly recommended to use the latest version of grid ( 2.6 ), mixing new connector and older versions of grid will may cause different, hardly detectable problems.

Also, in your server side code, you need to execute all $gridConn->event->attach BEFORE render_table or render_sql command. ( the render command initiates data output , any settings after it will not hae effect on the output )

Hi,

Sorry for the delay in reply. Yes its 2.6 and 1 the latest one’s on website.I’m thionking whether i should run debug :frowning: and see.

-thanks

Debug ran with more puzzling results :frowning:

It says inserted but I don’t see where it fails now. Could this be a sql issue ? Any way to find if the $gridConn->sql->query( … ) fails ??

 row 1291205907586 marked [inserted,valid]
 row 1291205907586 marked [updated,valid]
 Initiating data sending for all rows
 row 1291205907586 unmarked [updated,invalid]
Validation error for ID=[multiple]
 row 1291205907586 marked [updated,invalid]
 Initiating data sending for all rows
 Sending all data at once
 Server url: wg.php parameters

    1291205907586gr_id = 1291205907586
    c0 = Test2
    c1 = 0
    c2 = 0
    !nativeeditor_status = inserted

 Server response received details

    <?xml version='1.0' ?><data><action type='inserted' sid='1291205907586' tid='1291205907586' ></action></data>

 Action: inserted SID:1291205907586 TID:1291205907586
 row 1291205907586 unmarked [updated,valid]

I did a thorough test and found out that on the connector server side(wg.php) complete code given above, it goes upto validation but does’nt enter the my_insert(…) / my_update(…) function thats registered for insert operations. I think this makes it easy for you to troubleshoot for me. Any idea why it fails?

$gridConn->event->attach("beforeInsert","my_insert");

  function my_insert($data){
		 $name=$data->get_value("name");
		 $cof=$data->get_value("cof");
		 $sd=$data->get_value("start_mnth");
		 if(mysql_result(mysql_query("select count(*) from wg_def where name='". $name ."'"),0)==0){
		 	 info("inserted  wg_def(name,cof,start_mnth) values(" . $name . "," . $cof . "," . $sd . ")");
			 $gridConn->sql->query("insert into wg_def(name,cof,start_mnth) values('" . $name . "'," . $cof . "," . $sd . ")");
			 $data->success(); //mark operation as finished
		 }else{
		 	$data->invalid();	
		 };
  } ;



The funny thing is validation function

$gridConn->event->attach("beforeProcessing","validate");

is called and working perfectly. any idea why insertion and updation functions fail ?
I think if you can point why this fails it will resolve this complicated issue. Thanks for your time and patience. :stuck_out_tongue:

when I say fail it means its not called at all…just to be clear

be sure that
$gridConn->event->attach(“beforeInsert”,“my_insert”);
is issued before render_table or render_sql command

render_table or render_sql command triggers request processing, so any event manipulation after that point will not have any result on the output.

Also action will not fire if you are calling $action->success(); or $action->error(); from beforeProcessing

Thanks a ton !

You are my superstar. That did the trick and the function was called finally…

Now the only issue is

$data->get_value("cof");

doesnt detect the values submitted. Is it because we dont call success() ?

get_value returns

  • value which was sent from client side
  • value which was set by set_value command

If new value was created by some db level trigger it will not be available ( you can get ID of record and request all necessary data from DB manually )