viewbindinggriffon

Griffon view - can I bind to to array element


In a model I have ..

@Bindable contentList = ['A','B','C','D','E','F','G','H','J','K','L','M']

In a view i have

def offset = 0

2.times { outer ->
  6.times { inner ->
     panel(background: Color.white,
           border: BorderFactory.createLineBorder(Color.black),
           constraints: inner.equals(5) ? 'grow,wrap' : 'grow') {
           label(text: bind {model.contentList[offset++]},
                    font: new Font("Sans Serif",
                    Font.PLAIN, 18))
          }
     }
 }

On initial loading this works fine but when I try and update an element in the array in a controller it's not reflected in the screen .. Is there a way to bind to an array element ?

Thanks


Solution

  • You can use ObservableList for this purpose. For example, in the model, you can declare contentList as:

    ObservableList contentList = new ObservableList(['A', 'B', 'C', 'D', 'E', 
       'F', 'G', 'H', 'J', 'K', 'L', 'M'])
    

    If you bind to the content property of ObservableList, you will be notified when it is changed. You can then use a converter to retrieve the proper value based on certain index, for example:

    def offset = 0
    def c = { o, v -> v[o] }
    2.times { outer ->
       6.times { inner ->
           label(text: bind('content', source: model.contentList, 
                            converter: c.curry(offset++)))
       }
    }