Список манипуляций в XUL - PullRequest
       1

Список манипуляций в XUL

0 голосов
/ 22 сентября 2011

Я пытаюсь найти простой способ редактировать / добавлять / удалять элементы в списке в 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 мне нужен, чтобы кнопки действительно работали.В идеале я хотел бы иметь кнопку «Добавить», которая откроет новое окно с пустыми полями для каждого из столбцов, а затем добавит новую строку в список.Каков наилучший способ сделать это?

1 Ответ

1 голос
/ 22 сентября 2011

Вы используете обычные функции управления 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);
...