Here I’m trying to add a filter for the “organisation” column . The profiles.js file pass neccessary inputs to the _status_table.erb file .
import { initDataTable, toggleProfileCoderIdField } from "./common/helpers"
function toggleSubmitProfileButton(updateLocked) {
if (updateLocked === true) {
$("#updateProfileSubmitButton").hide();
$("#updateProfileSubmitButtonDisabled").show();
} else {
$("#updateProfileSubmitButton").show();
$("#updateProfileSubmitButtonDisabled").hide();
}
}
function processCoderIdUpdate() {
var editProfileModal = $("#editProfileModal");
var editProfileModalForm = $("#editProfileModal form");
var submitEditProfileFormBtn = $("#updateProfileSubmitButton");
var coderIdInput = $("#editProfileModal [data-name=profileCoderIdInput]");
var updateRelatedRecordsDialogModal = $("#updateCoderIdModal");
var updateRelatedRecordsBtns = $("[data-name='update-related-records']");
var hiddenInputState = $("#profile_update_related_records");
function isCoderIdRequiredForSelectedRole() {
return !coderIdInput.is(":hidden");
}
function isCoderIdPresent() {
return coderIdInput.val().length > 0;
}
function coderIdChanged(storedCoderId) {
return (
isCoderIdPresent() &&
coderIdInput.val() &&
coderIdInput.val() !== storedCoderId
);
}
submitEditProfileFormBtn.on("click", function(e) {
var storedCoderId = coderIdInput.data("storedCoderId");
if (
isCoderIdRequiredForSelectedRole() &&
coderIdChanged(storedCoderId) &&
storedCoderId
) {
$(".profileCoderIdStoredValue").text(storedCoderId);
$(".profileCoderIdNewValue").text(coderIdInput.val());
editProfileModal.modal("hide");
updateRelatedRecordsDialogModal.modal("show");
updateRelatedRecordsBtns.on("click", function(e) {
hiddenInputState.val($(this).data("value"));
editProfileModalForm.submit();
});
} else {
hiddenInputState.val(false);
editProfileModalForm.submit();
}
});
}
export function toggleProfileModal(modalId) {
$(modalId)
.modal({ autofocus: false })
.modal("show");
$("[data-trigger='dropdown']").dropdown();
toggleProfileCoderIdField(modalId);
$(modalId + " [data-name=profileRoleIdInput]")
.off()
.on("change", function() {
toggleProfileCoderIdField(modalId);
});
}
function prepareEditProfilePath(profileId, userId) {
return "/users/" + userId + "/profiles/" + profileId + "/edit.json";
}
function addOptionsForSelect(selectId, options) {
var $select = $("#editProfileModal " + selectId);
$select.empty();
$.each(options, function(_key, object) {
$select.append(
$("<option></option>")
.attr("value", object.value)
.attr("selected", object.selected)
.text(object.text)
);
});
}
function populateCoderIdInput(coderId) {
$("#editProfileModal [data-name=profileCoderIdInput]").attr("value", coderId);
}
function updateFormUrl(profileId) {
var $editProfileModal = $("#editProfileModal form");
var currentUrl = $editProfileModal.attr("action");
var regexp = new RegExp(
currentUrl.substr(currentUrl.lastIndexOf("/") + 1) + "$"
);
var newUrl = currentUrl.replace(regexp, profileId);
$editProfileModal.attr("action", newUrl);
}
export function toggleEditProfileModal(element) {
$.ajax({
type: "GET",
contentType: "application/json",
url: prepareEditProfilePath(
element.dataset.profileId,
element.dataset.userId
),
success: function(result) {
updateFormUrl(element.dataset.profileId);
addOptionsForSelect(
"#profile_organisation_id",
result.organisationsForSelect
);
addOptionsForSelect("#profile_role_id", result.rolesForSelect);
populateCoderIdInput(result.profile.coderId);
toggleSubmitProfileButton(result.profile.updateLocked);
toggleProfileModal("#editProfileModal");
$("#editProfileModal [data-name=profileCoderIdInput]").attr(
"data-stored-coder-id",
result.profile.coderId
);
}
});
}
document.addEventListener("DOMContentLoaded", function() {
if (!document.querySelector(".profiles.index, .users.admin_edit")) return;
initDataTable({ tableId: "#profilesTable", orderVal: 1, targetsVal: [0, 4] });
processCoderIdUpdate();
$(".menu .item").tab();
initDataTable({
tableId: "#profilesStatusTable",
orderVal: 1,
sortingDirection: "asc",
targetsVal: [0, 7]
});
});
And here on the ERB file I added custom filter for the table and related script for its functionality. But t does not filter records, When I select a value It shows an empty table, Any help would be appreciated. Here’s the code I modified,
<form id="filterForm">
<div class="ui compact selection dropdown">
<input type="hidden" id="organisationFilter">
<i class="dropdown icon"></i>
<div class="default text"> Organisation</div>
<div class="menu">
<% @all_organisations.each do |all_organisation| %>
<div class="item" data-value="<%= all_organisation.name_or_code %>"><%= all_organisation.name_or_code %></div>
<% end %>
</div>
</div>
</form>
<table id="profilesStatusTable" class="ui celled table">
<%= render partial: 'profiles/table_actions' %>
<thead>
<tr>
<th>
<div class="ui checkbox">
<%= check_box_tag 'select_all', 1, false, class: "select-all", onclick: "toggleAll(this)" %>
<label></label>
</div>
</th>
<th>Full Name</th>
<th>Role</th>
<th>Email</th>
<th>Confirmed</th>
<th>Organisation</th>
<th>Coder ID</th>
<th>Created at</th>
<th>Last Updated</th>
<th>Last Login at</th>
<th>Login Count</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<% @profiles.each do |profile| %>
<tr class="<%= cycle('row', 'alt-row') %>">
<td><%= check_box_tag 'delete[]', profile.user.id %><label></label></td>
<td><%= profile.user.full_name %></td>
<td><%= role_as_string(profile.role_key) %></td>
<td><%= profile.user.email %></td>
<td>
<span class="ui <%= profile.user.confirmed ? 'teal' : 'red'%> empty circular label"></span>
</td>
<td class="organisation-name"><%= profile.organisation.name_or_code %></td>
<td><%= profile.coder_id.present? ? profile.coder_id : 'N/A' %></td>
<td class="created-at"><%= localise_and_pretty_print(profile.user.created_at) %></td>
<td class="last-updated-at"><%= localise_and_pretty_print(profile.user.updated_at) %></td>
<td class="last-login-at"><%= localise_and_pretty_print(profile.user.last_login_at) %></td>
<td><%= profile.user.login_count %></td>
<td>
<%= link_to admin_edit_user_path(profile.user), class: "icon-action", title: "Edit" do %>
<i class="edit icon"></i>
<% end %>
</td>
</tr>
<% end %>
</tbody>
</table>
<div class="clear-float"></div>
<table id="episode_header_fixed" class="ui table episode-header-fixed"></table>
<div class="clear-float"></div>
<div class="ui grid display-block">
<div class="sixteen wide column">
<div class="ui grid">
<div class="eight wide column">
<%= link_to excel_numerator_report_reports_path, class: "download-excel-button", id: "user_report_download_excel_link" do %>
<span class="ui basic red button">Download</span>
<% end %>
</div>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.4.1/semantic.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.17.0/xlsx.full.min.js"></script>
<style>
.ui.compact.dropdown {
width: 160px;
}
</style>
<script>
document.addEventListener('DOMContentLoaded', function() {
// Get the filter dropdown and table rows
var filterDropdown = document.querySelector('.ui.selection.dropdown');
var tableRows = document.querySelectorAll('#profilesStatusTable tbody tr');
// Initialize the filter dropdown
$(filterDropdown).dropdown({
onChange: function(value, text) {
// Loop through the table rows and hide/show based on the filter value
tableRows.forEach(function(row) {
var organisationName = row.querySelector('.organisation-name').textContent.toLowerCase();
if (value === '' || organisationName.includes(value.toLowerCase())) {
row.style.display = '';
} else {
row.style.display = 'none';
}
});
}
});
// Show all table rows initially
tableRows.forEach(function(row) {
row.style.display = '';
});
// Add an event listener to the form for submit event
document.getElementById('filterForm').addEventListener('submit', function(event) {
event.preventDefault();
});
});
</script>
And to get the organisation names to the dropdown I included this in the profiles_controller
def index
authorize :profile
set_organisation_considering_role
@profiles = policy_scope(Profile).filtering(by_organisation: params[:organisation_id])
.includes(:user, notifications: :degrees)
.preload(:organisation)
@grouped_notifications = @profiles.joins(notifications: :degrees)
.group(:id, :"notifications.kind", :"degrees.kind")
.count
@all_organisations = Organisation.all.order(name: :asc) // The line I added
end