Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DataTable - export ordered data #418

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading