简体   繁体   English

如何在Firefox Addon XUL树中添加和删除行

[英]How to add and remove rows from a Firefox Addon XUL Tree

In my Firefox extension I have a simple tree which has two columns. 在我的Firefox扩展中,我有一个简单的树,它有两列。 I am trying to programatically: 我试图以编程方式:

  • Add a row 添加一行
  • Remove all rows 删除所有行

My XUL Tree: 我的XUL树:

<tree flex="1" id="mytree" hidecolumnpicker="true">

  <treecols>
    <treecol id="sender" label="Sender" flex="1"/>
    <treecol id="subject" label="Subject" flex="2"/>
  </treecols>

  <treechildren>

  </treechildren>

</tree>

Javascript I've tried to add rows: Javascript我试图添加行:

// trying to add rows
var data = {
    {'sender' : 'John', 'subject' : 'something'},
    {'sender' : 'Adam', 'subject' : 'something else'},
    {'sender' : 'Bob', 'subject' : 'something else again'}
};

document.getElementById("mytree").view.data = data;

There are no errors with this, the tree just does not get the rows added. 这没有错误,树只是没有添加行。 I know the code runs because if I add an alert() it fires. 我知道代码运行,因为如果我添加一个alert()它会触发。

Javascript I've tried to remove all rows: Javascript我试图删除所有行:

var tree = document.getElementById("mytree");

tree.view.data = {};
tree.view.treeBox.rowCountChanged(0, -1);

This produces an error: 这会产生错误:

document.getElementById("mytree").view.treeBox is undefined document.getElementById(“mytree”)。view.treeBox未定义

Edit: 编辑:

After Wladimir's suggestion to add the XUL elements directly, this will add a row: 在Wladimir建议直接添加XUL元素之后,这将添加一行:

var treeChildren = document.getElementById("my_tree_children");

var treeitem = document.createElement('treeitem');
var treerow = document.createElement('treerow');

var treecell_1 = document.createElement('treecell');
var treecell_2 = document.createElement('treecell');

treecell_1.setAttribute('label', 'John');
treecell_2.setAttribute('label', 'something');

treerow.appendChild(treecell_1);
treerow.appendChild(treecell_2);

treeitem.appendChild(treerow);

treeChildren.appendChild(treeitem);

Although hard coded in this example, the values for the cells (label attribute) will come from user input. 虽然在此示例中进行了硬编码,但单元格的值(标签属性)将来自用户输入。 What method of encoding/escaping should I use when putting user input directly into an attribute of a XUL element, to prevent XSS? 在将用户输入直接放入XUL元素的属性时,我应该使用什么编码/转义方法,以防止XSS?

Let me ask it this way: why should it work? 让我这样问:它为什么起作用? The documentation tells us that tree.view is an nsITreeView instance. 文档告诉我们tree.view是一个nsITreeView实例。 And nsITreeView has no property called data . nsITreeView没有名为data属性。 Just because you can set this property nevertheless doesn't mean that it will do anything. 仅仅因为你可以设置这个属性并不意味着它会做任何事情。

Now how would you actually put data into a tree widget? 现在,您如何将数据实际放入tree窗口小部件中? The documentation explains that there are several tree types, depending on type the data might come from a different place. 文档解释了有几种树类型,具体取决于数据类型可能来自不同的地方。 You probably want to use the default option - a content tree. 您可能希望使用默认选项 - 内容树。 This means that you need to create treeitem elements for each row in your data and add these to the treechildren element - as shown in the example . 这意味着您需要为数据中的每一行创建treeitem元素,并将这些元素添加到treechildren元素中 - 如示例所示。 You can generate these elements dynamically using the usual DOM methods. 您可以使用常用的DOM方法动态生成这些元素。

The tree widget can also generate its content automatically from an RDF or XML datasource. tree窗口小部件还可以从RDF或XML数据源自动生成其内容。 If you have your data in a JavaScript object however, the only alternative would be creating a custom nsITreeView implementation and assigning it to tree.view . 但是,如果您在JavaScript对象中拥有数据,唯一的选择是创建自定义nsITreeView实现并将其分配给tree.view But this is not quite trivial and usually an overkill. 但这并不是一件轻而易举的事,而且通常是一种矫枉过正。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM