I''m writing some code to subset a table based on the contents of particular columns. My event handler starts off like this: subsetChange: function(e) { var subset = $F(''subsetSelector''); var rows = $$(''.content-row''); rows.each(function(row) { var value = row.down(''.Platform_Category'').childNodes[0].data; switch (subset) { ... show/hide rows based on whether the value matches the user selected subset ... My question is whether there is a more Prototype-ish way to get the values from my table cells instead of using ".childNodes[0].data". Thanks in advance for any advice. -- Jon --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Why not just do $$(''.content-row .Platform_Category'')? Also, a safer alternative to childNodes[0] is #firstDescendant or #down - kangax On May 19, 5:25 pm, Jonathan_C <Jonathan.S.Calla...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m writing some code to subset a table based on the contents of > particular columns. My event handler starts off like this: > > subsetChange: function(e) { > var subset = $F(''subsetSelector''); > var rows = $$(''.content-row''); > rows.each(function(row) { > var value = row.down(''.Platform_Category'').childNodes[0].data; > switch (subset) { > > ... show/hide rows based on whether the value matches the user > selected subset ... > > My question is whether there is a more Prototype-ish way to get the > values from my table cells instead of using ".childNodes[0].data". > > Thanks in advance for any advice. > > -- Jon--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
I collect an array of rows because I want to do row.show()/row.hide() later on in the code. I thought the code would look cleaner this way as oppose to doing cell.up(''tr'').show()/hide(). And I tried using #down() and #firstDescendant() instead of #childNodes[0] but I always get a "...has no properties" message whenever I do. I''m running Firefox 2.0 on Ubuntu. -- Jon On May 19, 3:45 pm, kangax <kan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Why not just do $$(''.content-row .Platform_Category'')? > Also, a safer alternative to childNodes[0] is #firstDescendant or > #down > > - kangax > > On May 19, 5:25 pm, Jonathan_C <Jonathan.S.Calla...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I''m writing some code to subset a table based on the contents of > > particular columns. My event handler starts off like this: > > > subsetChange: function(e) { > > var subset = $F(''subsetSelector''); > > var rows = $$(''.content-row''); > > rows.each(function(row) { > > var value = row.down(''.Platform_Category'').childNodes[0].data; > > switch (subset) { > > > ... show/hide rows based on whether the value matches the user > > selected subset ... > > > My question is whether there is a more Prototype-ish way to get the > > values from my table cells instead of using ".childNodes[0].data". > > > Thanks in advance for any advice. > > > -- Jon--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
On May 20, 7:25 am, Jonathan_C <Jonathan.S.Calla...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m writing some code to subset a table based on the contents of > particular columns. My event handler starts off like this: > > subsetChange: function(e) { > var subset = $F(''subsetSelector''); > var rows = $$(''.content-row'');rows isn''ta good variable name as table and table section elements have a rows collection, so may be confusing.> rows.each(function(row) { > var value = row.down(''.Platform_Category'').childNodes[0].data; > switch (subset) { > > ... show/hide rows based on whether the value matches the user > selected subset ... > > My question is whether there is a more Prototype-ish way to get the > values from my table cells instead of using ".childNodes[0].data".Why not use either DOM 3 textContent or IE innerText? e.g. function getText (el) { if (typeof el.textContent == ''string'') return el.textContent; if (typeof el.innerText == ''string'') return el.innerText; } Or if you want to be "Prototypeis": Element.addMethods({ getText: function(element) { element = $(element); if (typeof element.textContent == ''string'') { return element.textContent; } if (typeof element.innerText == ''string'') { return element.innerText; } } }); Table rows have a cells collection that contains all the cells in the row, so if you want the content of the first cell: var value = rows[i].cells[0].getText(); -- Rob --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Sometimes I hate how in order to make things efficient (?), the code size becomes really huge: var getText = (function(){ var type = document.createElement(''div''); if (typeof element.textContent != ''undefined'') { return function(element) { return element.textContent; } } else if (typeof element.innerText != ''undefined'') { return function(element) { return element.innerText; } } return function(){}; })(); - kangax On May 20, 7:17 pm, RobG <rg...-AFFH1GffN5hPR4JQBCEnsQ@public.gmane.org> wrote:> On May 20, 7:25 am, Jonathan_C <Jonathan.S.Calla...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I''m writing some code to subset a table based on the contents of > > particular columns. My event handler starts off like this: > > > subsetChange: function(e) { > > var subset = $F(''subsetSelector''); > > var rows = $$(''.content-row''); > > rows isn''ta good variable name as table and table section elements > have a rows collection, so may be confusing. > > > rows.each(function(row) { > > var value = row.down(''.Platform_Category'').childNodes[0].data; > > switch (subset) { > > > ... show/hide rows based on whether the value matches the user > > selected subset ... > > > My question is whether there is a more Prototype-ish way to get the > > values from my table cells instead of using ".childNodes[0].data". > > Why not use either DOM 3 textContent or IE innerText? e.g. > > function getText (el) { > if (typeof el.textContent == ''string'') return el.textContent; > if (typeof el.innerText == ''string'') return el.innerText; > } > > Or if you want to be "Prototypeis": > > Element.addMethods({ > getText: function(element) { > element = $(element); > if (typeof element.textContent == ''string'') { > return element.textContent; > } > if (typeof element.innerText == ''string'') { > return element.innerText; > } > } > > }); > > Table rows have a cells collection that contains all the cells in the > row, so if you want the content of the first cell: > > var value = rows[i].cells[0].getText(); > > -- > Rob--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
On May 21, 10:45 am, kangax <kan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Sometimes I hate how in order to make things efficient (?), the code > size becomes really huge: > > var getText = (function(){ > var type = document.createElement(''div'');I think a more appropriate name for that variables is "temp" or maybe just t or x or whatever.> if (typeof element.textContent != ''undefined'') {I think you meant: if (typeof type.textContent != ''undefined'') { I don''t see the point of a comparison with ''undefined'' given that we know we want a string and only a string (maybe some implementation decides to return ''object'' or ''function'' or ''null'' or such weirdness). The above is no better than: if (type.textContent) { Some would argue that the strict equals operator (=== ) should be used. So: var t = document.createElement(''div''); if (typeof t.textContent === ''string'') {> return function(element) { > return element.textContent; > } > } > else if (typeof element.innerText != ''undefined'') { > return function(element) { > return element.innerText; > } > } > return function(){}; > })();Considering the result is more complex code, the benefit of such optimisation must be weighed against the loss of simplicity. I expect that before the above becomes noticeably faster the function will have to be run several thousand times in succession. That is unlikely to occur so very little (if anything) will be gained by such optimisation. I''d rather stick with simplicity. If the intention is to write a more general function, then the above should be extended to include a recursive function where neither textContent or innerText are supported, e.g.: Element.addMethods({ getText: (function() { var t = document.createElement(''div''); if (typeof t.textContent == ''string'') { return function(element) {return element.textContent;} } if (typeof t.innerText == ''string'') { return function(element) {return element.innerText;} } return function(element) { return rec(element); function rec(el) { var n, x = el.childNodes; var txt = []; for (var i=0, len=x.length; i<len; ++i){ n = x[i]; if (3 == n.nodeType) { txt.push(n.data); } else if (1 == n.nodeType){ txt.push(rec(n)); } } return txt.join('''').replace(/\s+/g,'' ''); } } })() }); However in the case of Prototype.js, any browser that doesn''t support either textContent or innerText is likely going to have lots of other issues. -- Rob --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
On May 20, 10:02 pm, RobG <rg...-AFFH1GffN5hPR4JQBCEnsQ@public.gmane.org> wrote:> On May 21, 10:45 am, kangax <kan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Sometimes I hate how in order to make things efficient (?), the code > > size becomes really huge: > > > var getText = (function(){ > > var type = document.createElement(''div''); > > I think a more appropriate name for that variables is "temp" or maybe > just t or x or whatever. >Not sure how "type" slipped in there, but it was originally an "element" of course : ) (need to be more careful next time)> > if (typeof element.textContent != ''undefined'') { > > I think you meant: > > if (typeof type.textContent != ''undefined'') { > > I don''t see the point of a comparison with ''undefined'' given that we > know we want a string and only a string (maybe some implementation > decides to return ''object'' or ''function'' or ''null'' or such > weirdness). The above is no better than: > > if (type.textContent) { >I didn''t want to enforce certain result type (i.e. string) therefore test for not "undefined". Testing for "string" definitely makes sense in this case if that''s what function guarantees to return.> Some would argue that the strict equals operator (=== ) should be > used. So: >I don''t use strict comparison with "typeof" since both values are of the same type, but I guess it''s a matter of preference/conventions.> var t = document.createElement(''div''); > if (typeof t.textContent === ''string'') { > > > return function(element) { > > return element.textContent; > > } > > } > > else if (typeof element.innerText != ''undefined'') { > > return function(element) { > > return element.innerText; > > } > > } > > return function(){}; > > })(); > > Considering the result is more complex code, the benefit of such > optimisation must be weighed against the loss of simplicity. > > I expect that before the above becomes noticeably faster the function > will have to be run several thousand times in succession. That is > unlikely to occur so very little (if anything) will be gained by such > optimisation. I''d rather stick with simplicity. >As I mentioned, I wasn''t sure if code complexity is worth the performance gains of this "optimization". I always find such decisions a pain to make. Best, kangax --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---