How to add a column dynamically to a repeating panel

I have a repeating panel inside a data grid. I fetch the rows from database and assign the rows to the repeating panel items. Based on the values of the columns for each row I want to calculate something and based on the result I want to then display a image or string on a new column for each row (ie ‘warning’,‘ok’,‘error’ etc). Is it possible to do this? How do I loop through each row in a repeating panel and add this column dynamically?

You can change the rows anytime using the items attribute of a repeating panel. I typically feed repeating panes a list of dicts. It might look something like:

self.repeating_panel.items = [{'foo':'bar','message':OK}]

To add a row to the bottom of this repeating panel, you could use

self.repeating_panel.items.append({'foo':'baz','message':'Error!'})

a quick note that using append alone isn’t enough here.
Use either:

self.repeating_panel.items.append({'foo':'baz','message':'Error!'})
self.repeating_panel.items = self.repeating_panel.items

or

self.repeating_panel.items = self.repeating_panel.items  +  [{'foo':'baz','message':'Error!'}]

related topics:


regarding how to loop over the repeating panels - you have some optoins

for componet in repeating_panel.get_components():
  # do something

or use the __init__ method of the ItemTemplate itself

class ItemTemplate(ItemTemplateForm):
   def __init__(self, **properties):
     self.init_components(**properties)
     # now you have self.item
     if self.item['bar']:
        # do something...
        self.bar_component.visible = False

or set databindings on the components of ItemTemplate
Screen Shot 2020-05-21 at 18.31.14


you might also find this post helpful

Thank you. Ended up using something like this:

    for r in self.repeating_panel_1.get_components():
            pt = r.item['Rank']
            if r.item['English'] is True:
              pt += 1
            if r.item['HighSchool'] is False:
              pt += 1
            if pt <= 3:
              r.grade_image.source = '_/theme/failed.png'
            if pt > 3 and pt < 5:
              r.grade_image.source = '_/theme/pass.png'
            if pt > 6 :
              r.grade_image.source = '_/theme/highest.png'

Notice to access the component in each repeating panel one need to use dot (.) ie r.grade_image.

1 Like

great!

i’d be tempted to put this in the template init method

class ItemTemplate(ItemTemplateForm):
  def __init__(self, **properties):
    self.init_components(**properties)
    pts = self.item['Rank'] + self.item['English'] + not self.item['HighSchool']
    # True and False can be treated as 1 and 0
    if pts > 6:
      self.grae_image.source = '_/theme/highest.png'
    elif pts > 3: 
      self.grae_image.source = '_/theme/pass.png'
    else:
      self.grae_image.source = '_/theme/failed.png'

1 Like