Skip to content

Commit

Permalink
Show correct count when filtering columns (#34)
Browse files Browse the repository at this point in the history
* Show correct count when filtering columns

* Fix typo in test name
  • Loading branch information
nathanjohnson320 authored Jan 20, 2021
1 parent 26ca3dd commit d70b19c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
22 changes: 21 additions & 1 deletion example/test/phoenix_datatables_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,26 @@ defmodule PhoenixDatatablesTest do
} = PhoenixDatatables.execute(query, request, Repo, [total_entries: 25])
assert draw == request["draw"]
end

test "do all of the things in phoenix datatables to search columns" do
{ request, query } = create_request_and_query()
request = request
|> update_in(["columns", "0", "search"], &(Map.put(&1, "value", "1NSN")))
|> Map.put("search", %{"regex" => "false", "value" => ""})

assert %Payload{
data: data,
draw: draw,
error: _error,
recordsFiltered: recordsFiltered,
recordsTotal: recordsTotal
} = PhoenixDatatables.execute(query, request, Repo, [columns: [id: 0, common_name: 0, nsn: 0]])

assert draw == request["draw"]
assert Enum.count(data) == 1
assert recordsTotal == 2
assert recordsFiltered == 1
end
end

def create_request_and_query do
Expand All @@ -68,7 +88,7 @@ defmodule PhoenixDatatablesTest do
query =
(from item in Item,
join: category in assoc(item, :category),
select: %{id: item.id, category_name: category.name})
select: %{id: item.id, category_name: category.name, nsn: item.nsn})
{request, query}
end

Expand Down
2 changes: 1 addition & 1 deletion lib/phoenix_datatables.ex
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ defmodule PhoenixDatatables do
|> Query.paginate(params)

filtered_entries =
if params.search.value == "" do
if params.search.value == "" and not Query.has_column_search?(params.columns) do
total_entries
else
Query.total_entries(filtered_query, repo)
Expand Down
4 changes: 2 additions & 2 deletions lib/phoenix_datatables/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -254,12 +254,12 @@ defmodule PhoenixDatatables.Query do
end
end

defp has_column_search?(columns) when is_map(columns) do
def has_column_search?(columns) when is_map(columns) do
columns = Map.values(columns)
Enum.any?(columns, &(&1.search.value != ""))
end

defp has_column_search?(_), do: false
def has_column_search?(_), do: false

defp do_search_columns(queryable, params, columns) do
dynamic = dynamic([], true)
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule PhoenixDatatables.Mixfile do
def project do
[
app: :phoenix_datatables,
version: "0.4.3",
version: "0.4.4",
elixir: "~> 1.5",
elixirc_paths: elixirc_paths(Mix.env),
deps: deps(),
Expand Down

0 comments on commit d70b19c

Please sign in to comment.