Drupal click event lost after d3js tree collapse

145 Views Asked by At

d3js (both v4 and also v3) charts in Drupal 7 with having click event attached to one of the svg text element using .attr("class", "ajax_button") and attaching Drupal behavior for that element. Problem is that click event is lost once tree gets collapsed and not get reattached on expand.
link : d3js + Drupal behavior
following is code for click element

    (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

There are 0 best solutions below