How to capture the new id after an Add

I’m performing an Add to a DataStore and I can see that the server is returning the new id (tid).

How can I capture that so that I may assign it to the ‘id’ of the form?

(I have a related post in the Grid forum as well. Same problem, just coming at it with a new perspective.)

Thank you,
Rob

Normally datastore must update new id automatically, but if it somehow doesn’t work you can use

store.data.attachEvent("onIdChange", function(old_id, new_id){ //do something here });

Stanislav,

Thank you, but that didn’t solve my problem :frowning:

Here’s a little more background on what I’m doing:
I have a datastore, to which I’ve bound a grid on a tab that isn’t visible to all users. What is visible is the form, which is bound to the grid.
Initially, the user gets a blank form that they fill in. When they click the submit button, I add the data to the datastore and get the new id. Now, thanks to you, I am able to assign that id to the id field in the form. They have the option to continue to edit their data. When they click Submit the second time, it needs to save the data from the form and have it updated on the server.

I am getting a ‘target’ error and it is failing there, never sending the data the second time.

Here’s the error I’m getting:
[color=red]
target is undefined
target._dhx_proto_wait && (target = target._dhx_proto_wait[0]); on datastore.js (line 18)

What I think is happening is that, while I’m saving the data to the datastore, there’s no clear path of correlation between the new data in the datastore and the form - the grid isn’t getting the info, or it isn’t relating the displayed info to the info in the form.

Here’s my code (abbreviated for relevance)

		form_1.attachEvent("onButtonClick", function(name, command){
			switch(name){
				case "cancel":
					tabbar_1.showTab('tab_1');
					tabbar_1.setTabActive('tab_1');
					tabbar_1.hideTab('tab_2');
//					form_1.clear();
					break; 
				case "save":
					var keyword = form_1.getItemValue('keyword');
					if (keyword =='') {
						alert('You must have a valid and available keyword.');
						break;
					} else {
						saveVCard();
						break; 
					}
			}
		});
		function saveVCard(){
//			alert(form_1.getItemValue('id'));
//			alert(gr_vCard.getSelectedRowId());
			if (form_1.getItemValue('newVC')==1){
				vCards.add({
					keyword:form_1.getItemValue('keyword')
				});
			} else { form_1.save(); }
		}

		var vCards = new dhtmlXDataStore({  
			url:"dp-vcard.php",
			datatype:"xml"
		});

		vCardDP = new dataProcessor("dp-vcard.php");
 		vCardDP.init(vCards); 
		vCardDP.setTransactionMode("POST",true);
		vCardDP.enableDataNames(true);
 		gr_vCard.sync(vCards);
		form_1.bind(gr_vCard);

		vCards.data.attachEvent("onIdChange", function(old_id, new_id){
		   form_1.setItemValue('id',new_id);
		   form_1.setItemValue('newVC',0);
		});

I’m not sure if this is a DataStore or a Grid/Form binding issue, but the error is occuring in the datastore.js file.

Thank you for your help!
Rob

Please try to update datastore codebase
codebase_1105.zip (35.4 KB)

Stanislav,

I tried that, and got the same results.

I’m afraid I have a logic problem, but I’m not sure how to address it.

From my description,

Client enters data in new record
Form checks to see if this is a new record or an existing record
If it is a new record, it adds record to datastore
If it is an existing record, it saves the data back to the grid, which updates the datastore. (But it has no reference to a record in the grid.)

This is where I think my problem is. I’m adding a record to the datastore, but not linking the data that is in the form to anything that exists in the grid.

(The grid is for “administrator use” - being able to manage multiple records.)
Should I be ‘updating’ the datastore? If I do, is the grid automatically updated/synced as well?

Or should I be reloading the data on the form from a record (that was added) in the grid? How do I do that?

Thank you,
Rob

One of possible designs is the next

datastore - loads and saves data (dataprocessor attached to it)
form.bind(datasource) - form shows selected record
grid.sync(datastore) - grid shows all data in datastore

with such design, when you will add new record to datastore it will appear in grid automatically. ( and all changes in datastore will be reflected in grid as well )

Also, you can use a simplified version, without datastore

  • load and save data though grid
  • assign form to grid ( form.bind(grid) )

That’s what I’m doing. Only I’m not displaying the grid to the user. <I plan on enabling multiple registrations later, but not yet. I don’t want to cause confusion with the first registration.>

So when I add the record to the datastore, I’m confident it is getting added to the grid. But, since I’m not changing the display, how do I relate the displayed data to the new record in the grid (when it has already been added to the datastore)? <Because when I edit an existing record, I am updating the grid, not the datastore.> Does this make sense?

I have no concerns with loading a record from the grid into the display - I’m just not sure what command will do that - without showing the grid and selecting a new record manually.

Thank you,
Rob

Because when I edit an existing record, I am updating the grid, not the datastore.
Does this make sense?

Is any reason why you not changing data directly in datastore. If above binding applied, you only need to call form.save to push update from form to datastore, which will trigger saving to database and update in grid.

Anyway, if grid has copied data from the datastore, it must have row with the same id as id of record in datastore and form id - so you can access it by such id and change as necessary.

I’m just not sure what command will do that - without showing the grid and selecting a new record manually.

if you have form bind to grid
grid.setCursor(id);
or,
grid.selectRowById(id);

first one will not make visual selection.

Stanislav,

From the code I have provided, you can see that I am calling form.save(). I think the problem is because the form is bound to the grid and the grid to the datastore. But I don’t know; I’m guessing.

Again, when I call form.save(), I’m getting an error,
target is undefined
target._dhx_proto_wait && (target = target._dhx_proto_wait[0]); on datastore.js (line 18)

I assumed that the “target” had to do with the index of the information being displayed not being properly set in the form. I don’t have a problem when I load a record that exists in the grid/datastore, only when I create a new record and attempt to edit it after saving.

Thank you,
Rob

PS - when I tried the datastore.js that you provided, the entire app stopped working. So I am still using the 10-10-2011 version of datastore.js.

Please check the attached sample
21_grid2form2store.zip (1.02 KB)