Skip to content

Commit

Permalink
DataTable - export ordered data
Browse files Browse the repository at this point in the history
  • Loading branch information
cristineguadelupe committed Apr 24, 2024
1 parent 1243b47 commit 4b5f470
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions lib/kino/table.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ defmodule Kino.Table do
The returned map must contain the binary, the file extension and the mime type.
"""
@callback export_data(state(), String.t()) ::
@callback export_data(rows_spec(), state(), String.t()) ::
{:ok, %{data: binary(), extension: String.t(), type: String.t()}}

@optional_callbacks export_data: 2
@optional_callbacks export_data: 3

use Kino.JS, assets_path: "lib/assets/data_table/build"
use Kino.JS.Live
Expand Down Expand Up @@ -129,7 +129,7 @@ defmodule Kino.Table do
end

def handle_event("download", %{"format" => format}, ctx) do
{:ok, exported} = ctx.assigns.module.export_data(ctx.assigns.state, format)
{:ok, exported} = ctx.assigns.module.export_data(rows_spec(ctx), ctx.assigns.state, format)
info = %{filename: ctx.assigns.info.name, type: exported.type, format: exported.extension}
reply_payload = {:binary, info, exported.data}
send_event(ctx, ctx.origin, "download_content", reply_payload)
Expand Down Expand Up @@ -161,14 +161,8 @@ defmodule Kino.Table do
end

defp get_content(ctx) do
rows_spec = %{
offset: (ctx.assigns.page - 1) * ctx.assigns.limit,
limit: ctx.assigns.limit,
order: ctx.assigns.order
}

{:ok, %{columns: columns, data: {orientation, data}, total_rows: total_rows}, state} =
ctx.assigns.module.get_data(rows_spec, ctx.assigns.state)
ctx.assigns.module.get_data(rows_spec(ctx), ctx.assigns.state)

{columns, key_to_string} = stringify_keys(columns, ctx.assigns.key_to_string)

Expand Down Expand Up @@ -255,4 +249,12 @@ defmodule Kino.Table do
_ -> nil
end
end

defp rows_spec(ctx) do
%{
offset: (ctx.assigns.page - 1) * ctx.assigns.limit,
limit: ctx.assigns.limit,
order: ctx.assigns.order
}
end
end

0 comments on commit 4b5f470

Please sign in to comment.