Skip to main content
15-Moonstone
June 5, 2020
Solved

Iterate through all the widgets available in a view by code.

  • June 5, 2020
  • 3 replies
  • 1597 views

Hi.

 

I want to iterate through all the widgets available in a view through JavaScript. I can access the list of all the widgets through this:

 

 

console.log($scope.view.wdg);

 

 

I observe that this is in JSON format. However, when I do

 

 

console.log(Object.keys($scope.view.wdg).length);

 

 

, which would return the number of keys in a JSON object, it logs "0".

 

Has anyone iterated through all the widgets available in a view before? If so, any help would be appreciated.

 

Thank you.

Best answer by RolandRaytchev

Hi @Radu 

different way to do this - here one example:

 var wdgs=$scope.app.view.Home.wdg; 
 for(wdg in wdgs)
 { console.warn(wdgs[wdg])
 for (prop in wdgs[wdg])
 console.warn(prop+":"+wdgs[wdg][prop])
 }

this will print for all widgets in the home view all properties

3 replies

21-Topaz I
June 5, 2020

Hi @Radu 

different way to do this - here one example:

 var wdgs=$scope.app.view.Home.wdg; 
 for(wdg in wdgs)
 { console.warn(wdgs[wdg])
 for (prop in wdgs[wdg])
 console.warn(prop+":"+wdgs[wdg][prop])
 }

this will print for all widgets in the home view all properties

21-Topaz I
June 5, 2020

here another way:

$scope.app.modelWdgListCreate= function(){
 var wdgs=$scope.view.wdg;
 if($scope.DEBUG){
 console.warn ($scope);console.warn(wdgs);}
retList=[];
 Object.keys(wdgs).forEach(function(key) { 
 var wdg = wdgs[key];
 if (('sequence' in wdg)==true){//is a model widget
 const objToadd={name:wdg['widgetName'],file:wdg['src']};
 //retList.push( objToadd);// in this case will create json
 retList[wdg['widgetName']]=objToadd; //this will make arr with index=nameId
 }});
$scope.seqMdlListArr= retList;//set the $scope list variable
if($scope.DEBUG) console.warn($scope.seqMdlListArr);
$scope.$applyAsync();};
//================================================

In this example above I create form the widget a list only for the widgetNaem and the source property. It checks and handled only widgets which have a sequnece property

21-Topaz I
June 5, 2020

here the code I reset the src and the sequence of widgets

//===========================================
// will reset the src and sequnece if was set
//===========================================
$scope.app.modelWdgReset= function(){

 wdgs=$scope.view.wdg
Object.keys(wdgs).forEach(function(key) { 
 wdg = wdgs[key]
 
 if (('sequence' in wdg)==true){
 
 if($scope.DEBUG) console.warn("=>modelWdgReset key="+key)
 $scope.setWidgetProp(key,'sequence','')
 $scope.setWidgetProp(key,'src','')
 $scope.setWidgetProp(key,'visible',false)
 // wdg['widgetName'].sequence ='';//set it to undefined
 // wdg['widgetName'].src='';//set it to undefined
 // wdg['widgetName'].visible=false; 
 									 }});
 $scope.$applyAsync();};