function ajax_example_simplest($form, &$form_state) {
//This is my ajax trigger element
$form['element_trigger'] = array(
'#type' => 'select',
'#options' => array(
'one' => 'one',
'two' => 'two',
'three' => 'three',
),
'#ajax' => array(
'callback' => 'ajax_example_simplest_callback',
/** Q: Can I somehow declare more than one wrapper? **/
//Say for instance, something like:
'wrapper' => array('replace_div_1', 'replace_div_2'),
),
);
//replace_div_1
$form['element_to_be_replaced_1'] = array(
'#type' => 'textfield',
'#title' => t("My conditional field one"),
'#prefix' => '<div id="replace_div_1">',
'#suffix' => '</div>',
);
//... more form elements here
//replace_div_2
$form['element_to_be_replaced_2'] = array(
'#type' => 'textfield',
'#title' => t("My conditional field two"),
'#prefix' => '<div id="replace_div_2">',
'#suffix' => '</div>',
);
return $form;
}
function ajax_example_simplest_callback($form, $form_state) {
//... do my stuff here
//normally I would return only the form bit for replacing a single wrapper
//declared in the trigger element, like this:
return $form['element_to_be_replaced_blahblah'];
}
Geri arama işlevinde AJAX çerçevesine $form['element_to_be_replaced_1']
yerine <div id="replace_div_1">
ve $form['element_to_be_replaced_2']
yerine <div id="replace_div_2">
?
Güncellemek üzere tek öğenin HTML kodunu döndürmek yerine, ajax geri aramanız ajax komutları dizisini döndürebilir. Böylece her elemanı değiştirmek için iki ajax_command_replace döndürebilir.
function ajax_example_simplest_callback(&$form, $form_state) {
return array(
'#type' => 'ajax',
'#commands' => array(
ajax_command_replace("#replace_div_1", render($form['element_to_be_replaced_1'])),
ajax_command_replace("#replace_div_2", render($form['element_to_be_replaced_2']))
)
);
}
Drupal 8 alternatif sözdizimi
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\ReplaceCommand;
class name extends FormBase{
function ajax_example_simplest(array $form, FormStateInterface &$form_state) {
$response = new AjaxResponse();
$response->addCommand(new ReplaceCommand("#replace_div_1", ($form['element_to_be_replaced_1'])));
$response->addCommand(new ReplaceCommand("#replace_div_2", ($form['element_to_be_replaced_2'])));
return $response;
}
}
Bir fark, render komutunun bırakılmasıdır, çünkü AjaxResponse Drupal\Core \Kılmak\ AttachmentsInterface
kılmak($ Form [ 'element_to_be_replaced_1'])
Render eklemek hala işe yarıyor, ancak bir TableSelect Tablosunu bu şekilde güncellerken sorunlar yaşadım.
Pierre Buyle'nin yanıtı benim için işe yaramadı. Ancak, aşağıdaki gibi bir şey çalıştı.
function ajax_example_simplest_callback(&$form, $form_state) {
$commands = array();
$commands[] = ajax_command_replace("#replace_div_1", render($form['element_to_be_replaced_1']));
$commands[] = ajax_command_replace("#replace_div_2", render($form['element_to_be_replaced_2']));
$page = array('#type' => 'ajax', '#commands' => $commands);
ajax_deliver($page);
}
AJAX komut dizisini döndürmek yerine ajax_deliver () çağrısına dikkat edin.