DHTMLX Docs & Samples Explorer

Button Select Listed Option Creation

Select Parent:
Position:
Title:
Select Image:
Add?
Source
<script type="text/javascript" src="../../codebase/dhtmlxcommon.js"></script>
<script type="text/javascript" src="../../codebase/dhtmlxtoolbar.js"></script>
<link rel="stylesheet" type="text/css" href="../../codebase/skins/dhtmlxtoolbar_dhx_skyblue.css"></link>
 
<div id="toolbarObj"></div>
<div style="margin-top: 200px;">
<table border="0" cellspacing="0" cellpading="2">
    <tr>
        <td style="padding-right: 5px;">Select Parent:</td>
        <td><select id="sel"></select></td>
    </tr>
    <tr>
        <td style="padding-right: 5px; padding-left: 5px;">Position:</td>
        <td><input id="txt1" type="text" value="1" style="width: 30px;"></td>
    </tr>
    <tr>
        <td style="padding-right: 5px; padding-left: 5px;">Title:</td>
        <td><input id="txt2" type="text" value="New Listed Option"></td>
    </tr>
    <tr>
        <td style="padding-right: 5px; padding-left: 5px;">Select Image:</td>
        <td><table cellspacing="0" cellpadding="2" border="0"><tr>
            <td><img id="text_document" onclick="changeImage(this);" src="../common/imgs/text_document.gif" style="border: #707070 1px solid; width: 16px; height: 16px; cursor: pointer;"></td>
            <td><img id="stylesheet"    onclick="changeImage(this);" src="../common/imgs/stylesheet.gif"    style="border: #E0E0E0 1px solid; width: 16px; height: 16px; cursor: pointer;"></td>
            <td><img id="database"      onclick="changeImage(this);" src="../common/imgs/database.gif"      style="border: #E0E0E0 1px solid; width: 16px; height: 16px; cursor: pointer;"></td>
            <td><img id="presentation"  onclick="changeImage(this);" src="../common/imgs/presentation.gif"  style="border: #E0E0E0 1px solid; width: 16px; height: 16px; cursor: pointer;"></td>
            <td><img id="other"         onclick="changeImage(this);" src="../common/imgs/other.gif"         style="border: #E0E0E0 1px solid; width: 16px; height: 16px; cursor: pointer;"></td>
            <td><img id="page"          onclick="changeImage(this);" src="../common/imgs/page.gif"          style="border: #E0E0E0 1px solid; width: 16px; height: 16px; cursor: pointer;"></td>
            <td><img id="page_range"    onclick="changeImage(this);" src="../common/imgs/page_range.gif"    style="border: #E0E0E0 1px solid; width: 16px; height: 16px; cursor: pointer;"></td>
            <td><img id="selection"     onclick="changeImage(this);" src="../common/imgs/selection.gif"     style="border: #E0E0E0 1px solid; width: 16px; height: 16px; cursor: pointer;"></td>
            <td><img id="settings"      onclick="changeImage(this);" src="../common/imgs/settings.gif"      style="border: #E0E0E0 1px solid; width: 16px; height: 16px; cursor: pointer;"></td>
        </tr></table></td>
    </tr>
    <tr>
        <td style="padding-right: 5px; padding-left: 5px;">Add?</td>
        <td><input type="button" value="Add Listed Option" onclick="add();"></td>
    </tr>
</table>
</div>
<div style="margin-top: 20px;">
<select id="sel2"></select>
<input type="button" value="Remove" onclick="remove();">
</div>
<script>
var sel;
var sel2;
var toolbar;
function doOnLoad() {
    sel = document.getElementById("sel");
    sel2 = document.getElementById("sel2");
    toolbar = new dhtmlXToolbarObject("toolbarObj");
    toolbar.setIconsPath("../common/imgs/");
    toolbar.loadXML("../common/dhxtoolbar_buttonselect.xml?etc=" + new Date().getTime(), updateList);
}
function getId(selObj) {
    var id = selObj.options[selObj.selectedIndex].value;
    return id;
}
var img = "text_document.gif";
var lastImg = document.getElementById("text_document");
function changeImage(imgObj) {
    img = imgObj.id;
    imgObj.style.border = "#707070 1px solid";
    lastImg.style.border = "#E0E0E0 1px solid";
    lastImg = imgObj;
}
function remove() {
    if (sel2.options.length == 0) {
        return;
    }
    var id = getId(sel2).split(";");
    toolbar.removeListOption(id[0], id[1]);
    updateList();
}
function add() {
    var itemId = getId(sel);
    var pos = Number(document.getElementById("txt1").value);
    var text = document.getElementById("txt2").value;
    id = new Date().getTime();
    toolbar.addListOption(itemId, id, pos, "button", text, lastImg.id + ".gif");
    updateList();
}
function updateList() {
    sel.options.length = 0;
    sel2.options.length = 0;
    toolbar.forEachItem(function(itemId) {
        if (toolbar.getType(itemId) == "buttonSelect") {
            sel.options.add(new Option(itemId, itemId));
            toolbar.forEachListOption(itemId, function(listId) {
                sel2.options.add(new Option(itemId + " - " + listId, itemId + ";" + listId));
            });
        }
    });
}
</script>