By sanderjp on
Hi there,
Well it sounds simple but I can't figure out how to do it.
I'd like to add a button with an ajax callback to each row inside a normal table. I have the following code:
$form['button'] = array(
'row2' => array(
'#type' => 'button',
'#value' => 'click',
'#ajax' => array(
'callback' => 'ajax_callback_function',
),
),
);
$form['table'] = array(
'#theme' => 'table',
'#header' => array(t('header1'),t('header2')),
'#rows' => array(array(
'row1' => 'value1',
'row2' => array(
'data' => array(
'#type' => 'button',
'#value' => 'click',
'#ajax' => array(
'callback' => 'ajax_callback_function',
),
)
),
)),
);
return $form;
Now the $form['button'] makes the button inside the form element but outside the table and when clicked it does process through ajax to the callback like expected.
Unfortunatly the button inside the table is displayed as a normal button and when clicked it simply submits the form. It seems to completely ignore the #ajax attribute.
Does anyone know why and how to get this to work?
Thanks in advance,
SanderJP
Comments
=-=
This question is better served in the module development and code questions forum. Please edit the opening post and move it.Thanks.What are actually trying to
What are actually trying to achieve, what is the purpose of the button?
Depending on the purpose, an alternative approach is to use an ajax link (which can be themed to looked like a button).
I need to modify a value in a
I need to modify a value in a custom database table.
Do you mean something like this?
Seems like a lot of work when it's so simple with the button outside the table. I don't understand why it doesn't work inside the table.
The problem here is a
The problem here is a difficult one to see right away.
You are passing everything through an element with #theme table. The problem is that this function will build the data into a table, but it does not call drupal_render() on any of the elements, as the theme is for a table in general, not specifically for a table in a form.
Since drupal_render() is not called on the element, the element does not work properly.
To get around this, you need to create your own theme function that will render the button AND theme the table.
Example:
Contact me to contract me for D7 -> D10/11 migrations.
Thank you very much for the
Thank you very much for the explanation and elaborate example, I will test this tonight.
Thanks, Jaypan - I have just
Thanks, Jaypan - I have just proved this works. Now I only need to figure out how to iterate multiple rows.
Short explanation (correct me
Short explanation (correct me if I am wrong!): AJAX enabled form elements appear invisible to Drupal's Form API when they remain inside a
"#theme" => "table"render array. This is probably due torender()usingelement_children()which does not look into a table's#headeror #rows elements.A way to workaround this is to add dummy elements to the form which are visible to the Form API and bind AJAX events to the original elements to trigger the dummy elements. I shared this workaround at http://stackoverflow.com/a/31098784/328272.
Example of implementing the workaround per AJAX enabled element when building a form:
Laurens Meurs
wiedes.nl
If it works, great. But the
If it works, great. But the example I showed earlier is the 'Drupal way'.
Contact me to contract me for D7 -> D10/11 migrations.
You should use ajax_pre_render_element
The main problem is that the method ajax_pre_render_element is not used
What you can do is something like this:
For me the workaround is ok but is not completed. Is not working as it should.