Prises de notes

**Ceci est une ancienne révision du document !**

Trier un tableau complexe

View

<table class="table table-sm table-striped my-2" id="table">
    <thead class="text-dark">
    <tr>
    <th class="col-sm align-top" scope="col"><%= sortable "name", t('batch.index.title.batch') %></th>
    <th class="col-sm align-top" scope="col"><%= sortable "tank", t('batch.index.title.tank') %></th>
    <th class="col-sm align-top" scope="col"><%= sortable "genotype", t('batch.index.title.genotype') %></th>
    <th class="col-sm align-top" scope="col"><%= sortable "species", t('batch.index.title.species') %></th>
    <th class="col-sm align-top" scope="col"><%= sortable "birth_date", t('batch.index.title.age') %></th>
    <th class="col-sm align-top" scope="col"><%= sortable "generation", t('batch.index.title.generation') %></th>
    <th class="col-sm align-top text-break" scope="col"><%= sortable "nb_male", t('batch.index.title.males') %></th>
    <th class="col-sm align-top text-break" scope="col"><%= sortable "nb_female", t('batch.index.title.females') %></th>
    <th class="col-sm align-top text-break" scope="col"><%= t('batch.index.title.individues') %></th>
    <th class="col-sm align-top" scope="col"><%= sortable "genetic_background", t('batch.index.title.genetic') %></th>
    <th class="col-sm align-top" scope="col"><%= sortable "user", t('batch.index.title.user') %></th>
    <th class="col-sm align-top" scope="col"><%= sortable "updated_at", t('batch.index.title.lastedit') %></th>
    <th class="col-sm align-top" scope="col"><%= sortable "exit_reason", t('batch.index.title.exit') %></th>
    <th class="col-sm align-top" scope="col"><%= t('batch.index.title.sanitary') %></th>
    <th class="col-sm align-top text-end" scope="col"><%= t('batch.index.actions') %></th>
    </tr>
    </thead>

Helper

module ApplicationHelper
  def sortable(column, title = nil)
    title ||= column.titleize
    css_class = if column != 'age'
                  column == sort_column ? "current #{sort_direction} text-dark" : 'sort text-dark'
                else
                  column == sort_column ? "current #{reverse_order(sort_direction)} text-dark" : 'sort text-dark'
                end
    direction = column == sort_column && sort_direction == 'asc' ? 'desc' : 'asc'
    link_to title, { sort: column, direction: direction }, { class: css_class }
  end
end

Controller

class BatchesController < ApplicationController
	before_action :check_profile
	before_action :set_batch, only: %i[show edit update destroy archive versions version revert]
	before_action :set_version, only: %i[version revert]
	before_action :check_manager, only: %i[destroy archive archives versions version revert]
 
	helper_method :sort_column, :sort_direction
 
	NB_ITEMS = ENV['ITEM_PER_PAGE']&.to_i || 25
	ALLOWED_SORTS = %w[name tank genotype species birth_date generation nb_male nb_female
genetic_background user updated_at exit_reason].freeze
	COLNAME = { 'genotype': 'genotypes.name', 'tank': 'tanks.rfid', 'species': 'species.name', 'user': 'users.uid' }.freeze
 
	def index
		@page = params.fetch(:page, 1).to_i
		@max_page = (Batch.count.to_f / NB_ITEMS).ceil
		@batches = Batch.where(is_archived: false)
				.includes(:species, :genotype, :tanks, { user: [:profile] })
				.order("#{request_colname} #{sort_direction}")
				.offset((@page - 1) * NB_ITEMS)
				.limit(NB_ITEMS)
	end
 
	def request_colname
		if %w[asc desc].include?(params[:direction])
			COLNAME.include?(params[:sort].to_sym) ? COLNAME[params[:sort].to_sym] : params[:sort]
		else
			'updated_at'
		end
	end
 
	def sort_column
		%w[asc desc].include?(params[:direction]) ? params[:sort] : 'updated_at'
	end
 
	def sort_direction
		ALLOWED_SORTS.include?(params[:sort]) ? params[:direction] : 'desc'
	end
end
ror/trier_tableau_complexe.1669887004.txt.gz · Dernière modification : de norore