XUL中的列表操作
List manipulation in XUL
我想找出一种简单的方法来编辑/添加/删除XUL列表上的项目。我最初的想法是用一个单独的文件来处理所有这些,但我不确定如何用另一个文件影响主XUL。到目前为止,我的列表看起来像:
<?xml version = "1.0"?>
<!DOCTYPE window>
<window title = "Hello"
xmlns:html="http://www.w3.org/1999/xhtml"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" >
<script>
</script>
<listbox id = "mainList" flex = "1">
<listhead>
<listheader label = "Album Name"/>
<listheader label = "Artist"/>
<listheader label = "Year"/>
<listheader label = "Sales"/>
<listheader label = "Rating"/>
<listheader label = "Genre"/>
<listheader label = "Edit" />
<listheader label = "Delete"/>
</listhead>
<listitem id = "1">
<listcell label = "OK Computer"/>
<listcell label = "Radiohead"/>
<listcell label = "1997"/>
<listcell label = "Platinum"/>
<listcell label = "5/5"/>
<listcell label = "Alternative Rock"/>
<button label = "Edit" oncommand= "editItem()"/>
<button label = "Delete" oncommand = "deleteItem()"/>
</listitem>
<listitem>
<listcell label = "The Moon and Antarctica"/>
<listcell label = "Modest Mouse"/>
<listcell label = "2000"/>
<listcell label = "Gold"/>
<listcell label = "4.5/5"/>
<listcell label = "Alternative Rock"/>
<button label = "Edit"/>
<button label = "Delete"/>
</listitem>
<listitem>
<listcell label = "Pinkerton"/>
<listcell label = "Weezer"/>
<listcell label = "1996"/>
<listcell label = "Gold"/>
<listcell label = "5/5"/>
<listcell label = "Alternative Rock"/>
<button label = "Edit"/>
<button label = "Delete"/>
</listitem>
<listitem>
<listcell label = "Helplessness Blues"/>
<listcell label = "Fleet Foxes"/>
<listcell label = "2011"/>
<listcell label = "Gold"/>
<listcell label = "4/5"/>
<listcell label = "Folk Pop"/>
<button label = "Edit"/>
<button label = "Delete"/>
</listitem>
</listbox>
</window>
相当简单,但我对我需要什么javascript来使按钮实际工作感到困惑。理想情况下,我希望有一个Add按钮,将打开一个新窗口,每个列的空白字段,然后将新行添加到列表中。实现这一目标的最佳方式是什么?
使用常规的DOM操作函数。当动态添加项目时,如果你有一个"模板",你可以克隆和修改,例如:
<listitem id="template" hidden="true">
<listcell class="album"/>
<listcell class="title"/>
<listcell class="year"/>
<listcell class="group"/>
<listcell class="rating"/>
<listcell class="category"/>
<button label="Edit" oncommand="editItem()"/>
<button label="Delete" oncommand="deleteItem()"/>
</listitem>
你可以像这样添加一个新条目:
var item = document.getElementById("template").cloneNode(true);
item.removeAttribute("id");
item.removeAttribute("hidden");
item.getElementsByClassName("album")[0].setAttribute("label", albumName);
item.getElementsByClassName("title")[0].setAttribute("label", songName);
...
document.getElementById("mainList").appendChild(item);
修改现有条目的文本也是类似的。你必须从某个地方获得新的文本-添加文本字段进行编辑是你的责任,但是,列表没有内置的编辑功能。
删除项显然更简单:
var item = document.getElementById("item1");
item.parentNode.removeChild(item);
相关文章:
- 无法在mvc视图中使用ajax调用获取操作返回的模型对象列表
- jQuery-将列表项的一个元素移动到同一项的另一个元素中+对我的列表中的每个项执行此操作
- 改变“;onClick"这个jQuery的操作从清除输入文本改为将输入文本添加到下面的列表中
- jQuery-在带有独立操作的下拉列表项末尾有图标
- 从填充了 AJAX 的下拉列表中获取控制器的操作方法中的值
- 以相同的形式处理javascript和php中下拉列表的操作
- MVC 下拉列表更改事件未正确调用控制器中的操作
- 将JSON对象列表传递给操作
- 如何操作TinyMCE列表框元素
- DOM 操作以重现订单列表
- 在 mongodb 查询中操作列表
- 节点列表操作
- 为什么聚合物核心列表选择中的切换操作不正确
- Facebook javascript:如何获取用户对应用程序执行的操作列表
- 为动态创建的列表项及其内容添加单击操作
- 加快列表的 jQuery 操作
- 操作需要识别{{#each}}列表中的动态项
- javascript列表操作库/框架
- 将有序操作列表合并为一个可观察量
- Sharepoint操作列表-由于时区原因,可能错误转换了截止日期