jQuery UI Widgets Forums Lists ListBox Items undefined(Listbox)

This topic contains 3 replies, has 2 voices, and was last updated by  ivailo 8 years, 7 months ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
  • Items undefined(Listbox) #75283

    joedbug
    Participant

    hello
    i am try to check if an item is added to a listbox already,
    here is my code
    `$(“#addbtn”).on(‘click’, function(event){
    //get the employee selected
    var items = $(“#empshiftlists”).jqxListBox(‘getSelectedItems’);

    if(items == “”){
    alert(“please select Employee to assign a shift”);
    }else{

    for(var i = 0; i< items.length – 0; i++){
    var value = items[i].value;
    var label = items[i].label;
    //alert(value);
    var flag = checkAssigned();
    if(flag){
    }else{
    $(“#assignedemp”).jqxListBox(‘addItem’, {value:value,label:label});
    }
    } // end for
    $(“#empshiftlists”).jqxListBox(‘clearSelection’);
    $(“#empshiftlists”).jqxListBox(‘endUpdate’);
    $(“#assignedemp”).jqxListBox(‘endUpdate’);
    }
    });
    function checkAssigned(){
    var items = $(“#assignedemp”).jqxListBox(‘getItems’);
    var selectItem = $(“#empshiftlists”).jqxListBox(‘getSelectedItem’);
    for(var i=0;i < items.length; i++){
    var value = items[i].value;
    if(value == selectItem.value ){
    return true;
    }else{
    return false;
    }
    }
    }

    
    here is how i create my listbox
    

    var url = “gets/getemp.php”;
    var source = {
    datatype: “json”,
    datafields: [
    {name: ‘staffId’},
    {name: ‘name’}
    ],
    url: url
    //async: true
    };
    var dataAdapter = new $.jqx.dataAdapter(source);
    $(“#empshiftlists”).jqxListBox({
    source: dataAdapter,
    theme: ‘classic’,
    allowDrag: true,
    allowDrop: true,
    width: 200,
    height: 250,
    displayMember: ‘name’,
    valueMember: ‘staffId’,
    multiple: false
    });
    $(“#assignedemp”).jqxListBox({
    width: 200,
    height: 250,
    allowDrag: true,
    allowDrop: true,
    displayMember: ‘name’,
    valueMember: ‘staffId’
    });`
    it is giving me an error items undefined. pls can i be put through to get the solution to my problem

    Items undefined(Listbox) #75317

    ivailo
    Participant

    Hi joedbug,

    You are trying to get items from empty listbox at the begining. This gives your that error.
    Try to check first is your object empty.

    if(!jQuery.isEmptyObject(items)){
            for (var i = 0; i < items.length; i++) {
                var value = items[i].value;
                if (value == selectItem.value) {
                    return true;
                } else {
                    return false;
                }
            }
        }else{
            return false;
        }

    Best Regards,
    Ivailo Ivanov

    jQWidgets Team
    http://www.jqwidgets.com

    Items undefined(Listbox) #75324

    joedbug
    Participant

    thanks Ivailo Ivanov,
    it works but still have issues in that it is not iterating in the list just work for the first data on the list.
    thanks

    Items undefined(Listbox) #75331

    ivailo
    Participant

    Hi joedbug,

    This behavior is occurred cause you return boolean after your first iteration of listbox array.
    Better use boolean flag and when the loop is finished return this flag.

    function checkAssigned() {
        var items = $("#assignedemp").jqxListBox('getItems');
        var foundSameItem=false;
        var selectItem = $("#empshiftlists").jqxListBox('getSelectedItem');
        if(!jQuery.isEmptyObject(items)){
            for (var i = 0; i < items.length; i++) {
                var value = items[i].value;
                if (value == selectItem.value) {
                    foundSameItem = true;
                } 
            }
            return foundSameItem;
        }else{
            return false;
        }
    }

    Best Regards,
    Ivailo Ivanov

    jQWidgets Team
    http://www.jqwidgets.com

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.