Searching in a xul tree
By glazou on Monday 8 June 2009, 17:25 - Mozilla - Permalink
Working on my new twitter app, I heavily use SQLite-based templates (kudos to Laurent Jouanneau for that) and implemented search in twits in a so simple way it's fast and beautiful. I would like to share that with you so if you're building an app or extension using SQLite-based templates, you could use it too:
- let's suppose you have a table of todo list items
todolistin your sqlite databasedesktop.sqlite - your table
todolisthas three columns:text,assigneeandimportance, all three beingvarchar - you want to be able to search in that beast in all columns
First, you can build a tree representing your table like this:
<tree seltype="multiple"
id="myTodoListTree"
enableColumnDrag="true"
draggable="true"
datasources="profile:desktop.sqlite"
ref="?"
querytype="storage">
<treecols>
<treecol id="cText" label="Text" flex="1" persist="width ordinal"/>
<splitter class="tree-splitter" persist="ordinal"/>
<treecol id="cAssignee" label="Assignee" flex="1" persist="width ordinal"/>
<splitter class="tree-splitter" persist="ordinal"/>
<treecol id="cImportance" label="Importance" flex="1" persist="width ordinal"/>
</treecols>
<template>
<query>
SELECT *
FROM todolist
WHERE (text LIKE :like
OR assignee LIKE :like
OR importance LIKE :like)
<param name="like" id="like" type="string">%</param>
</query>
<action>
<treechildren id="myTodoListTreeChildren">
<treeitem uri="?">
<treerow>
<treecell label="?text"/>
<treecell label="?assignee"/>
<treecell label="?importance"/>
</treerow>
</treeitem>
</treechildren>
</action>
</template>
</tree>
Then adding a search field to your UI is trivial, thanks to the new cool features of Firefox 3.5:
<textbox type="search" oncommand="SearchKeyword(this)"/>
where your SearchKeyword function is:
function SearchKeyword(aElt)
{
var like = document.getElementById("like");
var value = aElt.value;
like.textContent = "%" + (value ? value + "%" : "");
document.getElementById("myTodoListTree").builder.rebuild();
}
That's all. Of course, you could also add a listener on the tree builder to preserve the selection and other cool things but all the magic is above. And it's sooooo simpler than a custom tree view...
/* enjoy */

Comments
Wow, the power of declarative programming at its most impressive!
Cheerz
We do a similar trick with an XML-template driven tree in our apps. I've detailed it here:
http://www.peppertop.com/blog/?p=31...
Because I can't change the XML file that I'm using as a datasource, my approach filters the displayed results, rather than the returned results - but the effect is similar.
Just one comment, the way you name your variables is horrible.
They're long, verbose, contain useless "my" and also bits of hungarian notation. That's as bad as it gets, I guess.
@loufoque: thanks for such a useful comment... Pfff. Gimme a break.
How do you deal with searches that contain "%"?