Bug in postSubmit, submitSuccess events
Bug in postSubmit, submitSuccess events
rf1234
Posts: 2,986Questions: 87Answers: 421
Good morning Allan,
this code isn't working any longer with the "create" action. It used to work with previous Editor versions but not any longer.
.on( 'postSubmit', function ( e, json, data, action ) {
if ( action === 'edit' || action === 'create' ) {
var selected = contractTable.row( {selected: true} );
if (selected.any()) {
$.ajax({
type: "POST",
url: 'actions.php?action=generateCashFlow',
data: {
table: 'variable',
id: json.data[0].DT_RowId.substring(4), //only after "row_"
contractId: selected.data().contract.id
}
});
}
}
ajaxReloadTbls( [contractTable, cashFlowTable] );
});
with the "create" action json.data is an empty array; no DT_RowId, nothing... I also tried this with the submitSuccess event. Same issue. It works fine with the "edit" action. Could you provide a fix please. Many thanks.
This discussion has been closed.
Replies
Hi,
I fear that this is likely another case of this bug in 1.6.3. It will be fixed in 1.6.4. If I find a workaround before then, I'll post back with it.
Allan
thanks
Hi Allan, sorry but this bug wasn't fixed in 1.6.4. Just tried it. You mentioned the other bug that you linked to above in the relase note but not this one.
So it still doesn't work with "create", only with "edit". Can this be fixed please.
Just checked it with my debugger: json.data is an empty array if action === 'create'. Same problem as before. Will have to go back to 1.6.2 without a fix.
Interesting. I've just tried the following on this example:
and it appears to work without any issues - the data array from the JSON return is shown in the console.
Are you able to give me a link to a page showing the issue? Or perhaps your Editor configuration so I can see if there is any difference between that example and your use case?
Thanks,
Allan
Let's try the Editor configuration first. The test case would be rather complex to explain.
This is the configuration:
I tried it with a much simpler stand alone table - and it worked (checked with debugger). This Editor instance has the same default settings as the one above:
The one above is a child table; will try with the parent table of the above table after lunch and get back to you.
Now I tried with the parent table "contract". Works fine! So it does not work with the child table "fixed". It works with the stand alone table "rate" and the parent table of "fixed" which is "contract".
That's the
contractFixedEditor
Editor is it? I don't see anything immediately wrong with that.Could you show me the JSON that the server is returning in response to that create Ajax request?
Thanks,
Allan
Now to complete the picture I will try with a different child table and see whether I can reproduce the problem:
Tried with table "userphone" which is a simple child table of "user" - and it worked. Now I am really confused ...
Yes it is "contractFixedEditor" that has the problem. Hang on will send you the JSON shortly.
And this is the JSON response from an update (which is ok):
The JSON response from the create shows
data: []
- sopostSubmit
appears to be correct in saying that the data array is an empty array, since it is.The question therefore becomes, why is the server not sending data back for that created row?
Might the WHERE condition not be satisfied?
Allan
Hi Allan, I looked at the debug SQL myself and I think I understand the problem now:
I have three tables here "contract", "contract_has_fixed" (which is a link table) and "fixed". If I select a contract (parent table) I can create, edit or delete a child element "fixed".
Upon create of a "fixed" element Editor unfortunately does not automatically insert the link table "contract_has_fixed" which only has the two id's. Hence I do the insert myself on "postCreate".
As I see in the debug SQL Editor reads the inserted record from "fixed" immediately after the insert. At that time the link table insert has not been done. As you can see it comes later. The result is therefore empty! That explains the empty json response.
Question: How can I make editor read the inserted record only after the insert of the link table? Or, if that's not possible, how can I make Editor insert the link table directly so that I don't have to do it manually myself?
this is the PHP Editor instance:
Use the
writeCreate
event rather thanpostCreate
.writeCreate
will trigger before Editor gets the data from the database.Allan
This is overlapping, Allan! Will try writeCreate. My problem is I mostly don't understand those subtle differences between the events ... a little more detail in the description could be helpful sometimes ...
in the meantime I developed this:
tried now various versions of left join and Mjoin but no success.
Then I followed this example https://editor.datatables.net/examples/advanced/joinLinkTable.html in a very strict manner. In order to imitate what's going on in the example I created some kind of "pseudo-interaction" with the front-end even though it seems to be useless ... but anyway this way I could persuade Editor to do the right thing: Insert the link table BEFORE reading back the values. I would call this a work around.
PHP: I expanded the left join also to include the "contract" table. Secondly I added an options instance for contract_has_fixed.contract_id which is being filled with contract.id. I defined a pseudo label "type" which nobody sees at the front end. And of course I deleted the manual "insertLinkTable".
Javascript: I added a field type "hidden" called "contract_has_fixed.contract_id". I preset the field with the selected contract's id. Then contract.id gets returned from the client and can be inserted automatically into the link table.
No, I am wrong: WriteCreate is described really well. I missed it because it only came in 1.6.2 and I built these things with 1.5.x Thanks again!
PHP: This is my final solution - no front end changes required just this:
@allan, in one of the examples above I got the left join insert into the link table working but not with a regular left join but I had to use an options instance, send stuff to the client that isn't really needed etc ..
Is it possible to avoid this? I mean to achieve this without all of this and without a “manual“ insert of the link table?
You can update to a left joined table, but I don't think you can currently insert to it. The reason is that if the primary key for the left joined table is submitted, that will be used as the where condition for which row to write to. But if the primary key is not submitted, it is assumed that the table shouldn't be written to, rather than being inserted to.
I've wondered about this before and how it could be improved. Perhaps submitting
null
for the primary key or an empty string should allow an insert.Allan
"Perhaps submitting null for the primary key or an empty string should allow an insert."
Would be a great improvement for Editor 1.6.5 later this week
And less work in the forums for you