0

Drupal 7 中的 d3js(v4 和 v3)图表,点击事件附加到 svg 文本元素之一,使用.attr("class", "ajax_button")并附加该元素的 Drupal 行为。问题是一旦树被折叠并且不会在展开时重新附加,点击事件就会丢失。
链接:d3js + Drupal 行为
以下是点击元素的代码

    (function($) {

    Drupal.behaviors.listload = {

        attach: function(context, settings) {

            if (context == document) {
                $('.ajax_button', context).once(function() {
                    $(this).click(function(e) {
                        e.preventDefault();
                        // https://stackoverflow.com/a/1369080
                        // to prevent collapsible function which is attached to parent element
                        e.stopPropagation();
                        var the_id = $(this).attr('href'); // contextual filter(nid) to views
                        noSlashes = the_id.replace(/\//g, '');

                        $.ajax({
                            url: Drupal.settings.basePath + 'views/ajax',
                            type: 'post',
                            data: {
                                view_name: 'candidate_list_er', //view name
                                view_display_id: 'candidate_list_block', //your display id
                                view_args: noSlashes, // your views arguments, //view contexuall filter
                            },
                            dataType: 'json',
                            success: function(response) {
                                //console.log(response);
                                if (response[1] !== undefined) {
                                    //console.log(response[1].data); // do something with the view
                                    var viewHtml = response[1].data;
                                    $('#ajax-target .content').html(viewHtml);
                                    //Drupal.attachBehaviors(); //check if you need this.
                                }
                            },
                            error: function(data) {
                                alert('An error occured!');
                            }
                        });

                    }); //click ends
                }); //once ends
            }
        },
        detach: function(context, settings, trigger) { //this function is option
        $('.ajax_button').unbind(); //or do whatever you want;
        }
    };

})(jQuery);
4

0 回答 0