Add batch delete feature.

This commit is contained in:
BoHung Chiu 2022-05-22 20:12:32 +08:00
parent 1649b3e618
commit 943ee2b35e
3 changed files with 96 additions and 2 deletions

View File

@ -426,7 +426,11 @@ class Admin::AnnouncementsController < OrbitAdminController
Bulletin.notify_feed_delete(params[:ids])
end
end
redirect_to admin_announcements_path
if request.xhr?
render :nothing => true, :status => 204
else
redirect_to admin_announcements_path
end
end
def preview

View File

@ -1,10 +1,96 @@
<script>
if(document.querySelectorAll("#orbit-bar").length==0) location.reload();
if( Array.prototype.delete == undefined ){
Array.prototype.delete = function(v){
var idx = this.indexOf(v);
if(idx != -1){
return this.splice(idx, 1)[0];
}else{
return null;
}
}
}
function reload_tbody(url){
console.log("done");
if(url == undefined){
url = window.location.href;
}
$.get(url).done(function(data){
$('#index_table tbody').replaceWith($(data).find('tbody'));
if(window.scroll_top){
$(window).scrollTop(window.scroll_top);
}
window.archive_delete_ids = [];
$(".delete_archives_btn").addClass('hide');
});
}
$(document).on("click", ".select_delete", function(){
var that = $(this);
var id = that.data("id");
if(window.archive_delete_ids == undefined){
window.archive_delete_ids = [];
}
if(that.prop("checked")){
window.archive_delete_ids.push(id);
}else{
window.archive_delete_ids.delete(id);
}
if(window.archive_delete_ids.length == 0){
$(".delete_archives_btn").addClass('hide');
}else{
$(".delete_archives_btn").removeClass('hide');
}
})
function delete_archives(){
if(!window.archive_delete_ids || window.archive_delete_ids.length == 0){
return null;
}
var data = new FormData();
var csrf_token = document
.querySelector("meta[name='csrf-token']")
.getAttribute("content");
var csrf_param = document
.querySelector("meta[name='csrf-param']")
.getAttribute("content");
if (csrf_token && csrf_param) {
data.append(csrf_param, csrf_token);
}
window.archive_delete_ids.forEach(function(val){
data.append('ids[]', val);
});
$.ajax({
url: "<%=admin_announcements_delete_path%>",
method: 'DELETE',
data: data,
headers: {
// 'Content-Type': 'multipart/form-data',
// 使用 multipart/form-data 在此不需要設定 Content-Type。
'X-Requested-With': 'XMLHttpRequest',
'Authorization': `Bearer ${ csrf_token }`,
},
contentType: false, //required
processData: false, // required
statusCode: {
204: reload_tbody,
403: function() {
alert( "Delete failed!" );
}
}
})
}
$(document).on("click", ".delete_archives_btn", function(){
if(window.confirm("<%=t(:sure?)%>")){
if(window.confirm("<%=t(:sure?)%>")){
delete_archives();
}
}
})
</script>
<table class="table main-list">
<thead>
<tr class="sort-header">
<th style="width: 1em;"></th>
<% @table_fields.each do |f| %>
<%= thead(f) %>
<% end %>
@ -13,6 +99,8 @@
<tbody>
<% @bulletins.each do |b| %>
<tr>
<% is_editable = can_edit_or_delete?(b) %>
<td><% if is_editable %><input type="checkbox" class="select_delete" data-id="<%= b.uid %>"><% end %></td>
<td>
<%= b.status_for_table %>
</td>
@ -50,7 +138,7 @@
<div class="quick-edit">
<ul class="nav nav-pills">
<li><a href="#" class="detail-row" onclick="$('#<%= "#{b.id.to_s}-detail" %>').slideToggle(300); return false;"><%= t(:detail) %></a></li>
<% if can_edit_or_delete?(b) %>
<% if is_editable %>
<li><a href="/admin/announcements/<%=b.id.to_s%>/edit"><%= t(:edit) %></a></li>
<li><a href="#" class="delete text-error" rel="/admin/announcements/<%=b.id.to_s%>"><%= t(:delete_) %></a></li>
<% end %>
@ -99,6 +187,7 @@
<%=
content_tag :div, class: "bottomnav clearfix" do
content_tag(:div, paginate(@bulletins), class: "pagination pagination-centered") +
content_tag(:div, link_to(t("archive.delete_selected"), "javascript:void(0)", :class=>"btn btn-primary"), class: "pull-right hide delete_archives_btn") +
content_tag(:div, link_to(t(:new_),new_admin_announcement_path, :class=>"btn btn-primary"), class: "pull-right")
end
%>

View File

@ -32,6 +32,7 @@ Rails.application.routes.draw do
patch 'announcement/updatesettings', to: 'announcements#updatesettings'
post 'announcement/import_from_wp', to: 'announcements#import_from_wp'
post 'announcement/generate_iframe_url' => 'announcements#generate_iframe_url'
delete 'announcement/delete', to: 'announcements#delete'
resources :announcements
get 'announcements/:id/comment'=> 'announcements#comment'
get 'annc-comment-hidden/:id' => 'announcements#comment_hidden'