I am particularly new to Drupal. I am trying to create a table webform. I have multiple form elements in each row. On altering one of the form element in a row, a row should be appended in the table. What would be the best way to do it??

CommentFileSizeAuthor
#7 form.png2 KBdtksmsl

Comments

rfay’s picture

Project: Drupal core » Webform
Version: 7.2 » 7.x-3.x-dev
Component: ajax system » Code
Assigned: dtksmsl » Unassigned
Priority: Critical » Normal

I assume this is a support request for webform module, so changing to webform queue.

quicksketch’s picture

This is not possible with Webform module. See the similar request over in #354381: Webform Submission with "add another" text field.

dtksmsl’s picture

I am trying to do this by using an array. The ajax callback has to update a value in a text field. But my problem is that I am not able to change a value in a text field from inside ajax callback function.

quicksketch’s picture

First of all, are you actually using Webform module at all? If this is a general Form API question, this request was probably miscategorized. If you are using Webform module, I don't provide support on custom coding in the issue queue.

dtksmsl’s picture

I am using the form API. How do I correctly categorize this?

quicksketch’s picture

Project: Webform » Drupal core
Version: 7.x-3.x-dev » 7.2
Component: Code » ajax system

So looks like you had it correctly categorized to begin with, the mention of a "table webform" threw everyone off because "webform" is a specific module in Drupal.

There are generally two ways to do what you're asking. First you could replace the entire table in the form but add an "ajax-new-content" class to the last row in the table to make the new table row fade in after the AJAX request. Even though it looks like modules are simply adding a new row to the table, most of them use this approach to add a new row to a table. Examples of this include File upload fields and all the "Add another item" buttons provided by Field module.

The other way you can do this is by sending back and AJAX command for append on the tbody tag (http://api.drupal.org/api/drupal/includes--ajax.inc/function/ajax_comman...) and then restripe the table with the restripe command (http://api.drupal.org/api/drupal/includes--ajax.inc/function/ajax_comman...).

dtksmsl’s picture

Issue tags: +Ajax, +callback
StatusFileSize
new2 KB

Append would not work because in a row I have two textfields that get populated after the ajax callback. So the ajax callback would replace the current row and also append a fresh new row just below it. I am trying to use a counter which has to be updated from inside the ajax callback function. But after trying for hours I couldn't update the counter from inside the callback function. I tried drupal_set_value(), also tried global variables, I tried, form_state[][][] but to no avail.

The row looks like in this image attached. The disabled fields are actually to be replaced by ajax callback.

quicksketch’s picture

Title: Auto add a row in a tabular webform » Auto add a row in a tabular form

In which case you should also put a #submit operation on your form to set the necessary values. The #ajax callback you have specified will happen after the #submit handler. So the #submit handler sets all the values, then the #ajax callback just returns the new data. I suggest looking at http://api.drupal.org/poll_form or the Examples module to learn how to do AJAX properly with Forms API.

webankit’s picture

How to achieve this via ajax-new-content class?
I added this class to my last row.
(I am using $form_state['count'] for my last row max delta)
But what next...
Do I need to include some css file?
My table become normal div after ajax, as if theme function didn't run.

dddave’s picture

Status: Active » Fixed

The OP was fixed with quicksketch's remarks. #9 is not relevant to core. If you cannot solve this by reading up docs you should better ask in IRC for help.

Automatically closed -- issue fixed for 2 weeks with no activity.