2012-06-28 56 views
2

當用戶點擊一個按鈕(帶有id圖)時,我想用默認實例填充默認的Drupal內容div<div class="content">)。如何在AJAX請求期間「打印」一個主題(Drupal)

中的JavaScript:

jQuery(document).ready(function($) { 

    $('#toggle #graph').click(function() { 


    $.ajax({ 
     url: "http://www.mysite.com/?q=publications/callback", 
     type: 'POST', 
     data: { 
     'format' : 'graph' 
     }, 
     success: function(response) { 
     $('div#content div.content').html(response); 
     } 
    }); 
    }); 
}); 

的PHP:

$items['publications/callback'] = array(
    'type' => MENU_CALLBACK, 
    'title' => 'All Publications Callback', 
    'page callback' => '_process_publications', 
    'page arguments' => array(t('journal')), 
    'access callback' => TRUE, 
); 

這導致了頁面的回調:(我關心if代碼塊)

function _process_publications($venue) { 

if(isset($_POST['format']) && $_POST['format'] == "graph"){ 
    _make_bar_chart($venue); 
} 
elseif(isset($_POST['format']) && $_POST['format'] == "list") { 
    _make_list($venue); 
} 
else{ 
    return("<p>blah</p>"); 
} 

} 

和最後在回調函數中調用的函數:

function _make_bar_chart($venue) { 

// get active database connection 
$mysql = Database::getConnection(); 

// if connection is successful, proceed 
if($mysql){ 
     // do stuff 


     $graphael = array(
      'method' => 'bar', 
      'values' => $ycoordinates, 

      'params' => array(
       'colors' => $colors, 
       'font' => '10px Arial, sans-serif', 
       'opts' => array(
       'gutter' => '20%', 
       'type' => 'square', 
      ), 
       'label' => array(
       'values' => $xcoordinates, 
       'isBottom' => true, 
      ), 
      ), 

      'extend' => array(
       'label' => array(
       'values' => $ycoordinates, 
       'params' => array('attrText' => array(
        'fill' => '#aaa', 
        'font' => '10px Arial, sans-serif', 
       )), 
      ), 
      ), 
     ); 

    return theme('graphael', $graphael); 

} 

// else, connection was unsuccessful 
else{ 
    print("<p>bad connection</p>"); 
} 

} 

問題是:返回的主題並沒有真正發送任何回AJAX請求(不像print語句)。我試圖打印主題,但這會產生死亡的白色屏幕。我將如何生成圖形而不打印某些內容?

+0

你試過drupal_json_output()嗎? –

+0

不,我沒有,但我現在試試 – Don

+0

這隻輸出JSON格式的'$ graphael'參數;它實際上並不生成圖 – Don

回答

1

很大程度上得益於nevets在Drupal的論壇爲有用的提示:http://drupal.org/node/1664798#comment-6177944

如果你想使用AJAX使用Drupal,你最好離實際使用Drupal的特定AJAX相關的功能。在我的主題的page.tpl.php文件,添加以下,使這將調用AJAX鏈接:

<?php 
    // drupal_add_library is invoked automatically when a form element has the 
    // '#ajax' property, but since we are not rendering a form here, we have to 
    // do it ourselves. 
    drupal_add_library('system', 'drupal.ajax'); 


     // The use-ajax class is special, so that the link will call without causing 
     // a page reload. Note the /nojs portion of the path - if javascript is 
     // enabled, this part will be stripped from the path before it is called. 
     $link1 = l(t('Graph'), 'ajax_link_callback/graph/nojs/', array('attributes' => array('class' => array('use-ajax')))); 
     $link2 = l(t('List'), 'ajax_link_callback/list/nojs/', array('attributes' => array('class' => array('use-ajax')))); 
     $link3 = l(t('Create Alert'), 'ajax_link_callback/alert/nojs/', array('attributes' => array('class' => array('use-ajax')))); 

     $output = "<span>$link1</span><span>$link2</span><span>$link3</span><div id='myDiv'></div>"; 
     print $output; 

?> 

當點擊上面的鏈接之一,回調函數被調用(如ajax_link_callback /圖):

// A menu callback is required when using ajax outside of the Form API. 
    $items['ajax_link_callback/graph'] = array(
    'page callback' => 'ajax_link_response_graph', 
    'access callback' => 'user_access', 
    'access arguments' => array('access content'), 
    'type' => MENU_CALLBACK, 
); 

..而且因爲它是指回調:

function ajax_link_response_graph($type = 'ajax') { 
    if ($type == 'ajax') { 
    $output = _make_bar_chart('journal'); 
    $commands = array(); 
    // See ajax_example_advanced.inc for more details on the available commands 
    // and how to use them. 
    $commands[] = ajax_command_html('div#content div.content', $output); 
    $page = array('#type' => 'ajax', '#commands' => $commands); 
    ajax_deliver($page); 
    } 
else { 
    $output = t("This is some content delivered via a page load."); 
    return $output; 
} 
} 

這將替換內<div class="content">任何HTML與個graphael圖表從_make_bar_chart上面返回。