PDA

View Full Version : [SOLVED] DomQuery - select element where child != value



adblockfreak
23 Feb 2009, 2:30 PM
Hi,
Having problems getting DomQuery to do exactly what I want. My customer has a page set up that displays an Ext JS grid containing several records, and they want to hide any records that contain an element "action" with inner text "Browsed". I've read docs and tutorials and tried several permutations like this...

message:not(message/action="Browsed")
message:not(.action="Browsed")
message:not(.action:contains(Browsed))
message:not(message.action=Browsed)

... All of these either display no results or all the results. I know exactly how I would do this in XPath (message[action!='Browsed']) but I'm stumped as to how to do it in DomQuery. Any ideas?

Sample data:


<root>
<messages>
<message>
<action>Browsed</action>
</message>
<message>
<action>aaaa</action>
</message>
<message>
<action>cccc</action>
</message>
</messages>
</root>

zhegwood
23 Feb 2009, 9:36 PM
Off the top of my head, so the syntax might not be 100%... I think this is what you're trying to accomplish, right?



var arrMessages = messages.childNodes();
for (var a = 0; a < arrMessages.length; a++) {
var action = arrMessages[a].firstChild();
var actionval = action.nodeValue;
if (actionval === "Browsed") {
//do stuff
}
}


'messages' must be a dom element, not an Ext object or element.

Also, here's a list of the dom functions ... Hope it helps!
http://www.w3schools.com/Dom/dom_element.asp

adblockfreak
24 Feb 2009, 6:20 AM
Close ... I actually want to do stuff if actionval != "Browsed"

This DomQuery is being passed in as the "record" parameter of a new XmlReader, which is then passed in as the "reader" parameter of a new dataStore, which is then used to populate the grid. So I don't know if I can rewrite this as a loop without restructuring the way the grid is populating.

I'll look into that link you sent me - maybe I can use those dom functions in my DomQuery to get what I need. Thanks!

Animal
24 Feb 2009, 8:00 AM
Why not just piddle about with it on the Firebug console command line until you get it right?

That's just what I did.

Get a document:



d=new DOMParser().parseFromString("<root><messages><message><action>Browsed</action></message><message><action>aaaa</action></message><message><action>cccc</action></message></messages></root>","text/xml");


Then start playing:



Ext.DomQuery.select("message", d)


OK, that get's them all, so READING THE DOCS, I tried.



Ext.DomQuery.select("message:not(:contains(Browsed))", d)


Why can't you do that?

adblockfreak
24 Feb 2009, 9:12 AM
Hi Animal,

Thanks for the Firebug instructions - I knew there was a way to test using Firebug but I couldn't figure out how to set up my test data. That helped a lot.

Unfortunately the query you suggest isn't quite what I need. I don't want to omit all messages that contain "Browsed" -- just those with a child "action" whose contents equal "Browsed." The message element can contain a lot of other data, which I omitted for brevity. For example, you could have one like this:



<message>
<subject>Have You Browsed This Site?</subject>
<sender>Tommy</sender>
<action>new</action>
</message>

I wouldn't want to omit this message (which the query you suggest would do). Like I said earlier, I have read the doc and looked at several other resources before asking for help.

I'll keep playing around using Firebug and see if I can come up with any new ideas, now that I know how to do that. Thanks again for that tip.

adblockfreak
26 Feb 2009, 11:51 AM
Hi Animal,

Thanks again for the Firebug code. One thing it showed me is that you can begin a phrase inside parentheses with ":thing(" which I don't think I had figured out. The documentation is great but there aren't really any examples. So, here is the query that works:



message:not(:has(action:nodeValue(Browsed)))


I tested it out with the following XML and it omitted just the first record, which is what I want:

<root>
<messages>
<message>
<action>Browsed</action>
</message>
<message>
<action>aaaa</action>
</message>
<message>
<subject>Have You Browsed This Site?</subject>
<sender>Tommy</sender>
<action>new<sub>Browsed</sub>
</action>
</message>
<message>
<action>cccc</action>
</message>
</messages>
</root>

adblockfreak
27 Feb 2009, 7:57 AM
Update, in the interest of sharing my learning: The code above worked fine in Firebug, but once I put it into my web site and opened the site in IE7, I got no results back. If I opened the site in Firefox, I got the filtered results that I expected. So I suspect there's some browser differences that Ext JS 2.0 is not quite compensating for -- I get the impression (but can't prove) that these problems center around the ":not" pseudo class. I hope this helps someone else.

Animal
27 Feb 2009, 8:40 AM
One thing that isn't so well documented.

You can add your own pseudos, so you can basically select using your own plugin function which can examine nodes, and see if they are what you want.