cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Learn all about PTC Community Badges. Engage with PTC and see how many you can earn! X

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

Radu
15-Moonstone

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

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.

1 ACCEPTED SOLUTION

Accepted Solutions

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

View solution in original post

3 REPLIES 3

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

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

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();};
Top Tags