我创建了一个复选框列,用户可以在其中选择要在表上删除的多个项目。像这样,但只有一个按钮“批量删除”:
这是我的代码:
<%= render "shared/nav_dashboard" %>
<%= render "shared/header_dashboard" %>
<section>
<div class="container-fluid">
<!-- Page Header-->
<header>
<h1 class="h3 display">Pages</h1>
</header>
<div class="row">
<div class="col-lg-12">
<div class="card">
<div class="card-header">
<h4>List of Pages</h4>
</div>
<div class="card-body">
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th><%= link_to "Bulk Delete", page_path(page), method: :delete, data: { confirm: "Are you sure?" }%></th>
<th>Id</th>
<th>Title</th>
<th>Summary</th>
<th>Date Created</th>
<th colspan="3">Action</th>
</tr>
</thead>
<tbody>
<% @pages.each do |page| %>
<tr>
<td><input type="checkbox" value="false"> </td>
<td scope="row"><%= page.id %></td>
<td><%= page.title %></td>
<td><%= page.body.truncate(60) %></td>
<td> <%= page.created_at.strftime("%B %d, %Y") %> </td>
<td><%= link_to "Show", page_path(page)%></td>
<td><%= link_to "Edit", edit_page_path(page)%></td>
<td><%= link_to "Delete", page_path(page), method: :delete, data: { confirm: "Are you sure?" }%></td>
</tr>
<% end %>
</tbody>
</table>
</div>
</div>
</div>
</div>
</section>
<%= render "shared/footer_dashboard" %>
我想知道为什么它不起作用?
如何逐步执行此功能?
答案 0 :(得分:1)
您可以使用表格。看起来像这样(我使用的是simpleform,但这将适用于您喜欢的每个表单标签助手):
在routes.rb
中:
resources :pages do
collection do
delete :bulk_destroy
end
end
您认为:
<%= simple_form_for :pages, url: "/pages/bulk_destroy", method: :delete do |f| %>
<ul>
<% @pages.each do |page| %>
<li> <%= page.name %> <input type="checkbox" name="collection_ids[]" value="<%= page.id %>" /> </li>
<% end %>
</ul>
<%= f.submit %>
<% end %>
在您的控制器中:
class PagesController < ApplicationController
def bulk_destroy
Page.where(id: params[:collection_ids]).destroy_all
end
end
繁重的工作为您服务:https://github.com/joshmn/active_action或在https://github.com/joshmn/bulk_actions_poc上查看最基本的概念证明
答案 1 :(得分:1)
如果您不想编写其他单独的方法,则可以尝试以下操作,只需确保选择了多个ID时,它应作为params [:ids]发送,而不是params [:id] < / p>
public NotificationCompat.Builder buildLocalNotification(Context context, PendingIntent pendingIntent, AppointmentModel appointmentModel) {
String channelId = "Appoinment Reminder";
Log.e("@@ChannelId", channelId);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channelId)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Appoinment Reminder")
.setContentText("Your appointment with "+appointmentModel.getContact() +" at "+ appointmentModel.getDate1()+" "+appointmentModel.getTime())
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
@SuppressLint("WrongConstant") NotificationChannel channel = new NotificationChannel(channelId, "Appoinment Reminder", NotificationManager.IMPORTANCE_DEFAULT);
manager.createNotificationChannel(channel);
}
manager.notify(NotificationID.getID(), builder.build());
return builder;
}
intentToRepeat.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivities(context, NotificationID.getID(), new Intent[] {backIntent, intentToRepeat}, PendingIntent.FLAG_UPDATE_CURRENT);
buildLocalNotification(context, pendingIntent, appointmentModelList.get(i)).build();
答案 2 :(得分:0)
您可能需要根据您的jquery版本更改语法
$('#bulk_delete').on('click', function(e){
var selectedIds = [];
$("input[type='checkbox']:checked").each(function(e){
ids.push($(this).data('id'))
})
$.ajax({url: 'your action url',type: 'DELETE', data: {ids: selectedIds}})
})