0

私は Drupal 7 に取り組んでいます。フォームはこのように構築されています。

function qt_debate_response_form($form, &$form_state, $node_id){
$form['node_id'] = array(
    '#type' => 'value',
    '#value' => $node_id,
);
$form['response_body'] = array(
    '#type' => 'textarea',
    '#required' => TRUE,
    '#row' => 4,
    '#default_value' => '',
);
$form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Post'),
    '#ajax' => array(
      'callback' => 'qt_debate_response_form_js',
      'wrapper' => 'response-message-' . $node_id,
      'method' => 'append',
      'effect' => 'fade',
    ),
);
return $form;
}

そして、新しいコメントを追加する ajax コールバック関数

function qt_debate_response_form_js($form, $form_state) {
global $user;
$body_text = $form_state['values']['response_body'];
$node_id = $form_state['values']['node_id'];

$message_js = '
<script language="javascript" type="text/javascript">
    qt_debate_response_load_new_item(' . $node_id . ',' . $user->uid . ');

    jQuery(".response-form-wrapper textarea").val("");
</script>';


$comment = new stdClass();
$comment->nid = $form_state['values']['node_id']; // Node Id the comment will attached to
$comment->cid = 0;
$comment->pid = 0;
$comment->uid = $user->uid;
$comment->is_anonymous = 0;
$comment->homepage = '';
$comment->status = COMMENT_PUBLISHED;
$comment->language = LANGUAGE_NONE;
$comment->subject = text_summary($body_text, null, 60);
$comment->comment_body[$comment->language][0]['value'] = $body_text;
$comment->comment_body[$comment->language][0]['format'] = 'filtered_html'; 
comment_submit($comment);
comment_save($comment);

$output = $message_js;

return $output;
}

ここに、新しく作成されたコメントを Div (ajax) にロードする私の Javascript があります。

function qt_debate_user_post_load_new_items(debate_id) {
// get the latest comment id in context
$top_comment = jQuery(".view-debate-user-posts .views-row").first();
$top_comment_id = jQuery(".nid-field-hidden", $top_comment).html();

jQuery.ajax({
    type: "GET",
    url: "/qt_debate/ajax/load_new_items/" + debate_id + "/" + $top_comment_id,
    data: "",
    success: function(html){
        $new_items = jQuery(".view-content", html);
        jQuery("form", $new_items).attr("action","/debate/199");
        jQuery(".form-submit", $new_items).attr("id","edit-submit--5");

        if ($new_items.html() != null) {
            html = '<div class="new_items_wrapper" style="display: none">' + $new_items.html() + '</div>';
            if (jQuery(".view-debate-user-posts .view-content").length == 0) {
                jQuery(".view-debate-user-posts .view-empty").remove();
                jQuery(".view-debate-user-posts").append('<div class="view-content"></div>');
            }

            jQuery(".view-debate-user-posts .view-content").prepend(html);

            jQuery(".view-debate-user-posts .view-content .new_items_wrapper").fadeIn(500, function() {
                jQuery(".views-row", this).unwrap();
                Drupal.attachBehaviors();
            });
        }
    },
});

var t = setTimeout("qt_debate_user_post_load_new_items(" + debate_id + ")", 30000)
}

ビューのコンテンツを jQuery コールバックに返す hook_menu

function qt_debate_ajax_load_new_items() {
$debate_id = arg(3);

print views_embed_view('debate_user_posts_load_new_items_', 'default', array($debate_id));
exit(0);
}

テンプレートファイルを表示し、内部に新しいフォームも返します

print drupal_render(drupal_get_form('qt_debate_response_form', $row->nid));

返されたビュー コンテンツは、Javascript の Drupal.attachBehaviors で適切にレンダリングされ、返されたビュー コンテンツの他のすべての効果も適切に機能します。フォーム送信 ajax を除いて。

誰でも助けてくれますか?attachBehaviors は return ajax フォームでは機能しません。

本当にありがとう!

4

1 に答える 1

1
Drupal.attachBehaviors(context);

基本的に、によって定義された関数を再実行します

Drupal.behaviors.yourFunctionName = function(context) {
    $('div.someSelectorclass:not(.already-processed-class)', context).addClass('already-processed-class').bind(someMethod);
}

これらのメソッドは、bind();かどうかをテストするためにセレクター[already-processed-class]を追加する必要があります。[またはclick(function(e){}); またはeach(function(){}); または]がすでに追加されています。「コンテキスト」は、「ドキュメント」未満を渡すことです。たとえば、新しいコンテンツが、元の動作関数によって引き続き検出される小さなコンテキスト内にあることがわかっている場合、この例では、私の親コンテナセレクターを渡すことができます。新しい'div.someSelectorclass'

Drupal.attachBehaviors('div.parentContainerClass');

それ以外の

Drupal.attachBehaviors(document);
于 2012-04-15T19:19:05.130 に答える