英文:
Adding a filter to a table - ruby on rails (Javascript)
问题
这里我试图为"organisation"列添加一个过滤器。这使用datatables库来处理表格。profiles.js
文件将必要的输入传递给_status_table.erb
文件。
在profiles.js
文件中,我添加了以下额外的代码:
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]
});
// 我在下面添加了自定义事件监听器以获取来自其他文件的值
$("#organizationFilterDropdown").dropdown();
// 附加更改事件监听器
$("#organizationFilterDropdown").on("change", function() {
var selectedOrganizationId = $(this).dropdown("get value");
// 获取表实例
var profilesStatusTable = $("#profilesStatusTable").DataTable();
// 清除任何现有的过滤器
profilesStatusTable.columns().search("").draw();
// 应用到"Organisation"列的过滤器
profilesStatusTable
.columns(5)
.search(selectedOrganizationId, false, false)
.draw();
});
});
在_status_table.html.erb
中,我添加了下拉选择框部分:
<div class="ui fluid labeled search selection dropdown" id="organizationFilterDropdown" data-selected-organization="<%= @selected_organization %>">
<input type="hidden" id="organizationFilterInput">
<i class="dropdown icon"></i>
<div class="default text">按组织筛选</div>
<div class="menu">
<% @organizations.each do |organization| %>
<div class="item" data-value="<%= organization.id %>"><%= organization.name_or_code %></div>
<% end %>
</div>
</div>
这是_status_table.html.erb
的其余部分:
<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>全名</th>
<th>角色</th>
<th>电子邮件</th>
<th>已确认</th>
<th>组织</th>
<th>编码器 ID</th>
<th>创建时间</th>
<th>上次更新</th>
<th>上次登录时间</th>
<th>登录次数</th>
<th>操作</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><%= profile.organisation.name_or_code %></td>
<td><%= profile.coder_id.present? ? profile.coder_id : 'N/A' %></td>
<td><%= localise_and_pretty_print(profile.user.created_at) %></td>
<td><%= localise_and_pretty_print(profile.user.updated_at) %></td>
<td><%= 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: "编辑" do %>
<i class="edit icon"></i>
<% end %>
</td>
</tr>
<% end %>
</tbody>
</table>
问题在于,当我从下拉菜单中选择一个值时,它会显示空表格。我在onchange()
函数中添加了console.log("selectedOrganizationId");
,它打印出了正确的值。
我正在参考此链接中的部分并实施上述代码,如果可以的话,请帮我解决这个问题。
英文:
Here I'm trying to add a filter for the "organisation" column. This uses datatables library to handle the tables The profiles.js
file pass neccessary inputs to the _status_table.erb
file .
in profiles.js
file I added these extra lines ,
document.addEventListener("DOMContentLoaded", function() {
//existing code
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]
});
// I added this below custom event listner for get the value from other file
$("#organizationFilterDropdown").dropdown();
// Attach the change event listener
$("#organizationFilterDropdown").on("change", function() {
var selectedOrganizationId = $(this).dropdown("get value");
// Get the table instance
var profilesStatusTable = $("#profilesStatusTable").DataTable();
// Clear any existing filters
profilesStatusTable.columns().search("").draw();
// Apply the filter to the "Organisation" column
profilesStatusTable
.columns(5)
.search(selectedOrganizationId, false, false)
.draw();
});
and in the _status_table.html.erb
I added this dropdown section
<div class="ui fluid labeled search selection dropdown" id="organizationFilterDropdown" data-selected-organization="<%= @selected_organization %>">
<input type="hidden" id="organizationFilterInput">
<i class="dropdown icon"></i>
<div class="default text">Filter by Organization</div>
<div class="menu">
<% @organizations.each do |organization| %>
<div class="item" data-value="<%= organization.id %>"><%= organization.name_or_code %></div>
<% end %>
</div>
</div>
Here's the rest of the _status_table.html.erb
<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><%= profile.organisation.name_or_code %></td>
<td><%= profile.coder_id.present? ? profile.coder_id : 'N/A' %></td>
<td><%= localise_and_pretty_print(profile.user.created_at) %></td>
<td><%= localise_and_pretty_print(profile.user.updated_at) %></td>
<td><%= 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>
The issue is when I select a value from dropdown it shows empty table, I did enter console.log("selectedOrganizationId");
inside onchange() function
and it prints the correct value.
I was referring to this section and implemented above code, please help me if you can
答案1
得分: 1
profilesStatusTable
没有引用,所以如果您实际执行此操作,它将引发TypeError: profilesStatusTable.column is not a function
错误。
由于您说没有错误,可能是这个守卫条件if (!document.querySelector(".profiles.index, .users.admin_edit")) return;
正在隐藏错误,因为我无法重现您的问题。示例。
document.addEventListener("DOMContentLoaded", function() {
profilesStatusTable = new DataTable("#profilesStatusTable",{
tableId: "#profilesStatusTable",
orderVal: 1,
sortingDirection: "asc",
targetsVal: [0, 7]
});
$("#organizationFilterDropdown").dropdown(
{onChange: function() {
var selectedOrganizationId = $(this).dropdown("get value");
// 将过滤器应用于“Organization”列
profilesStatusTable.column(5).search(selectedOrganizationId).draw();
}})
});
注意:
- 由于
initDataTable
也没有在任何地方定义为函数,我使用了DataTables
的标准语法。 - 我选择将
onChange
函数传递给dropdown
,因为我认为它看起来更清晰,更好地包含了相关的逻辑;但是,您的实现方式也会完全相同。
更新
除了上述问题外,我稍后注意到,OP正在尝试将orginazation.id
用作搜索参数,但是正在针对profile.organisation.name_or_code
进行搜索,所以扩展的解决方案也是更改
<div class="item" data-value="<%= organization.id %>">
为
<div class="item" data-value="<%= organization.name_or_code %>">
英文:
profilesStatusTable
has no reference so if you were actually getting to this action it would raise TypeError: profilesStatusTable.column is not a function
.
Since you are saying there are no errors it is possible that this guard clause if (!document.querySelector(".profiles.index, .users.admin_edit")) return;
is hiding them from you, as I cannot reproduce your issue. Example.
document.addEventListener("DOMContentLoaded", function() {
profilesStatusTable = new DataTable("#profilesStatusTable",{
tableId: "#profilesStatusTable",
orderVal: 1,
sortingDirection: "asc",
targetsVal: [0, 7]
});
$("#organizationFilterDropdown").dropdown(
{onChange: function() {
var selectedOrganizationId = $(this).dropdown("get value");
// Apply the filter to the "Organisation" column
profilesStatusTable.column(5).search(selectedOrganizationId).draw();
}})
});
Note:
- Since
initDataTable
is also not a function defined anywhere I used the standard syntax forDataTables
. - I chose to pass the
onChange
function todropdown
because it IMO it looks cleaner and contains the associated logic better; however, your implementation would work exactly the same.
UPDATE
Along with the above issues I later noted that the OP is trying to use orginazation.id
as a search parameter but is searching against the profile.organisation.name_or_code
so the extended solution was also to change
<div class="item" data-value="<%= organization.id %>">
to
<div class="item" data-value="<%= organization.name_or_code %>">
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论