d3js树崩溃后,Drupal click事件丢失

时间:2018-01-25 09:22:23

标签: d3.js svg drupal jquery-events drupal-behaviors

Drupal 7中的

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

    (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);

0 个答案:

没有答案