0

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);
kiranking
  • 306
  • 11
  • 29
  • This behavior doesn't get reattached on expand ? Make sure that it's the case using for ex. console.log('listload') at the very beginning of the function, before this line if (context == document). By the way why restraining the context to the document ? – EricLavault Feb 03 '18 at 17:39
  • yes. it is not attaching when expanded. and for context I thought it will prevent multiple ajax call made on clicking of .ajax_button element (more detailed info I posted at https://www.drupal.org/forum/support/module-development-and-code-questions/2011-08-21/every-events-fired-multiple-times#comment-12280889 ) . Events are lost only for child nodes of d3js and you can see it live at http://firststateindia.org/d3v4-test-wrapper – kiranking Feb 06 '18 at 09:41

0 Answers0