[gelöst] Kann man in einem Formular über #ajax auch mehrere Elemente triggern?
am 23.02.2015 - 07:26 Uhr in
Hallo,
ich mabe ein SELECT- Element und möchte mehr als ein anderes Element bei Änderungen in diesem Feld bearbeiten und ändern. Geht das?
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' => '
'#suffix' => '
',
);
//... more form elements here
//replace_div_2
$form['element_to_be_replaced_2'] = array(
'#type' => 'textfield',
'#title' => t("My conditional field two"),
'#prefix' => '
'#suffix' => '
',
);
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'];
}
- Anmelden oder Registrieren um Kommentare zu schreiben

ja, geht
am 23.02.2015 - 09:06 Uhr
function callback_function($form, &$form_state){
$commands = array();
$commands[] = ajax_command_replace('#element1', drupal_render($form[any form element]));
$commands[] = ajax_command_replace("#element2", drupal_render($form[any form element]));
return array('#type' => 'ajax', '#commands' => $commands);
}
Vielen Dank für die schnelle
am 23.02.2015 - 09:44 Uhr
Vielen Dank für die schnelle Antwort.
Diese Lösung kenne ich schon. Hat aber bei mir nicht funktioniert. Es wird doch nur von ajax das aktualisiert, was in #wrapper des zu ändernden Elements steht und das ist:
. In den #wrapper kann ich aber nur eine id eintragen, also auch nur ein Element ändern. Wie kann ich das zweite Element auch bearbeiten?
function ajax_command_replace
am 23.02.2015 - 10:31 Uhr
function ajax_command_replace fügt ein / ersetzt mittels jquery der replaceWith() Methode. Wenn ich mir das in der Erklärung (auch in den Kommentaren) durchlese, müsste es das aber sein!???
https://api.drupal.org/api/drupal/includes!ajax.inc/function/ajax_command_replace/7
https://api.drupal.org/api/drupal/includes!ajax.inc/function/ajax_command_append/7#comment-39488
mein Quellcode
am 23.02.2015 - 12:04 Uhr
Irgendwie kann ich den Fehler nicht sehen. So sieht der relevante Quellcode bei mir aus.
Wenn ich das richtig verstehe, kann man mit dem wrapper bei ajax die ID des DIV angeben, deren Inhalt man verändern will.
<?php
function order_produktauswahl_form($form, &$form_state) {
$form['gif-banner']['gif-anzahl'] = array(
'#type' => 'select',
'#title' => t('Anzahl'),
'#prefix' => "
'#suffix' => "
",
'#options' => array(
0 => 0,1 => 1,2 => 2,3 => 3,4 => 4,5 => 5,6 => 6,7 => 7,8 => 8,9 => 9,
10 => 10,15 => 15,
20 => 20,25 => 25,
30 => 30,35 => 35,
40 => 40,45 => 45,
50 => 50,60 => 60,70 => 70,80 => 80,90 => 90,100 => 100,
),
'#ajax' => array(
'event' => 'change',
'callback' => 'calc_preis_gif_ajax_callback',
'wrapper' => 'gif-gesamtpreis',
'method' => 'replace',
'effect' => 'fade',
),
);
$form['gif-banner']['gif-gesamtpreis'] = array(
'#title' => t('Gesamtpreis'),
'#type' => 'textfield',
'#size'=> 10,
'#attributes' => array(
'readonly' => 'readonly',
),
'#prefix' => "
'#suffix' => "
",
'#default_value' => isset($values['gif-gesamtpreis']) ? $values['gif-gesamtpreis'] :'0,00 €',
);
return $form;
}
function calc_preis_gif_ajax_callback($form, $form_state) {
$form['gif-banner']['gif-gesamtpreis']['#value'] = 'super Preis';
// das funktioniert nicht
$commands = array();
$commands[] = ajax_command_replace('gif-gesamtpreis', drupal_render($form['gif-banner']['gif-gesamtpreis']));
return array('#type' => 'ajax', '#commands' => $commands);
// das funktioniert
// return $form['gif-banner']['gif-gesamtpreis'];
};
Ist gif-gesamtpreis ein
am 23.02.2015 - 12:11 Uhr
Ist gif-gesamtpreis ein selector??? Also Klasse oder ID? Falls ja fehlt dort ein punkt oder #.
ja, habe ich mit #prefix erstellt
am 23.02.2015 - 12:23 Uhr
<?php
function order_produktauswahl_form($form, &$form_state) {
$form['gif-banner']['gif-anzahl'] = array(
'#type' => 'select',
'#title' => t('Anzahl'),
'#prefix' => "<div id='gif-anzahl'>",
'#suffix' => "</div>",
'#options' => array(
0 => 0,1 => 1,2 => 2,3 => 3,4 => 4,5 => 5,6 => 6,7 => 7,8 => 8,9 => 9,
10 => 10,15 => 15,
20 => 20,25 => 25,
30 => 30,35 => 35,
40 => 40,45 => 45,
50 => 50,60 => 60,70 => 70,80 => 80,90 => 90,100 => 100,
),
'#ajax' => array(
'event' => 'change',
'callback' => 'calc_preis_gif_ajax_callback',
'wrapper' => 'gif-gesamtpreis',
'method' => 'replace',
'effect' => 'fade',
),
);
$form['gif-banner']['gif-gesamtpreis'] = array(
'#title' => t('Gesamtpreis'),
'#type' => 'textfield',
'#size'=> 10,
'#attributes' => array(
'readonly' => 'readonly',
),
'#prefix' => "<div id='gif-gesamtpreis'>",
'#suffix' => "</div>",
'#default_value' => isset($values['gif-gesamtpreis']) ? $values['gif-gesamtpreis'] :'0,00 €',
);
return $form;
}
function calc_preis_gif_ajax_callback($form, $form_state) {
$form['gif-banner']['gif-gesamtpreis']['#value'] = 'super Preis';
// das funktioniert nicht
$commands = array();
$commands[] = ajax_command_replace('gif-gesamtpreis', drupal_render($form['gif-banner']['gif-gesamtpreis']));
return array('#type' => 'ajax', '#commands' => $commands);
// das funktioniert
// return $form['gif-banner']['gif-gesamtpreis'];
};
?>
Also fehlt, wie schon von
am 23.02.2015 - 12:34 Uhr
Also fehlt, wie schon von meinem Vorredner angemerkt, der Lattenzaum (#) um die ID zu markieren in ajax_command_replace. Es muß also dort #gif-gesamtpreis heißen. Im übrigen ist das div Statement falsch. Eine Klasse oder ID gehört in " und nicht in ' geklammert.
wla schrieb Also fehlt, wie
am 23.02.2015 - 12:36 Uhr
Also fehlt, wie schon von meinem Vorredner angemerkt, der Lattenzaum (#) um die ID zu markieren in ajax_command_replace. Es muß also dort #gif-gesamtpreis heißen. Im übrigen ist das div Statement falsch. Eine Klasse oder ID gehört in " und nicht in ' geklammert.
Hi Werner,
ich glaube das ist egal!
wenn ich euch nicht hätte
am 23.02.2015 - 13:01 Uhr
Habe den # nicht gesetzt, daran lag es.
Ich komme jetzt auch ohen 'wrapper' => 'gif-gesamtpreis', im ajax aus.
Vielen Dank!!!