1
0
mirror of https://github.com/jcwimer/wrestlingApp synced 2026-03-31 19:45:45 +00:00

Removed the use of datatables and added pagination and search on all_matches.

This commit is contained in:
2026-02-19 17:53:40 -05:00
parent 8670ce38c3
commit dc50efe8fc
8 changed files with 190 additions and 189 deletions

View File

@@ -5,7 +5,6 @@ import "@hotwired/turbo-rails";
import { createConsumer } from "@rails/actioncable"; // Import createConsumer directly
import "jquery";
import "bootstrap";
import "datatables.net";
// Stimulus setup
import { Application } from "@hotwired/stimulus";
@@ -39,9 +38,9 @@ application.register("match-spectate", MatchSpectateController);
}
}).call(this);
console.log("Propshaft/Importmap application.js initialized with jQuery, Bootstrap, Stimulus, and DataTables.");
console.log("Propshaft/Importmap application.js initialized with jQuery, Bootstrap, and Stimulus.");
// If you have custom JavaScript files in app/javascript/ that were previously
// handled by Sprockets `require_tree`, you'll need to import them here explicitly.
// For example:
// import "./my_custom_logic";
// import "./my_custom_logic";

View File

@@ -116,9 +116,59 @@ class TournamentsController < ApplicationController
end
def matches
@matches = @tournament.matches
.includes({ wrestler1: :school }, { wrestler2: :school }, { weight: :matches })
.order(:bout_number)
per_page = 50
@page = params[:page].to_i > 0 ? params[:page].to_i : 1
offset = (@page - 1) * per_page
matches_table = Match.arel_table
matches_scope = @tournament.matches.order(:bout_number)
if params[:search].present?
wrestlers_table = Wrestler.arel_table
schools_table = School.arel_table
search_terms = params[:search].downcase.split
search_terms.each do |term|
escaped_term = ActiveRecord::Base.sanitize_sql_like(term)
pattern = "%#{escaped_term}%"
matching_wrestler_ids = Wrestler
.joins(:weight)
.left_outer_joins(:school)
.where(weights: { tournament_id: @tournament.id })
.where(
wrestlers_table[:name].matches(pattern)
.or(schools_table[:name].matches(pattern))
)
.distinct
.select(:id)
term_scope = @tournament.matches.where(w1: matching_wrestler_ids)
.or(@tournament.matches.where(w2: matching_wrestler_ids))
if term.match?(/\A\d+\z/)
term_scope = term_scope.or(@tournament.matches.where(bout_number: term.to_i))
end
matches_scope = matches_scope.where(id: term_scope.select(:id))
end
end
@total_count = matches_scope.count
@total_pages = (@total_count / per_page.to_f).ceil
@per_page = per_page
loser1_not_bye = matches_table[:loser1_name].not_eq("BYE").or(matches_table[:loser1_name].eq(nil))
loser2_not_bye = matches_table[:loser2_name].not_eq("BYE").or(matches_table[:loser2_name].eq(nil))
non_bye_scope = matches_scope.where(loser1_not_bye).where(loser2_not_bye)
@matches_without_byes_count = non_bye_scope.count
@unfinished_matches_without_byes_count = non_bye_scope.where(finished: [nil, 0]).count
@matches = matches_scope
.includes({ wrestler1: :school }, { wrestler2: :school }, { weight: :matches })
.offset(offset)
.limit(per_page)
if @match
@w1 = @match.wrestler1
@w2 = @match.wrestler2

View File

@@ -1,11 +1,5 @@
<% if @tournaments.size > 0 %>
<h3>My Tournaments</h3>
<script>
// $(document).ready(function() {
// $('#tournamentList').dataTable();
// pagingType: "bootstrap";
// } );
</script>
<table class="table table-hover" id="tournamentList">
<thead>
<tr>

View File

@@ -1,13 +1,15 @@
<h1>All <%= @tournament.name %> matches</h1>
<script>
$(document).ready(function() {
$('#matchesList').dataTable();
pagingType: "bootstrap";
} );
</script>
</br>
<% matches_path = "/tournaments/#{@tournament.id}/matches" %>
<%= form_tag(matches_path, method: :get, id: "search-form") do %>
<%= text_field_tag :search, params[:search], placeholder: "Search wrestler, school, or bout #" %>
<%= submit_tag "Search" %>
<% end %>
<p>Search by wrestler name, school name, or bout number.</p>
<br>
<table class="table table-striped table-bordered table-condensed" id="matchesList">
<thead>
<tr>
@@ -35,6 +37,49 @@
<% end %>
</tbody>
</table>
<% if @total_pages.present? && @total_pages > 1 %>
<nav aria-label="Matches pagination">
<ul class="pagination">
<% if @page > 1 %>
<li class="page-item">
<%= link_to "Previous", { controller: "tournaments", action: "matches", id: @tournament.id, page: @page - 1, search: params[:search] }, class: "page-link" %>
</li>
<% else %>
<li class="page-item disabled"><span class="page-link">Previous</span></li>
<% end %>
<% window = 5
left = [1, @page - window / 2].max
right = [@total_pages, left + window - 1].min
left = [1, right - window + 1].max
%>
<% (left..right).each do |p| %>
<% if p == @page %>
<li class="page-item active"><span class="page-link"><%= p %></span></li>
<% else %>
<li class="page-item"><%= link_to p, { controller: "tournaments", action: "matches", id: @tournament.id, page: p, search: params[:search] }, class: "page-link" %></li>
<% end %>
<% end %>
<% if @page < @total_pages %>
<li class="page-item">
<%= link_to "Next", { controller: "tournaments", action: "matches", id: @tournament.id, page: @page + 1, search: params[:search] }, class: "page-link" %>
</li>
<% else %>
<li class="page-item disabled"><span class="page-link">Next</span></li>
<% end %>
</ul>
</nav>
<p class="text-muted">
<% start_index = ((@page - 1) * @per_page) + 1
end_index = [@page * @per_page, @total_count].min
%>
Showing <%= start_index %> - <%= end_index %> of <%= @total_count %> matches
</p>
<% end %>
<br>
<p>Total matches without byes: <%= @matches.select{|m| m.loser1_name != 'BYE' and m.loser2_name != 'BYE'}.size %></p>
<p>Unfinished matches: <%= @matches.select{|m| m.finished != 1 and m.loser1_name != 'BYE' and m.loser2_name != 'BYE'}.size %></p>
<p>Total matches without byes: <%= @matches_without_byes_count %></p>
<p>Unfinished matches: <%= @unfinished_matches_without_byes_count %></p>

View File

@@ -1,8 +1,3 @@
<script>
// $(document).ready(function() {
// $('#matchList').dataTable();
// } );
</script>
<script>
const setUpMatchesRefresh = () => {
if (window.__upMatchesRefreshTimeout) {