1

My question is similar to Implementing a hyperlink within a dojo datagrid, and I'm successfully able to add markup for hyperlinks to a Dojo grid using a formatter.

However, I need to wire up a click events on these hyperlinks, to trigger a function within the Dijit containing the grid.

I have a formatter like the following:

var createEditLinks = function (data) {
   return '<a class="my-css-class" href="#" onclick="myFunctionInsideTheDijit()">' + data.title + '</a>'
}

While this works (I do get the markup inside the grid cell), the myFunctionInsideTheDijit function is unavailable (unless I would declare it on the global scope).

I've looked a little at dom-construct, but I just can't figure out how to add a hyperlink that invokes a Dijit function when clicked.

Any help is greatly appreciated! Thanks!

Community
  • 1
  • 1
Ted Nyberg
  • 7,001
  • 7
  • 41
  • 72

2 Answers2

2

A more modern way to do this than with dojo.behavior would be to use on and event delegation. dgrid instances already expose their own on function to make this slightly easier:

grid.on('a.my-css-class:click', function (event) {
    ...
});
Ken Franqueiro
  • 10,559
  • 2
  • 23
  • 40
  • Fantastic, works like a charm! Thanks a lot for your help! – Ted Nyberg Mar 17 '16 at 13:09
  • Only weird thing is that 'event.target' becomes the inner element of the anchor tag, not the anchor element itself. – Ted Nyberg Mar 17 '16 at 13:19
  • That's expected; `event.target` will be the innermost element that fired the event. `this` will be the element matching the selector. This is explained in the reference guide page I linked to. – Ken Franqueiro Mar 18 '16 at 00:59
0

I resorted to using dojo/behavior to make it work:

// Code inside Dijit's startup function
var that = this;

behavior.add({
    "a.my-css-class": {
        onclick: function (e) {
            e.preventDefault();

            that.myFunctionInsideTheDijit();
        }
    }
});

behavior.apply();

Not sure if there's a more elegant way of doing this? :)

Ted Nyberg
  • 7,001
  • 7
  • 41
  • 72