在Ruby on Rails中向表格添加筛选器 – JavaScript

huangapple go评论73阅读模式
英文:

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(&quot;DOMContentLoaded&quot;, function() {
//existing code 
if (!document.querySelector(&quot;.profiles.index, .users.admin_edit&quot;)) return;
initDataTable({ tableId: &quot;#profilesTable&quot;, orderVal: 1, targetsVal: [0, 4] });
processCoderIdUpdate();
$(&quot;.menu .item&quot;).tab();
initDataTable({
tableId: &quot;#profilesStatusTable&quot;,
orderVal: 1,
sortingDirection: &quot;asc&quot;,
targetsVal: [0, 7]
});
// I added this below custom event listner for get the value from other file 
$(&quot;#organizationFilterDropdown&quot;).dropdown();
// Attach the change event listener
$(&quot;#organizationFilterDropdown&quot;).on(&quot;change&quot;, function() {
var selectedOrganizationId = $(this).dropdown(&quot;get value&quot;);
// Get the table instance
var profilesStatusTable = $(&quot;#profilesStatusTable&quot;).DataTable();
// Clear any existing filters
profilesStatusTable.columns().search(&quot;&quot;).draw();
// Apply the filter to the &quot;Organisation&quot; column
profilesStatusTable
.columns(5)
.search(selectedOrganizationId, false, false)
.draw();
});

and in the _status_table.html.erb I added this dropdown section

&lt;div class=&quot;ui fluid labeled search selection dropdown&quot; id=&quot;organizationFilterDropdown&quot; data-selected-organization=&quot;&lt;%= @selected_organization %&gt;&quot;&gt;
&lt;input type=&quot;hidden&quot; id=&quot;organizationFilterInput&quot;&gt;
&lt;i class=&quot;dropdown icon&quot;&gt;&lt;/i&gt;
&lt;div class=&quot;default text&quot;&gt;Filter by Organization&lt;/div&gt;
&lt;div class=&quot;menu&quot;&gt;
&lt;% @organizations.each do |organization| %&gt;
&lt;div class=&quot;item&quot; data-value=&quot;&lt;%= organization.id %&gt;&quot;&gt;&lt;%= organization.name_or_code %&gt;&lt;/div&gt;
&lt;% end %&gt;
&lt;/div&gt;
&lt;/div&gt;

Here's the rest of the _status_table.html.erb

&lt;table id=&quot;profilesStatusTable&quot; class=&quot;ui celled table&quot;&gt;  
&lt;%= render partial: &#39;profiles/table_actions&#39; %&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;
&lt;div class=&quot;ui checkbox&quot;&gt;
&lt;%= check_box_tag &#39;select_all&#39;, 1, false, class: &quot;select-all&quot;, onclick: &quot;toggleAll(this)&quot; %&gt;
&lt;label&gt;&lt;/label&gt;
&lt;/div&gt;
&lt;/th&gt;
&lt;th&gt;Full Name&lt;/th&gt;
&lt;th&gt;Role&lt;/th&gt;
&lt;th&gt;Email&lt;/th&gt;
&lt;th&gt;Confirmed&lt;/th&gt;
&lt;th&gt;Organisation&lt;/th&gt;
&lt;th&gt;Coder ID&lt;/th&gt;
&lt;th&gt;Created at&lt;/th&gt;
&lt;th&gt;Last Updated&lt;/th&gt;
&lt;th&gt;Last Login at&lt;/th&gt;
&lt;th&gt;Login Count&lt;/th&gt;
&lt;th&gt;Actions&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;% @profiles.each do |profile| %&gt;
&lt;tr class=&quot;&lt;%= cycle(&#39;row&#39;, &#39;alt-row&#39;) %&gt;&quot;&gt;
&lt;td&gt;&lt;%= check_box_tag &#39;delete[]&#39;, profile.user.id %&gt;&lt;label&gt;&lt;/label&gt;&lt;/td&gt;
&lt;td&gt;&lt;%= profile.user.full_name %&gt;&lt;/td&gt;
&lt;td&gt;&lt;%= role_as_string(profile.role_key) %&gt;&lt;/td&gt;
&lt;td&gt;&lt;%= profile.user.email %&gt;&lt;/td&gt;
&lt;td&gt;
&lt;span class=&quot;ui &lt;%= profile.user.confirmed ? &#39;teal&#39; : &#39;red&#39;%&gt; empty circular label&quot;&gt;&lt;/span&gt;
&lt;/td&gt;
&lt;td&gt;&lt;%= profile.organisation.name_or_code %&gt;&lt;/td&gt;
&lt;td&gt;&lt;%= profile.coder_id.present? ? profile.coder_id : &#39;N/A&#39; %&gt;&lt;/td&gt;
&lt;td&gt;&lt;%= localise_and_pretty_print(profile.user.created_at) %&gt;&lt;/td&gt;
&lt;td&gt;&lt;%= localise_and_pretty_print(profile.user.updated_at) %&gt;&lt;/td&gt;
&lt;td&gt;&lt;%= localise_and_pretty_print(profile.user.last_login_at) %&gt;&lt;/td&gt;
&lt;td&gt;&lt;%= profile.user.login_count %&gt;&lt;/td&gt;
&lt;td&gt;
&lt;%= link_to admin_edit_user_path(profile.user), class: &quot;icon-action&quot;, title: &quot;Edit&quot; do %&gt;
&lt;i class=&quot;edit icon&quot;&gt;&lt;/i&gt;
&lt;% end %&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;% end %&gt;
&lt;/tbody&gt;
&lt;/table&gt;

The issue is when I select a value from dropdown it shows empty table, I did enter console.log(&quot;selectedOrganizationId&quot;); 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(&quot;.profiles.index, .users.admin_edit&quot;)) return; is hiding them from you, as I cannot reproduce your issue. Example.

document.addEventListener(&quot;DOMContentLoaded&quot;, function() {
profilesStatusTable = new DataTable(&quot;#profilesStatusTable&quot;,{
tableId: &quot;#profilesStatusTable&quot;,
orderVal: 1,
sortingDirection: &quot;asc&quot;,
targetsVal: [0, 7]
});
$(&quot;#organizationFilterDropdown&quot;).dropdown(
{onChange: function() {
var selectedOrganizationId = $(this).dropdown(&quot;get value&quot;);
// Apply the filter to the &quot;Organisation&quot; column
profilesStatusTable.column(5).search(selectedOrganizationId).draw();
}})
});

Note:

  • Since initDataTable is also not a function defined anywhere I used the standard syntax for DataTables.
  • I chose to pass the onChange function to dropdown 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

&lt;div class=&quot;item&quot; data-value=&quot;&lt;%= organization.id %&gt;&quot;&gt;

to

&lt;div class=&quot;item&quot; data-value=&quot;&lt;%= organization.name_or_code %&gt;&quot;&gt;

huangapple
  • 本文由 发表于 2023年7月13日 20:15:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76679273.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定