我想通过ajax在链接上显示一个项目列表。我的链接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中返回这样的ajax回调?
发布于 2016-04-18 08:31:08
您可能想看看Drupal8AjaxAPI (https://api.drupal.org/api/drupal/core!core.api.php/group/ajax/8)
您可以定义自己的回调函数,或者如果有链接,则可以转到控制器的方法。在这里,您必须定义一个AjaxResponse并在响应中放置命令。
下面是我的项目的一个例子。
链路建立
$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只是重新生成链接来更新它。
https://stackoverflow.com/questions/36672671
复制相似问题