简体   繁体   English

使用FontAwesome更改文本时如何“刷新”(重新渲染)按钮?

[英]How to “refresh” (re-render) button when text change while using FontAwesome?

I have the following UI: 我有以下用户界面:

在此处输入图片说明

and from there I need to change dynamically the button Archive and Unarchive as well as the icon from FontAwesome depending on the action taken and the response from the backend. 然后根据需要执行的操作和来自后端的响应,从那里动态更改按钮ArchiveUnarchive以及FontAwesome中的图标。 The response basically looks like: 响应基本上如下所示:

{"row_id":"12518","status":true}

In a few words: 简而言之:

  • In the blue row (archived item): if I click on Unarchive button and the response is true I should: remove the background color, change the button text to Archive , change the button class from unarchive-form to archive-form and change the FontAwesome icon class from fa fa-arrow-circle-up to fa fa-arrow-circle-down . 在蓝色行(已存档的项目)中:如果我单击“ Unarchive按钮并且响应为true ,则应该:删除背景色,将按钮文本更改为“ Archive ,将按钮类从“ unarchive-form archive-form更改为“ archive-form然后将FontAwesome图标类,从fa fa-arrow-circle-upfa fa-arrow-circle-down
  • In the other row (non archived item): if I click on Archive and the response is true then I should: add the blue background, change the button text to Unarchive , change the button class from archive-form to unarchive-form and change the FontAwesome icon class from fa fa-arrow-circle-down to fa fa-arrow-circle-up . 在另一行(非归档项目)中:如果单击“ Archive并且响应为true则应该:添加蓝色背景,将按钮文本更改为“ Unarchive ,将按钮类从archive-form unarchive-form更改为“ unarchive-form然后更改从fa fa-arrow-circle-downfa fa-arrow-circle-up的FontAwesome图标类。

I have almost everything covered (and I will show the sources for just one case since it's pretty much the same and I don't want to make a big post): 我几乎涵盖了所有内容(我将仅显示一种情况的来源,因为它几乎相同,并且我不想发表大量文章):

$.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");
});

But the problem comes when I change the text for the FontAwesome since I get the plain text instead of the icon. 但是问题出在我更改FontAwesome的文本时,因为我得到的是纯文本而不是图标。 For example clicking on Unarchive in the first row will become into this: 例如,单击第一行中的“ Unarchive ”将变为:

在此处输入图片说明

As you can see there everything is fine but the FontAwesome icon. 如您所见,除了FontAwesome图标之外,一切都很好。 Is there any way to refresh the button so the changes takes effect? 有什么方法可以刷新按钮,使更改生效? Or do you know any other way to achieve this? 还是您知道实现此目标的其他方法?

You don't have to change the text like you are doing now, use .html() instead and replace only necessary strings, this way you maintain the structure of the button's content. 您不必像现在一样更改文本,而只需使用.html()并仅替换必要的字符串,就可以维护按钮内容的结构。

Try this: 尝试这个:

// 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;
    }
});

As per my understanding you only need the functionality for Archieve and UnArchieve, 据我了解,您只需要Archieve和UnArchieve的功能,

Could you please try the following script 您可以尝试以下脚本吗

$(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;
            }
        });

    }

});

I hope this will solve your problem. 我希望这能解决您的问题。

Let me know if you need further details. 让我知道您是否需要更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ReactJS:按钮(跨度)视觉应在单击时重新呈现和更改,但不会(使用 setState) - ReactJS: button (span) visual should re-render and change when clicked, but doesnt (using setState) 如何刷新/重新渲染 OpenLayers 6 Map - How to refresh/re-render an OpenLayers 6 Map 如何在使用分页时重新渲染 Redux 表单 - How to re-render the Redux forms while using pagination 在 React 上使用 sockets 时如何防止重新渲染? - How to prevent re-render when using sockets on React? 在 React 上重新渲染文本 - Re-render text on React 如何在单选按钮更改 JS/AJAX 上仅重新呈现特定数据 - How to re-render only specific data on radio button change JS/AJAX 使用 useState 挂钩时,我的 2D 网格不会重新呈现。 有没有办法在我改变里面元素的 object 属性后重新渲染? - my 2D grid does not re-render while using with useState hook. Is there any way to re-render after I change object attributes of the elements inside? 使用带有 graphql 的反应钩子时,按钮元素中的 onClick 不会触发反应表中的重新渲染 - onClick in button element not triggering re-render in react-table when using react hooks with graphql Function 单击按钮时不重新渲染 - Function does not re-render when clicking button Function 组件在我更改 state 时重新渲染 - Function components re-render when I change the state
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM