Page 1 of 1
Datagrid closefield event?
Posted: Wed Mar 16, 2022 2:04 pm
by Zax
Hello,
I know it has already been discussed but I don't find a correct answer.
Is there is a way to trap the equivalent of "
on closeField", but for a Datagrid (table) editable cell? I would like to know what row
and cell has been modified.
I found:
Code: Select all
on closeFieldEditor pFEditor
put the dgHilitedIndexes of me into numIndex
put the dgDataOfIndex[numIndex] of me into tLine
...
end closeFieldEditor
But there is 2 problems:
1 - only the modified row is known, not the cell
2 - tLine (array)
only contains the previous values of the DG line, not the ones after "closing" the cell.
Thank you.
@Klaus: it is the right time to answer a very simple built-in function and say "it's Livecode"

Re: Datagrid closefield event?
Posted: Wed Mar 16, 2022 2:18 pm
by Klaus
Hi Zax,
well, datagrids are quite complicated beasts, so no easy built-in function!
This is from the Data_Grid.pdf from the LC lessons site:
Code: Select all
on CloseFieldEditor pFieldEditor
put the dgIndex of me into theIndex
put the dgDataOfIndex[theIndex] of the dgControl of me into theDataA put the text of pFieldEditor into theDataA[the dgColumn of me]
set the dgDataOfIndex[theIndex] of the dgControl of me to theDataA
end CloseFieldEditor
So you could check
-> the dgcolumn of me = the column that is being updated
and
-> theDataA = the updated array of the index
at the end of that handler
Is that what you mean?
Best
Klaus
Re: Datagrid closefield event?
Posted: Wed Mar 16, 2022 2:47 pm
by Zax
Yes !!!
Thank you very much Klaus, even it's not an easy one-line function.
EDIT : is there a way to know the colum's name of the edited cell?
EDIT 2: "the dgColumn of me" is empty.
BTW: where is this .pdf you are talking about?
Re: Datagrid closefield event?
Posted: Wed Mar 16, 2022 3:10 pm
by dunbarx
The real issue with datagrids is that when a "cell" seems to be open for editing, what you are seeing is in fact another field entirely, that overlies the cell if of interest, called in many discussions on this forum a "phantom" field. It is in that field that you edit data, and when you close out, the data in that field is loaded into a "real" field just below.
The actual fields in a DG are named something like "Col 1 0005", which is sort of self explanatory.
But the phantom field does not send "exitField" or "closeField" messages. You would have to hack the DG itself, very doable, since it is 100% built from standard LiveCode controls, but not easy.
Klaus loves to call them "beasts".
Craig
Re: Datagrid closefield event?
Posted: Wed Mar 16, 2022 3:21 pm
by Zax
Well, I could hack the "EditValue" command from the default behavior, and set new custom properties, like this:
Code: Select all
command EditValue
EditFieldText the long ID of me, the dgIndex of me, the dgColumn of me
set the MT_editIndex of the dgControl of me to the dgIndex of me
set the MT_editColName of the dgControl of me to the dgColumn of me
end EditValue
But it seems rather complicated, and maybe not very safe. So I would prefer Klaus' way... if only I could retrieve the column's name.
Re: Datagrid closefield event?
Posted: Wed Mar 16, 2022 4:09 pm
by dunbarx
If it is of any help to you, that delicate ephemeral field that the DG builds on the fly is named "dataGridFieldEditor". It is hard to catch.
Craig
Re: Datagrid closefield event?
Posted: Wed Mar 16, 2022 4:45 pm
by Klaus
Hi Zax,
sorry, looks like the code snippt will only work if you also evoked the FieldEditor yourself via script.
And unfortunately, the DataGrid PDF is not avaialable anymore on the LC website.
I put it in my Dropbox:
https://www.dropbox.com/s/8fkuzeipk48q8 ... f.zip?dl=1
I am currently looking into the 15.000 lines of the datagrid library to find a hint on the column clicked.
Will report here if I am successful...
Best
Klaus
Re: Datagrid closefield event?
Posted: Wed Mar 16, 2022 4:53 pm
by Zax
Thanks for the upload Klaus
I will also look at the pdf. I suggest to both report here, even if we are not successful.
Re: Datagrid closefield event?
Posted: Wed Mar 16, 2022 4:58 pm
by Klaus
Success!
Put this into the script of the datagrid group and see what the messagebox shows:
Code: Select all
command EditFieldText pField, pIndex, pKey
put "Index:" && pIndex & CR & "Columnname:" && pKey
## Most important line:
pass EditFieldText
end EditFieldText
Tested and works!
Re: Datagrid closefield event?
Posted: Thu Mar 17, 2022 6:54 am
by Zax
Thank you so much for your time Klaus
I tested your script. It's an "openField" script that does the same thing that the one I posted earlier:
Code: Select all
command EditValue
EditFieldText the long ID of me, the dgIndex of me, the dgColumn of me
set the MT_editIndex of the dgControl of me to the dgIndex of me
set the MT_editColName of the dgControl of me to the dgColumn of me
---- testing
put "Index:" && the dgIndex of me & CR & "Columnname:" && the dgColumn of me
end EditValue
However, yours is more convenient because it can be placed in the DG grid script itselft, and not in a deep and mysterious template.
But I have to say that an "openField" script is not exactly what I am looking for because I need to perform action on "closeField" (when cell data has been modified).
As far as I can see, the problem is
the dgColumn: I don't understand when it can be accessed or not.
Re: Datagrid closefield event?
Posted: Thu Mar 17, 2022 7:23 am
by Zax
OK, I finally found it
This script must be placed in the
default column behavior, following this lesson:
https://lessons.livecode.com/m/datagrid ... -to-a-cell
Code: Select all
on CloseFieldEditor pFieldEditor
put the dgIndex of me into tIndex
put the dgColumn of me into colName
put the text of pFieldEditor into newCellData
put "tIndex=" & tIndex & "---colName=" & colName & "---newCellData=" & newCellData
end CloseFieldEditor
Re: Datagrid closefield event?
Posted: Thu Mar 17, 2022 10:17 am
by Klaus
Glad you conquered your complicated beast!

Re: Datagrid closefield event?
Posted: Thu Mar 17, 2022 4:05 pm
by stam
Re: Datagrid closefield event?
Posted: Thu Mar 17, 2022 4:15 pm
by Zax
Thanks for the link.