dhtmlxToolbar: How to test if an item exists

Since you can add/remove items as well as create a toolbar via xml, how to do test to see if a specific element exists?



The best I could come up with is using the iterator to loop through and see if something exists. This seems like a foolish method, is there a better one?



function WebBarHasItem(TheBar,TheItem){

    var tRet = false

    TheBar.forEachItem(

        function(itemId){

            if (itemId == TheItem){

                tRet = true

            }

        }

    );

    return tRet

}


There is not public method to check if item exists.


But you can check that as follows:


if(toolbar.objPull[toolbar.idPrefix+itemId]) alert(“exists”)


Where toolbar is toolbar object. itemId is id of the item you want to check

Is there any reason there isn’t a WebBar.ItemExists(“ItemID”) function?  It seems like a pretty basic feature no?  

Given your short answer, I think I can modify the source and add the function.  Thank you for your rapid response.


Hello,


possibly we’ll add this method in some of the next versions. But what is the reason for using this method ?

Well, if you don’t know an item exists and you try to reference it then you get errors without wrapping everything in lots of try/catchs.  Generally I prefer to test for the existence of an item rather than trying to access it and handing an error.

I am using the toolbar and loading its collection from XML generated by ASP scripts based on SQL database info.  I am using the bar as part of a quick order form for a web site.  The display will be an array of toolbars being added by using your dhtmlxCombo to ajax the server on product SKUs and add a new toolbar row for the product selected.  The bar is being used to display product details and provide the ability to enter a quantity/options.  Some items require a select list because they can be ordered only in multiples, ie 6,12,24, etc.  Other items can be ordered one at a time and thus the bar will have a text box.  Since the text box and select lists need to be handled differently, I need a method to find out whether an item called “qty_sel” (the select list) or “qty_text” exists in the bar.  Since the bar is populated by XML from server side, the client JS needs a way to see if specific items were generated.

I had expected that if a component/object has a collection, a way to see if an item exists in that collection seemed a completely natural need.  Your objects are so well done that I was so totally surprised that no function existed to test for the existence of an item on the bar.

My pseudo logic in looping through the toolbar array that \ I was planning would have looked something like:

if ( webbar[theIndex].itemExists(textbox) ){
    get value of text box item
    get value of price item
    product total=qty*price
    stuff result in a div for display
    add to subtotal var
} else {
    get text of selected option in select list   (will be “6 @ $2.99”)
    split it on the @
    product total=split qty * split price
    stuff result in a div for display
    add to subtotal var
}

 
I hope that makes sense.


You’re right. Some public functionality for item testing will be useful.


We’ll add getItem(id) into toolbar 2.5. It will return item object if the item exists (toolbar.objPull[toolbar.idPrefix+itemId]).