如何使用jQuery到laravel if-else变量

发布于 2025-01-22 05:35:05 字数 1943 浏览 0 评论 0原文

我有一个约会项目。我们正在使用Laravel 6和JQuery 3.4.1

问题是我在接收Ajax时需要绘制DIV。

因此,JavaScript和刀片模板:

static countNewMessages() {
    $.get('/some/link/here', results => {
      let total = 0;
      if (results.length === 0) {
        $('.chat__list-block').each(function (index) {
          $(this).removeClass('chat__list-block_new');
        });
        $('.chat__list-non-read-counter').addClass('chat__list-non-read-counter_hidden').each(function (index) {
          $(this).text('');
        });
        $('#number-of-new-messages').addClass('d-none').removeClass('d-flex').html('');
        $('#inbox-messages-count-title').html('0');

        return false;
      }

      results.forEach(v => {
        if (Chat.containers?.threads) {
          let threadElement = $('.chat__list-block[data-pid=' + v.from_userid + ']');

          threadElement.addClass('chat__list-block_new');
          threadElement.find('.chat__list-non-read-counter')
            .addClass('chat__list-non-read-counter_hidden')
            .text(v.count);

          if (0 < threadElement.length && !threadElement.hasClass('chat__list-block_active') && 0 < v.count) {
            threadElement.find('.chat__list-non-read-counter')
              .removeClass('chat__list-non-read-counter_hidden');
          }
        }

        total += v.count;
        $('#number-of-new-messages').addClass('d-flex').removeClass('d-none').html(total);
        $('#inbox-messages-count-title').html(total);
      });
    });
  }
@if(count($threads))
<div>Chat requests</div>
@else
<div>No chat requests</div>
@endif

模板中的标准IF-ELSE行为非常适合我。如果用户访问页面但没有消息,则显示第二个块,如果他有消息,则将显示第一个块。但是,如果在块上“不聊天请求”并收到新消息的用户,则仅在完整的页面刷新后才渲染块“聊天请求”。

如果您需要更多信息,请告诉我

I have a dating project. We are using laravel 6 and jquery 3.4.1

The problem is that I need to draw a div when receiving AJAX.

So, javascript and blade template :

static countNewMessages() {
    $.get('/some/link/here', results => {
      let total = 0;
      if (results.length === 0) {
        $('.chat__list-block').each(function (index) {
          $(this).removeClass('chat__list-block_new');
        });
        $('.chat__list-non-read-counter').addClass('chat__list-non-read-counter_hidden').each(function (index) {
          $(this).text('');
        });
        $('#number-of-new-messages').addClass('d-none').removeClass('d-flex').html('');
        $('#inbox-messages-count-title').html('0');

        return false;
      }

      results.forEach(v => {
        if (Chat.containers?.threads) {
          let threadElement = $('.chat__list-block[data-pid=' + v.from_userid + ']');

          threadElement.addClass('chat__list-block_new');
          threadElement.find('.chat__list-non-read-counter')
            .addClass('chat__list-non-read-counter_hidden')
            .text(v.count);

          if (0 < threadElement.length && !threadElement.hasClass('chat__list-block_active') && 0 < v.count) {
            threadElement.find('.chat__list-non-read-counter')
              .removeClass('chat__list-non-read-counter_hidden');
          }
        }

        total += v.count;
        $('#number-of-new-messages').addClass('d-flex').removeClass('d-none').html(total);
        $('#inbox-messages-count-title').html(total);
      });
    });
  }
@if(count($threads))
<div>Chat requests</div>
@else
<div>No chat requests</div>
@endif

The standard if-else behavior in the template suits me fine. If a user visits the page but has no messages the second block is displayed, and if he has messages the first block is displayed. But if a user who is on the block "no chat requests" and receives new messages then the block "chat requests" is rendered only after a full refresh of the page.

If you need more information, please let me know

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天邊彩虹 2025-01-29 05:35:06

尝试以下操作:

@if(count($threads))
   <div data-threads-count="{{ count($threads) }}">Chat requests</div>
@else
   <div data-threads-count="{{ count($threads) }}">No chat requests</div>
@endif

现在您可以通过在jQuery ex中使用数据函数来访问线程计数:

$(selector).data('threads-count');

$(selector).attr('data-threads-count');

两个将返回线程计数

我希望它很有用

Try this :

@if(count($threads))
   <div data-threads-count="{{ count($threads) }}">Chat requests</div>
@else
   <div data-threads-count="{{ count($threads) }}">No chat requests</div>
@endif

Now you can access threads count by using data function in jquery ex :

$(selector).data('threads-count');

or

$(selector).attr('data-threads-count');

Both will return threads count

i hope it was useful ????

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文