Skip to content

Commit

Permalink
Send the row index into the rowAttrs function
Browse files Browse the repository at this point in the history
This allows one to do client-side paging on a table while maintaining
the sort order given by the columns.
  • Loading branch information
rehno-lindeque committed Jul 6, 2017
1 parent bf632ce commit a646081
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions src/Table.elm
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ import Html exposing (Html, Attribute)
import Html.Attributes as Attr
import Html.Events as E
import Html.Keyed as Keyed
import Html.Lazy exposing (lazy2, lazy3)
import Html.Lazy exposing (lazy)
import Json.Decode as Json


Expand Down Expand Up @@ -185,7 +185,7 @@ type alias Customizations data msg =
, thead : List (String, Status, Attribute msg) -> HtmlDetails msg
, tfoot : Maybe (HtmlDetails msg)
, tbodyAttrs : List (Attribute msg)
, rowAttrs : data -> List (Attribute msg)
, rowAttrs : Int -> data -> List (Attribute msg)
}


Expand Down Expand Up @@ -253,8 +253,8 @@ lightGrey symbol =
Html.span [ Attr.style [("color", "#ccc")] ] [ Html.text (" " ++ symbol) ]


simpleRowAttrs : data -> List (Attribute msg)
simpleRowAttrs _ =
simpleRowAttrs : Int -> data -> List (Attribute msg)
simpleRowAttrs _ _ =
[]


Expand Down Expand Up @@ -429,7 +429,7 @@ view (Config { toId, toMsg, columns, customizations }) state data =

tbody =
Keyed.node "tbody" customizations.tbodyAttrs <|
List.map (viewRow toId columns customizations.rowAttrs) sortedData
List.indexedMap (viewRow toId columns customizations.rowAttrs) sortedData

withFoot =
case customizations.tfoot of
Expand Down Expand Up @@ -479,16 +479,19 @@ onClick name isReversed toMsg =
Json.map2 State (Json.succeed name) (Json.succeed isReversed)


viewRow : (data -> String) -> List (ColumnData data msg) -> (data -> List (Attribute msg)) -> data -> ( String, Html msg )
viewRow toId columns toRowAttrs data =
( toId data
, lazy3 viewRowHelp columns toRowAttrs data
)
viewRow : (data -> String) -> List (ColumnData data msg) -> (Int -> data -> List (Attribute msg)) -> Int -> data -> ( String, Html msg )
viewRow toId columns toRowAttrs sortIndex data =
let
lazy4 f a b c d = lazy (\(w,x,y,z) -> f w x y z) (a,b,c,d)

This comment has been minimized.

Copy link
@HanStolpo

HanStolpo Jul 6, 2017

I think this is never going to be lazy because the lamda is created newly each time and the function itself is also checked for equality, so basically I think only top level functions can safely be wrapped in lazy. Also the other point is that referential transparency is used and this creates a new tuple. @rehno-lindeque see https://github.com/elm-lang/virtual-dom/blob/4be12eb1ff81d0b97e745aedf089aaf148212eb0/src/Native/VirtualDom.js#L561-L570 . So I am pretty sure this will never be lazy and force a diff each time due to both issues, using a lambda and creating a new tuple.

This comment has been minimized.

Copy link
@rehno-lindeque

rehno-lindeque Jul 6, 2017

Author

Dang, I had an incling that might be weird. Thanks for pointing it out.

This comment has been minimized.

Copy link
@ocharles

ocharles Jul 6, 2017

Yep, this is true. I personally think the approach to laziness is wrong, but there's not much we can do about that 🤷‍♂️

This comment has been minimized.

Copy link
@rehno-lindeque

rehno-lindeque Jul 6, 2017

Author

Yeah, geez that really is a bummer. The only way I can think of to handle this is by messing around with keys in some ugly way.

I'm going to try something completely different instead...

in
( toId data
, lazy4 viewRowHelp columns toRowAttrs sortIndex data
)


viewRowHelp : List (ColumnData data msg) -> (data -> List (Attribute msg)) -> data -> Html msg
viewRowHelp columns toRowAttrs data =
Html.tr (toRowAttrs data) (List.map (viewCell data) columns)
viewRowHelp : List (ColumnData data msg) -> (Int -> data -> List (Attribute msg)) -> Int -> data -> Html msg
viewRowHelp columns toRowAttrs sortIndex data =
Html.tr (toRowAttrs sortIndex data) (List.map (viewCell data) columns)


viewCell : data -> ColumnData data msg -> Html msg
Expand Down

0 comments on commit a646081

Please # to comment.