ajax_command_append 在 Drupal 8 中
ajax_command_append in Drupal 8
我想通过 ajax 单击 link 来显示项目列表。我的 link html 是
<a class="get-list use-ajax ajax-processed" href="get-my-list">My List</a>
我可以通过以下方式在 Drupal 7 中执行此操作:
return array(
'#type' => 'ajax',
'#commands' => array(
ajax_command_append('#my-wrapper', theme('item_list', array('items' => $my_list, 'attributes' => array('class' => array('my-list'))))),
),
);
如何在 Drupal 8 中 return 一个 ajax 回调?
您可能想看看 drupal 8 Ajax API (https://api.drupal.org/api/drupal/core!core.api.php/group/ajax/8)
您可以定义自己的回调函数,或者如果您有一个 link,您可以转到控制器的方法。在这里,您必须定义一个 AjaxResponse 并将命令放入响应中。
这是我项目中的一个示例。
link 积累
$build['ajax-link'] = [
'#title' => '',
'#type' => 'link',
'#id' => 'ajax-link',
'#url' => $url,
'#ajax' => [
'event' => 'click',
'progress' => [
'type' => 'none',
],
],
'#attributes' => [
'class' => [
'fa fa-heart-o fa-2x ' . $activeClass,
],
'title' => 'Ajax heart',
],
];
它调用的控制器方法
$response = new AjaxResponse();
$response->addCommand(new ReplaceCommand('#ajax-link', $this->subscribeElementGenerator->generateSubscribeElement($event)));
return $response;
ReplaceCommand 只是重新生成 link 来更新它。
我想通过 ajax 单击 link 来显示项目列表。我的 link html 是
<a class="get-list use-ajax ajax-processed" href="get-my-list">My List</a>
我可以通过以下方式在 Drupal 7 中执行此操作:
return array(
'#type' => 'ajax',
'#commands' => array(
ajax_command_append('#my-wrapper', theme('item_list', array('items' => $my_list, 'attributes' => array('class' => array('my-list'))))),
),
);
如何在 Drupal 8 中 return 一个 ajax 回调?
您可能想看看 drupal 8 Ajax API (https://api.drupal.org/api/drupal/core!core.api.php/group/ajax/8)
您可以定义自己的回调函数,或者如果您有一个 link,您可以转到控制器的方法。在这里,您必须定义一个 AjaxResponse 并将命令放入响应中。
这是我项目中的一个示例。
link 积累
$build['ajax-link'] = [
'#title' => '',
'#type' => 'link',
'#id' => 'ajax-link',
'#url' => $url,
'#ajax' => [
'event' => 'click',
'progress' => [
'type' => 'none',
],
],
'#attributes' => [
'class' => [
'fa fa-heart-o fa-2x ' . $activeClass,
],
'title' => 'Ajax heart',
],
];
它调用的控制器方法
$response = new AjaxResponse();
$response->addCommand(new ReplaceCommand('#ajax-link', $this->subscribeElementGenerator->generateSubscribeElement($event)));
return $response;
ReplaceCommand 只是重新生成 link 来更新它。