Skip to content

Commit

Permalink
Add check is_985, is_211, is_good when search.
Browse files Browse the repository at this point in the history
  • Loading branch information
zw963 committed May 1, 2024
1 parent b693b3a commit 138d481
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
20 changes: 18 additions & 2 deletions src/actions/universities/index.cr
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
class Universities::Index < BrowserAction
get "/universities" do
q = params.get?(:q)
is_985 = params.get?(:is_985)
is_211 = params.get?(:is_211)
is_good = params.get?(:is_good)

builder = Avram::QueryBuilder.new(table: :universities)
unless q.blank?
builder = builder.where(Avram::Where::Raw.new("name &@~ ?", q))
builder = builder.where(Avram::Where::Raw.new("(universities.name &@~ ?", q))
builder = builder.or do |or|
or.where(Avram::Where::Raw.new("description &@~ ?", q))
or.where(Avram::Where::Raw.new("universities.description &@~ ?)", q))
end
end

results = UniversityQuery.new
results.query = builder

unless is_985.blank?
results = results.is_985(true)
end

unless is_211.blank?
results = results.is_211(true)
end

unless is_good.blank?
results = results.is_good(true)
end

pages, universities = paginate(results.id.desc_order, per_page: 50)

html IndexPage, universities: universities, pages: pages
Expand Down
19 changes: 14 additions & 5 deletions src/components/universities/checkbox.cr
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
class Universities::CheckBox < BaseComponent
needs attribute : Avram::PermittedAttribute(Bool)
needs id : String
needs name : String
needs description : String
needs full_path : String

def render
label for: id, class: "s12 m8 input-field" do
checkbox(attribute, "false", "true", id: id)
label for: name, class: "m4 input-field" do
input(
type: "checkbox",
name: name,
value: "true",
id: name,
"hx-get": Index.path,
"hx-target": "#main",
"hx-select": "#main",
"hx-push-url": "true",
"hx-include": "[name='q']"
)
span description
end
mount Shared::FieldErrors, attribute
end
end
8 changes: 8 additions & 0 deletions src/pages/universities/index_page.cr
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,18 @@ class Universities::IndexPage < MainLayout
"hx-select": "#main",
"hx-trigger": "search, keyup delay:400ms changed",
"hx-push-url": "true",
"hx-include": "[name='is_985'],[name='is_211'],[name='is_good']"
)
submit("搜索", class: "btn")
end
end

div class: "row" do
full_path = context.request.resource
mount CheckBox, "is_985", "仅显示985", full_path
mount CheckBox, "is_211", "仅显示211", full_path
mount CheckBox, "is_good", "显示包含双一流专业高校", full_path
end
end

def render_universities
Expand Down

0 comments on commit 138d481

Please sign in to comment.