【问题标题】:ajax_command_append in Drupal 8Drupal 8 中的 ajax_command_append
【发布时间】:2016-04-17 04:27:05
【问题描述】:

我想在链接点击时通过 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 回调?

【问题讨论】:

    标签: drupal drupal-8


    【解决方案1】:

    您可能想看看 drupal 8 Ajax API (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 只是重新生成链接以更新它。

    【讨论】:

      猜你喜欢
      • 2017-04-29
      • 1970-01-01
      • 2016-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多