하나의 #ajax 트리거 요소에 의해 트리거 된 둘 이상의 양식 요소 (래퍼)를 교체 할 수 있습니까?


52
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'];

}

그것은 AJAX 프레임 워크를 알려주는 콜백 함수에 하나 개 이상의 형태의 비트를 반환 할 수 있습니다 $form['element_to_be_replaced_1']교체해야 <div id="replace_div_1">하고 $form['element_to_be_replaced_2']교체해야을 <div id="replace_div_2">?

답변:


73

업데이트 할 단일 요소의 HTML을 반환하는 대신 ajax 콜백은 ajax 명령 배열을 반환 할 수 있습니다 . 따라서 각 요소를 대체하기 위해 두 개의 ajax_command_replace 를 리턴 할 수 있습니다 .

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']))
    )
  );
}

2
이것은 굉장한 것입니다! 많은 시간을 절약했습니다. 대단히 감사합니다 :)
— Sandesh Yadav

현상금이 24 시간 내로 나옵니다.
— AyeshK

콜백 함수의 이름을 변경해야했습니다. ajax_example_simplest_callback (& ​​$ form, $ form_state) {} 함수를 사용하면 흰색 화면이 사라졌습니다.
— Ghoti

5

Drupal 8 대체 구문

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;
   }
}

차이점은 AjaxResponse가 Drupal \ Core \ Render \ AttachmentsInterface를 구현하기 때문에 render 명령이 삭제된다는 것입니다.

렌더링 ($ form [ 'element_to_be_replaced_1'])

렌더 추가는 여전히 작동하지만 TableSelect Table을 업데이트 할 때 문제가 발생했습니다.


작동합니다 감사합니다. 나는 드루팔 문서화 드루팔 (9)에서 사용할 수 실 거예요의 deplicated을 말한다 때문) (렌더링 사용하는 것이 현명하지 않을 것이라고 생각
— Ngatia Frankline

4

Pierre Buyle의 답변이 효과가 없었습니다. 그러나 다음과 같은 것이 효과적이었습니다.

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 명령의 배열을 반환하는 대신 ajax_deliver ()를 호출하십시오 .


2
양식에 #ajax [ 'path']를 사용하고있는 것 같습니다. 이 경우 hook_menu 구현에서 'deliver callback'속성을 사용해야합니다. 이렇게하면 Drupal이 페이지 콜백 결과를 마크 업 대신 AJAX 명령으로 렌더링하도록 지시합니다. 그러나 페이지 콜백에서 직접 사용하면 일반적인 Drupal 페이지 전달을 우회합니다.
— Pierre Buyle
당사 사이트를 사용함과 동시에 당사의 쿠키 정책과 개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.