Elixir 订单搜索结果
我最近在我的应用程序中添加了搜索书籍功能。现在,我想添加一个排序功能,但我很难做到这一点。我想要达到的目标: 当我不搜索任何书籍标题时,我希望能够根据书籍/索引上的价格或标题对书籍进行排序。但是,如果我使用搜索功能,我希望能够按价格或标题对找到的书籍进行排序。有人可以帮助我或指导我如何做,并结合我目前的代码吗?我附上我当前的搜索代码,以及我为排序功能提供的表单 搜索栏位于layout/app.html.eex中 book/index.html.eex-顶部的排序表单Elixir 订单搜索结果,elixir,phoenix-framework,Elixir,Phoenix Framework,我最近在我的应用程序中添加了搜索书籍功能。现在,我想添加一个排序功能,但我很难做到这一点。我想要达到的目标: 当我不搜索任何书籍标题时,我希望能够根据书籍/索引上的价格或标题对书籍进行排序。但是,如果我使用搜索功能,我希望能够按价格或标题对找到的书籍进行排序。有人可以帮助我或指导我如何做,并结合我目前的代码吗?我附上我当前的搜索代码,以及我为排序功能提供的表单 搜索栏位于layout/app.html.eex中 book/index.html.eex-顶部的排序表单 <%= f = for
<%= f = form_for @conn, book_path(@conn, :index), [method: :get, as: "order_and_filter"], fn f-> %>
<% options = ["Order by": "", "Title": "title_asc", "Title (Desc)": "title_desc",
"Price": "price_asc", "Price (Desc)": "price_desc"] %>
<%= select f, :order_by, options, class: "tag-select tag-select-sm" %>
<%= submit "Sort" %>
<% end %>
</form>
<div class="grid sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-5 gap-x-10 gap-y-10 p-6 ">
<%= for book <- @books do %>
<%= link to: Routes.book_path(@conn, :show, book) do %>
<div
class="flex flex-col justify-between w-60 sm:w-60 h-80 bg-white bg-center text-gray-800 shadow-md overflow-hidden cursor-pointer rounded-md"
style='background-image: url("<%= book_img_without_img_tag(book, :thumbnail) %>"); background-repeat: no-repeat; background-size: 100% 100%;'>
<div class="flex justify-between items-center ml-4 pr-8">
</div>
<div class="bg-white bg-opacity-95 shadow-md rounded-r-xl p-4 flex flex-col mr-4 mb-8">
<h3 class="truncate text-s font-bold pb-2"><%= book.title %></h3>
<p class="truncate text-gray-500 text-sm"><%= full_name(book.author) %></p>
<div class="flex justify-between items-center">
<span class="text-gray-400 text-xs">£<%= book.original_price %></span>
</div>
</div>
</div>
<% end %>
<% end %>
</div>
存货清单
def list_books(params) do
search_term = get_in(params, ["query"])
Book
|> Book.search(search_term)
|> preload(:category)
|> preload(:author)
|> Repo.all()
end
我认为当模式匹配对您有所帮助时,情况就是这样。 我建议您在下面添加代码,以匹配空字符串的大小写和排序,如果搜索中有一些值,如果我理解正确,请不要排序
def search(query, "") do # change there
wildcard_search = "%#{""}%"
from(b in query,
join: a in assoc(b, :author),
where: fragment("? % ?", b.title, ^wildcard_search),
or_where: ilike(b.description, ^wildcard_search),
or_where: ilike(a.last_name, ^wildcard_search),
or_where: ilike(a.first_name, ^wildcard_search),
order_by: fragment("similarity(?, ?) DESC", b.title, ^wildcard_search)
)
end
def search(query, search_term) do
wildcard_search = "%#{search_term}%"
from(b in query,
join: a in assoc(b, :author),
where: fragment("? % ?", b.title, ^wildcard_search),
or_where: ilike(b.description, ^wildcard_search),
or_where: ilike(a.last_name, ^wildcard_search),
or_where: ilike(a.first_name, ^wildcard_search)
# removed order by here
)
end
您好,谢谢您的回复和建议。但是,我不知道如何编写排序功能。我的意思是有两种情况。首先,当我进入索引页面时,我想按价格/标题对图书目录/asc进行排序。其次,在我搜索一本书并显示所有匹配的结果后,我想按价格/标题对结果desc/asc进行排序。
def search(query, "") do # change there
wildcard_search = "%#{""}%"
from(b in query,
join: a in assoc(b, :author),
where: fragment("? % ?", b.title, ^wildcard_search),
or_where: ilike(b.description, ^wildcard_search),
or_where: ilike(a.last_name, ^wildcard_search),
or_where: ilike(a.first_name, ^wildcard_search),
order_by: fragment("similarity(?, ?) DESC", b.title, ^wildcard_search)
)
end
def search(query, search_term) do
wildcard_search = "%#{search_term}%"
from(b in query,
join: a in assoc(b, :author),
where: fragment("? % ?", b.title, ^wildcard_search),
or_where: ilike(b.description, ^wildcard_search),
or_where: ilike(a.last_name, ^wildcard_search),
or_where: ilike(a.first_name, ^wildcard_search)
# removed order by here
)
end