Hi,
I want to insert a record in mySQL and the edit it.
I’m using non-autoincrement id in the table.
Files are as follows:
[php/getDataStore.php]
(…)
$dataConn = new JSONDataConnector($mysqlcon,“MySQLi”);
$dataConn->render_table(“mytable”,“id”,“id,descr”);
[main.js]
(…)
var myDataStore = new dhtmlXDataStore({
url: “php/getDataStore.php”,
dataFeed: “php/getDataStore.php”,
datatype: “json”
});
(…)
myGrid.setColumnIds(“id,descr”);
myGrid.init();
myGrid.sync(myDataStore );
var dp = new dataProcessor(“php/getDataStore.php”);
dp.init(myDataStore );
(…)
myButton.attachEvent(“onclick”,function(id){
var json_obj = getMyJSON(“php/getnewid.php?table=mytable&field=id”); //returns newId
newId = json_obj[0].newId;
var dataToInsert = {
id: newId,
descr: "new description"
};
myDataStore .add(dataToInsert );
// so far data are inserted correctly in the db
// but the grid displays 0 as id
// "old" record may be changed in the grid (and immediatly updated in the db)
// but editing in the grid the new record does nothig on db
// so I've tried adding this:
dp.sendData();
myGrid.clearAll();
myGrid.sync(myDataStore );
// after this grid shows the correct id,
// but editing in the grid the new record does nothig on db
// (while "old" ones are updated correctly)
};
});
What is the correct way to:
- show records on a grid
- insert (a) new record by pressing a button
- show + select the new record on the grid (and linked forms)
- let the user change values of the new inserted record
- save modified record to db ?
I guess it may be something that everyones does everyday with dxtmlx,
but after several days I can’t find a way (nor some working example using mySQL) to get out of this mess…
Thank you for any help.
Please note that if the ID is set to autoincrement on MySQL
(and removed from render_table field list
$dataConn->render_table(“mytable”,“id”,“descr”);
)
everything works fine…
Hi,
The code, which is exactly the same, as the above one, must work correctly.
I have created a local sample, and it works for me.
But problem can really occur if you are changing the id value after the initial adding, as ID as row id and ID as content of columns are two separate entities, and while the ID as value can be change, ID of row need a special command call to be change ( grid.changeRowId )
I’ve mada a new GRID connector:
php/getDataGrid.php is:
$gridConn = new GridConnector($mysqlcon,“MySQLi”); // connector initialization
$gridConn->render_table(“mytable”,“id”,“id,descr”); // data configuration
And a new button that clears the grid and reloads data:
myGrid.clearAll();
myGrid.load(“php/getDataGrid.php”);
after that the grid displays all the records correctly (with IDs ok)
changes the “old” records, but dies nothing when changing the new inserted
moreover,if I add the following line:
myGrid.sync(myDataStore);
the grid returns to have all record but the new inserted presents ID=0
even more: data processon onAfterUpdate event is not fired when updating the new record
after gird reload or grid.changeRowId
i’ve made a mini-playground zip to test this behaviour:
please help me !!!
TestNewInsert.zip (300 KB)
I’ve tried to change the database colum “id” to “xid”:
dataToBeinserted.xid=55
dataToBeinserted.descr=New record
sending myTableDataStore.add(dataToBeInserted);
myTableDataStore.onAfterAdd: id=1460635986466 pos=3
it seems that datastore generate a new id 1460635986466 even if I pass 55 as xid…
(even so the grid present 0 as xid ad any updating goes with 0 as id…)
Hi,
You are using GridConnector and JSONDataConnector, both of which expect that id field in a database will have autoIncrement. After adding a new record, the connector will ask the new ID of a row from a database, and if id field has not auto increment, 0 value will be returned.
I think, you need to separate the real id and editable id. Add an extra field to the database ( like my_id ) and use it as a column in the datatable, while id value will be used as row id.
I’m not sure I have understood well, but:
- I’ve created a new field in the database (my_id) and set as primary key autoincrement
- i’ve told in php that my_id is the key:
(…)
$dataConn->render_table(“mytable”,“my_id”,“id,descr”);
what happens now is that the record is well inserted (here the log)
INSERT INTO mytable(id
,descr
,) VALUES (‘21’,‘mydescription’)
where 21 is the given Id (the autoincrement value assigned by mysql is 100)
but the grid displays 100 in the “id” column instead of 21
now, when I modify the descr field, the query sent to the db is:
UPDATE mytable SET id
=‘100’,descr
=my modified description' WHERE
my_id`=‘100’
changing the “id” field (that is my real key to several child tables) to 100.
It seems that when “DHTMLX” founds a database field that has the name “id” it
assumes that this field is the id (I don’t know what id: the grid rowid, the datastore etc)
To be sure i’ve renamed the id field in the database to xid and everithing works well
(but change ID field to XID in the real database is a way that I cannot use).
Any suggestions?
Thank you.
Hi,
If it possible change the columns like next
- id - autogenerated id filed, not used in business logic
- my_id - id field which you want to edit and use in business logic
- descr - any other fields
then on server side you can use
$dataConn->render_table("mytable","id","my_id,descr");
and on client side
myGrid.setColumnIds("my_id,descr");
Hi to all.
I need to create a new record on db with a button and edit it on a grid.
After several days and bad words I’ve finally found a workaround to do this (not very elegant, but it works).
Quick sayd,
I create an invisible window with an invisible form that calls a php page that insert the new record.
A callback function will then refresh the grid.
(ops, I can’t refresh the grid [but it may be just for my ignorance],
so I’ve recreate and reassigned datastore and dataProcessor ).
someComponent.attachEvent("onClick", function (buttonID) {
if (buttonID === "btnCreateNewRecord") {
var url="php/createNewRecord.php";
var dhxWins = new dhtmlXWindows();
var loginWindow = dhxWins.createWindow();
var myFormData = [];
var myForm = loginWindow.attachForm(myFormData,true);
var callback = function(loader,response) {
alert(response);
myGrid.clearAll();
myDataStore= new dhtmlXDataStore({
url: "php/someDataStore.php",
dataFeed: "php/someDataStore.php",
datatype: "json"
});
myGrid.sync(myDataStore);
var dp = new dataProcessor ("php/someDataStore.php");
dp .init(myDataStore);
};
myForm.send(url, callback);
dhxWins.unload();
};
Hope it may be useful for someone.
Feel free to criticize this dirty trick (I’ll change my code if someone put a working piece of code).
Bye !