0

我有两个不同的视图需要在节点页面上切换。我无法将这两个视图放在一起,所以我创建了一个表单,它基本上将采用我创建的表单的值,并将它们作为参数发送到视图并显示视图。我正在尝试使用这个 ajax。它工作正常,但问题是我第二次运行表单时它不会刷新或更新该视图。

    function photoflight_albums() {
        photoflight_gallery_themes();
      $output = render(drupal_get_form('photoflight_gallery_form'));
      $output .= "<div id='gallery-ajax-wrapper'>";
      $output .= views_embed_view('gallery', 'default');
      $output .= "</div>";

      return $output;

    }

    //Grabs the node titles
    function photoflight_gallery_themes(){
        $type = "photo_theme"; 
        $theme_list = array();
        $nodes = node_load_multiple(array(), array('type' => $type)); 
        foreach($nodes as $themes){
            $theme_list[$themes->title] = $themes->title;
        }
        return $theme_list;
    }

//Form calls back to the function above to the gallery-ajax-wrapper div output above
    function photoflight_gallery_form($form, &$form_state){
        $form = array();

      $form['themes'] = array(
        '#type' => 'select',
        '#options' => array(photoflight_gallery_themes()),
        '#ajax' => array(
                'callback' => 'photoflight_simplest_callback',
                'wrapper' => 'gallery-ajax-wrapper',
            ),
      );
      //debug($form);
      return $form;
    }


    //Ajax callback
    function photoflight_simplest_callback($form, $form_state) {
        $view = views_get_view('gallery');
        $args = array( 'title' => $form_state['values']['themes']);
        $view->set_exposed_input($args);
        $output = $view->preview('default', $args);
        return array("#markup" => $output);

    }
4

1 回答 1

0

问题在于它正在删除 HTML 元素,因此第二次调用没有要替换的 HTML。更改为使用 ajax 更新或在回调中再次将返回值包装在 div 中

function photoflight_simplest_callback($form, $form_state) {
    $view = views_get_view('gallery');
    $args = array( 'title' => $form_state['values']['themes']);
    $view->set_exposed_input($args);
    $output = '<div id="gallery-ajax-wrapper">';
    $output .= $view->preview('default', $args);
    $output .= '</div>';
    return array("#markup" => $output);

}
于 2012-11-16T17:13:50.583 回答