Total items count?

How do I get the total number of items in a dropdown list?


7 Replies

PO Prince Oliver Syncfusion Team September 19, 2017 11:15 AM UTC

Hi Michael 

Thank you for contacting Syncfusion forums. 

You can get the total number of items in a DropDownList from the getListData method. Kindly refer to the following code snippet.  

<script type="text/javascript"> 
    function buttonclick(){ 
        var target = $('#skillsets').data("ejDropDownList"); // accessing the control's instance 
        alert("Number of items: "+target.getListData().length); //accessing number of items from getListData method 
    } 
</script> 

Kindly refer to the following playground sample: http://jsplayground.syncfusion.com/qjpiump4 

Regards, 
Prince 



ML Michael Lambert September 19, 2017 11:31 PM UTC

I tried that and get this error:


I tried grp1.getListData.length (without paras) but the value is always 0.



PO Prince Oliver Syncfusion Team September 20, 2017 04:26 AM UTC

Hi Michael  

Thank you for your update. 

The getListData is a method which returns the list of items present in the DropDownList, so grp1.getListData.length (without parenthesis) will return value 0. Kindly refer to the following UG documentation link: https://help.syncfusion.com/api/js/ejdropdownlist#methods:getlistdata 

You can update your code to validate the getListData method whether it is empty or not before accessing length. Kindly refer to the following code snippet. 

<script > 
    var grp1 = $('#ddlBUGrp2').data("ejDropDownList"); // accessing control's instance 
    var x = ej.isNullOrUndefined(grp1.getListData()) ? 0 : grp1.getListData().length; //if the getListData is undefined, then return 0. 
</script> 

Regards, 
Prince 




ML Michael Lambert September 20, 2017 08:47 PM UTC

That works, but now run into another interesting thing:

var grp2 = $("#ddlBUGrp2").data("ejDropDownList");

   $('#ddlBUGrp2').ejDropDownList({ dataSource: data});  //data is a list with say 4 items
   var numItems1 = ej.isNullOrUndefined(grp2.getListData()) ? 0 : grp2.getListData().length;  //numItems1 = 4 which would be correct

  $('#ddlBUGrp2').ejDropDownList({ dataSource: null});
  var numItems2 = ej.isNullOrUndefined(grp2.getListData()) ? 0 : grp2.getListData().length;  //numItems2 still = 4 shouldn't it be 0;

Mike




PO Prince Oliver Syncfusion Team September 21, 2017 05:22 AM UTC

Hi Michael   

Thank you for your update. 

Alternatively, you can use our internal _getLi method which returns all the <li> elements in the DropDownList’s popup wrapper. Kindly refer to the following code snippet. 

<script type="text/javascript">   
    var grp2 = $("#ddlBUGrp2").data("ejDropDownList"); 
    $('#ddlBUGrp2').ejDropDownList({ dataSource: data});  
    var numItems1 = grp2._getLi().length;   
    $('#ddlBUGrp2').ejDropDownList({ dataSource: null}); 
    var numItems2 = grp2._getLi().length;  
</script> 


Regards, 
Prince 



PK pradeep kurmar replied to Prince Oliver March 7, 2018 04:20 PM UTC

Hi Michael 

Thank you for contacting Syncfusion forums. 

You can get the total number of items in a DropDownList from the getListData method. Kindly refer to the following code snippet.  

<script type="text/javascript"> 
    function buttonclick(){ 
        var target = $('#skillsets').data("ejDropDownList"); // accessing the control's instance 
        alert("Number of items: "+target.getListData().length); //accessing number of items from getListData method 
    } 
</script> 

Kindly refer to the following playground sample: http://jsplayground.syncfusion.com/qjpiump4 

Regards, 
Prince 


HELLO



IB Ilakkiya Baskar Syncfusion Team March 9, 2018 01:00 PM UTC

Hi Michael, 
 
Thank you for your patience.  
 
We have validated the reported issue at our end. We have considered it as a defect and logged a defect report. The fix for the issue will be included in Vol-1 2018 service pack -1 release, which will be available by the end of March  2018. We appreciate your patience until then.  
 
Regards,  
Ilakkiya B 


Loader.
Up arrow icon