forked from evancz/elm-sortable-table
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Send the row index into the rowAttrs function
This allows one to do client-side paging on a table while maintaining the sort order given by the columns.
- Loading branch information
1 parent
bf632ce
commit a646081
Showing
1 changed file
with
16 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
||
|
||
|
@@ -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) | ||
} | ||
|
||
|
||
|
@@ -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 _ _ = | ||
[] | ||
|
||
|
||
|
@@ -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 | ||
|
@@ -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.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
rehno-lindeque
Author
|
||
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 | ||
|
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.