Forum OpenACS Development: Forms X matrix input structure

Collapse
Posted by Iuri Sampaio on
Hi there,

I have been struggling for 5 long days trying to figure a way out to create a form to handle the creation of several items at once.

I thought the best way to go would be that the formulary might manage the creation of items using a matrix structure.

Furthermore, I would like the form having the ability to load data at the moment the user selects the item's code in the first field of the row.
As in the javascript onChange event the form would get refreshed and the fields filled.
Moreover the scenario would be that for every row the user selects the first value the rest of the fields might be filled automatically. The after he chooses as many items (rows) he wants, he could hit submit and all the items would be sent to the database

Here it is the link to see the interface
http://iurix.com/cnauto/assurances/assurance-ae-2?assurance_id=272520

I already tried some ajax grid samples such as http://www.sigmawidgets.com/products/sigma_grid2/ but mixing it with TCL turns out into a nightmare.

Does anyone have an enlightening idea?

Collapse
Posted by Dave Bauer on
Genreally what you want to do is create elements named like

field1.0 field2.0 field3.0
field1.1 field2.1 field3.1

And automatically append new rows with the row number appended with a dot to the field name.

Then you can use

ad_page_contract {

} {
field1:array,optional
field2:array,optional
field3:array,optional
}

You can still use ad_form for the form processing blocks but you'll need to loop over all the arrays to get each row and the process them that way.

There isn't any openacs code specifically that you need to fill fields based on the value of a previous field.

You'll need to add event handlers to the fields (don't use onChange). There are many examples everywhere using whatever javascript you want, plain javascript, YUI, jQuery etc. They patterns are all very similar.

Try this google search

https://www.google.com/?q=update%20options%20of%20select%20based%20on%20previous%20select

Collapse
Posted by Iuri Sampaio on
Dave,

What do you mean by "don't use onChange" javascript event?
How would I load up the other fields then?

For example, let's supose the user selects field1.0.
The form must be refreshed and the fields must be automatically filled. "Every row respectively"

field2.0 field3.0 field4.0 field5.0

I agree their patterns are very much the same. I would rather to go with plain javascript. That way I get a cleaner mix with TCL, becasue I wouldn't be using any other libraries with xml and json data structures.

The hard part, that I am stuck at, is to load the fields into the html inputs. Mainly how to pass the data between javascript and TCL, and what structure to use.

I guess i have the knowledge about the pieces, however I still don't have a picture of "how" to accomplish the task.

Collapse
Posted by Ryan Gallimore on
Build your datasource in Tcl as usual, then output the results to your ADP as Javascript.

To send data from your form to the db (without refreshing), execute an AJAX request with your favourite library to a Tcl file that saves the data to the db.

Don't use "onChange", instead use an Event Listener (google addEventListener).

Hope that helps.

Collapse
Posted by Iuri Sampaio on
Ryan and Dave,
Thank you guys for the feedback.
It helped a lot. I don't know how but it works! (sharing and writting about difficulties surprisingly open the mind. It makes you to look outsite the box.

It turned out I wrote the datasource on TCL and a json structure to move the data here and there, between the javascript and TCL structures.

I saw openacs lacks json interfaces. Its API is very poor. Isn't?

Am I correct?

Collapse
Posted by Torben Brosten on
Iuri,

You might be interested in the qf_ form API. It's made for scenarios like the one you have, with multiple arrays, dynamically declared forms and fields etc:

https://openacs.org/forums/message-view?message_id=3602056

It doesn't include javascript, but it's easily added.

cheers,

Torben

Collapse
Posted by Dave Bauer on
OpenACS 5.7 has json-procs.tcl in the acs-tcl package.