我有以下用户界面:
从那里我需要动态更改按钮Archive
和Unarchive
以及FontAwesome中的图标,具体取决于所采取的操作和后端的响应。响应基本上如下:
{"row_id":"12518","status":true}
用几句话说:
Unarchive
按钮并且回复为true
我应该:删除背景颜色,将按钮文本更改为Archive
,将按钮类从unarchive-form
更改为archive-form
,并将FontAwesome图标类从fa fa-arrow-circle-up
更改为fa fa-arrow-circle-down
。Archive
并且回复为true
,那么我应该:添加蓝色背景,将按钮文字更改为Unarchive
,将按钮类从archive-form
更改为unarchive-form
,并将FontAwesome图标类从fa fa-arrow-circle-down
更改为fa fa-arrow-circle-up
。我几乎覆盖了所有内容(我将只展示一个案例的来源,因为它几乎相同,而且我不想做一个大帖子):
$.ajax({
url: '/ajax/forms/archive',
type: 'POST',
dataType: 'json',
data: {
form_id: ids
}
}).done(function (response) {
$.each(response, function (index, value) {
var this_row_tr = $('input[data-form=' + value.row_id + ']').closest('tr'),
this_btn = this_row_tr.find('.archive-form');
if (value.status === true) {
// here I add the background color to the TR
this_row_tr.addClass('info');
// here I swap the button class
this_btn.removeClass('archive-form').addClass('unarchive-form');
// here I swap the button text and also the whole FontAwesome part
this_btn.text('<i class="fa fa-arrow-circle-up" aria-hidden="true"></i> Unarchive');
} else if (value.status === false) {
this_row_tr.addClass('error');
all_archived = false;
}
});
if (all_archived) {
toastr["success"]("The forms were archived successfully.", "Information");
} else {
toastr["error"]("Something went wrong, the forms could not be archived.", "Error");
}
}).error(function (response) {
console.log(response);
toastr["error"]("Something went wrong, the forms could not be archived.", "Error");
});
但是当我更改FontAwesome的文本时问题出现了,因为我得到了纯文本而不是图标。例如,点击第一行中的Unarchive
将成为:
正如你所看到的,一切都很好,但FontAwesome图标。有没有办法刷新按钮,以便更改生效?或者你知道其他任何方法来实现这个目标吗?
答案 0 :(得分:1)
您不必像现在这样更改文字,而是使用.html()
代替必要的字符串,这样就可以维护按钮内容的结构。
试试这个:
// this one is for the Archive action
$.each(response, function (index, value) {
var this_row_tr = $('input[data-form=' + value.row_id + ']').closest('tr');
var archiveToUnarchive = this_row_tr.find('.btn[class*="archive"]');
if (value.status === true) {
this_row_tr.addClass('info');
archiveToUnarchive.toggleClass('archive-form unarchive-form')
.html(archiveToUnarchive.html()
.replace('Archive', 'Unarchive')
.replace('fa-arrow-circle-down', 'fa-arrow-circle-up'));
} else if (value.status === false) {
this_row_tr.addClass('error');
all_deleted = false;
}
});
// this one is for the Unarchive action
$.each(response, function (index, value) {
var this_row_tr = $('input[data-form=' + value.row_id + ']').closest('tr');
var unarchiveToArchive = this_row_tr.find('.btn[class*="archive"]');
if (value.status === true) {
unarchiveToArchive.toggleClass('archive-form unarchive-form')
.html(unarchiveToArchive.html()
.replace('Unarchive', 'Archive')
.replace('fa-arrow-circle-up', 'fa-arrow-circle-down'));
this_row_tr.removeClassName('info');
} else if (value.status === false) {
this_row_tr.addClass('error');
all_deleted = false;
}
});
答案 1 :(得分:0)
根据我的理解,您只需要Archieve和UnArchieve的功能,
请你试试下面的剧本
$(document).ready(function(){
$(".unarchive-form").click(unArchieveToArchieve);
$(".archive-form").click(archieveUnArchieve);
function unArchieveToArchieve(){
var btn = $(this);
var rowId = $(this).parent().parent().parent().find(".to-upload").attr("data-form");
var response = [{"row_id":rowId,"status" :true}];
$.each(response, function (index, value) {
var this_row_tr = $('input[data-form=' + value.row_id + ']').closest('tr');
if (value.status === true) {
this_row_tr.attr('class','');
$(btn).text("Archieve");
$(btn).removeClass("unarchive-form");
$(btn).addClass("archive-form");
$(btn).click(archieveUnArchieve)
} else if (value.status === false) {
this_row_tr.attr('class','error');
all_deleted = false;
}
});
}
function archieveUnArchieve(){
var btn = $(this);
var rowId = $(this).parent().parent().parent().find(".to-upload").attr("data-form");
var response = [{"row_id":rowId,"status" :true}];
$.each(response, function (index, value) {
var this_row_tr = $('input[data-form=' + value.row_id + ']').closest('tr');
if (value.status === true) {
this_row_tr.attr('class','info');
$(btn).text("Unarchive");
$(btn).removeClass("archive-form");
$(btn).addClass("unarchive-form");
$(btn).click(unArchieveToArchieve)
} else if (value.status === false) {
this_row_tr.attr('class' , 'error');
all_deleted = false;
}
});
}
});
我希望这能解决你的问题。
如果您需要更多详细信息,请与我们联系。