fn-fx / fn-fx

A Functional API around JavaFX / OpenJFX.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Need a way to define `:cell-value-factory` for `table-column` component

titonbarua opened this issue · comments

As far as I know, currently there is no idiomatic way to define :cell-value-property for table-column components. You have to jump through hoops to create an object which implement the java Callback interface. Currently I am using a custom function, thanks to this blogpost. Here's the code I am using:

(defn cell-value-factory [f]
  (reify javafx.util.Callback
    (call [this entity]
      (javafx.beans.property.ReadOnlyObjectWrapper. (f (.getValue entity))))))

(defui MyTable
  (render [this _]
      (ui/table-view
        :items [{:name "foo" :age 10}
                {:name "bar" :age 56}]
        :columns [(ui/table-column
                  :text "Name"
                  :cell-value-factory (cell-value-factory #(:name %)))
                  (ui/table-column
                   :text "Age"
                   :cell-value-factory (cell-value-factory #(:age %)))])))

While this works; ideally (IMO), the object creation should be handled by the table-column component itself and :cell-value-factory should accept plain clojure functions. Like this:

(defui MyTable
  (render [this _]
      (ui/table-view
        :items [{:name "foo" :age 10}
                {:name "bar" :age 56}]
        :columns [(ui/table-column
                  :text "Name"
                  :cell-value-factory #(:name %))
                  (ui/table-column
                   :text "Age"
                   :cell-value-factory #(:age %))])))