[ 9 posts ]

pragnesh

  • Joined: Mon May 30, 2011 10:55 pm
  • Posts: 1
  • Offline
  • Profile

how to know what is clicked in cellClickEvent in DataTable

Post Posted: Mon May 30, 2011 11:06 pm
+0-
I want to know which element is clicked within cell in DataTable inside cellClickEvent.

let's say Cell has two element A and B and B is link.
Want to open CellEditor if element is A and follow link if element is B

Is there any way achieve this functionality?

Alberto Santini

YUI Contributor

  • Offline
  • Profile
Tags:

Re: how to know what is clicked in cellClickEvent in DataTab

Post Posted: Tue May 31, 2011 12:24 am
+0-
Hello pragnesh.

I suppose you should inspect the cell content.

See, for instance, the topic "Hide or disable a cellEditor" as starting point:
viewtopic.php?p=19228

myCell = record.getData('someMandatoryField');
// and now you can inspect myCell content.

Hope that helps,
IceBox

Dave McDaniel

  • Username: dam5h
  • Joined: Mon Aug 30, 2010 6:53 am
  • Posts: 50
  • Offline
  • Profile
Tags:

Re: how to know what is clicked in cellClickEvent in DataTab

Post Posted: Thu Jun 02, 2011 8:56 am
+0-
I check the column key to see what column was clicked as such:
Code:
dT.subscribe('cellClickEvent',function(oArgs) {
var target = oArgs.target,
column = this.getColumn(target);
if (column.key == "key-1"){
  alert("key-1 column was clicked");
}
}

Todd Smith

YUI Contributor

  • Username: stlsmiths
  • Joined: Thu Nov 05, 2009 10:03 am
  • Posts: 675
  • GitHub: stlsmiths
  • Gists: stlsmiths
  • IRC: t_smith
  • Offline
  • Profile

Re: how to know what is clicked in cellClickEvent in DataTab

Post Posted: Thu Jun 02, 2011 9:50 am
+0-
I think pragnesh was referring to each individual cell (column) containing more complex data, such as from a custom formatter where the contents might be text and/or a link, button or other DOM element in addition to the basic data.

If that is the case, it is very difficult to do using DataTable methods because the most detailed level that DT supports is "cell". You can't use DT methods to track events (click, mouseover, etc..) to internal elements. See a similar thread of http://yuilibrary.com/forum/viewtopic.php?f=90&t=7887.

Pragnesh, if this is what you are looking to do, you may need to use Event Delegation (see http://developer.yahoo.com/yui/examples/event/event-delegation.html) on the internal DOM elements of the cells.

... let us know if this is what you are looking for, okay?

Todd

Dave McDaniel

  • Username: dam5h
  • Joined: Mon Aug 30, 2010 6:53 am
  • Posts: 50
  • Offline
  • Profile

Re: how to know what is clicked in cellClickEvent in DataTab

Post Posted: Thu Jun 02, 2011 9:58 am
+0-
Good point Todd, I was a bit hasty in reading that. My code above is of little help I'm sure.

Perhaps you make the first item "A" a link of button formatted with css to your liking appearance wise and then have an onclick method to load the dialog box.

Or even use

Code:
clickXY = YAHOO.util.Event.getXY(oArgs.event)


to get the XY position of the click, which could then be compared to the length of "A", either a constant or defined in the responseSchema as a variable.

Todd Smith

YUI Contributor

  • Username: stlsmiths
  • Joined: Thu Nov 05, 2009 10:03 am
  • Posts: 675
  • GitHub: stlsmiths
  • Gists: stlsmiths
  • IRC: t_smith
  • Offline
  • Profile

Re: how to know what is clicked in cellClickEvent in DataTab

Post Posted: Thu Jun 02, 2011 10:25 am
+0-
Dave,

Sure you can define onclick or Event click handlers all you want to to cells of a DataTable. But the problem is this is a major source of memory leaks and problems. If you have a DT with 500 rows in it, and have a formatter that defines click handlers in a formatter ... that is adding potentially a thousand click handlers. If the user edits or sorts a column, the formatter gets re-called again!, so you can duplicate event handlers ... it it becomes mayhem.

That is one of the reasons Event Delegation is good. Let's say your DT is built upon <div id="idTable>. And let's say your custom formatter addes <a> elements in a column on each record. You can use the following;
Code:
YAHOO.util.Event.delegate( "idTable", "click",  myLinkHandler, "a");
to define one click handler which is far superior to having thousands (from a performance perspective). The problem is, finding the "id" or Column and Record position of that link in the DataTable, since it is no longer available.

Regards,
Todd

Dave McDaniel

  • Username: dam5h
  • Joined: Mon Aug 30, 2010 6:53 am
  • Posts: 50
  • Offline
  • Profile

Re: how to know what is clicked in cellClickEvent in DataTab

Post Posted: Thu Jun 02, 2011 11:44 am
+0-
Interesting, thanks for the tip.

Leads to another question, why is it "no longer available"; is that since it a general method applied to all links?

Also if the XY method is only being called within a cell click event and after the column filtering method I very first posted, does that still represent a memory issue?

Thanks,
Dave

Todd Smith

YUI Contributor

  • Username: stlsmiths
  • Joined: Thu Nov 05, 2009 10:03 am
  • Posts: 675
  • GitHub: stlsmiths
  • Gists: stlsmiths
  • IRC: t_smith
  • Offline
  • Profile

Re: how to know what is clicked in cellClickEvent in DataTab

Post Posted: Thu Jun 02, 2011 11:56 am
+0-
Hey Dave,

As for the XY method, I don't know how that would work in a DataTable? Won't the .getXY just return the absolute or offset screen coordinates of the click ... how can that be translated into a specific row/column ??

The memory leak issue happen when Listeners are added and not fully purged and/or removed completely. If you put listeners in a DataTable column formatter you run the risk of them not properly being removed prior to being re-created on the same DOM element on a formatter re-call (sort, edit, insert, etc...).

As for the "no longer available" comment, if you define a Delegate listener on an element of DataTable as I listed, you potentially eliminate the issues in the prior paragraph (listeners and memory leaks). The problem is the listener is not a specific method of DataTable, and therefore has no way of knowing what row/column pair the target is. The target now is just a DOM element that "happens to be" in a DataTable.

One way I have thought of getting around this is to have a global variable (or namespace protected variable) outside of DataTable that tracks current_row, current_column based on "cellMouseOverEvent". Then in the delegated "myLinkHandler" you can now use the last current_row / current_column pair as a reasonable owner of the target. Haven't tried it, just a thought, (I don't do these types of complex formatters with multiple DOM members ...)

Todd

Dave McDaniel

  • Username: dam5h
  • Joined: Mon Aug 30, 2010 6:53 am
  • Posts: 50
  • Offline
  • Profile

Re: how to know what is clicked in cellClickEvent in DataTab

Post Posted: Thu Jun 02, 2011 12:07 pm
+0-
That makes sense regarding sticking things inside of formatters, since they get called so often.

I'm not familiar with delegates but this is helpful info.

I currently use the XY click location method to do a "star" rating column on the table. First I get the cellXY and then calculate the difference to get the position within the cell.

Code:
var cellXY = YAHOO.util.Dom.getXY(target),
clickXY = YAHOO.util.Event.getXY(oArgs.event);


It comes from this forum post last fall:

viewtopic.php?f=90&t=5386
  [ 9 posts ]
Display posts from previous:  Sort by  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum