How to loop foreach in blade while using jquery in Laravel - html

I'm using Laravel 5.4 and I have never tried jquery so I have no idea what I'm doing, unfortunately.
This is my blade:
<h3>Comments:</h3>
<ul class="list-unstyled activity-list">
#foreach ($threads->comments()->where('parent', 0)->orderBy('created_at', 'desc')->get() as $comment)
<li>
<img src="{{ $comment->user->getAvatar() }}" alt="Avatar" class="img-circle pull-left avatar">
<p><a href="{{ route('profile.view', $comment->user->id) }}">
{{ $comment->user->user_name }}</a>:
<br>
{{ $comment->content }}
<span class="timestamp">{{ $comment->created_at->diffForHumans() }}</span>
</p>
#if ($threads->lock == 0)
<a id="btn-comment" style="padding-left:3.5em">reply</a>
<br>
<form action="{{ route('threads.comments.add') }}" id="comment" method="POST" style="padding-left:3.5em; display:none;">
{{ csrf_field() }}
<input type="hidden" name="thread_id" value="{{ $threads->id }}">
<input type="hidden" name="parent" value="{{ $comment->id }}">
<input type="text" name="content" class="form-control" placeholder="Add reply...">
<input type="submit" class="btn btn-primary btn-xs" value="Add Reply">
</form>
<br>
#endif
#foreach ($comment->childs as $child)
<div style="padding-left:3.5em;">
<img src="{{ $child->user->getAvatar() }}" alt="Avatar" class="img-circle pull-left avatar">
<p><a href="{{ route('profile.view', $child->user->id) }}">
<span style="padding-left:1em;">{{ $child->user->user_name }}</a>: </span>
<br>
<span style="padding-left:1em;">{{ $child->content }}</span>
<span class="timestamp" style="padding-left:4.3em;">{{ $child->created_at->diffForHumans() }}</span>
</p>
</div>
#endforeach
</li>
#endforeach
</ul>
Here's my custom script:
<script>
$(document).ready(function(){
$('#btn-comment').click(function(){
$('#comment').toggle('slide');
});
});
</script>
These lines specifically are the ones that I'm trying to run using the script above:
#if ($threads->lock == 0)
<a id="btn-comment" style="padding-left:3.5em">reply</a>
<br>
<form action="{{ route('threads.comments.add') }}" id="comment" method="POST" style="padding-left:3.5em; display:none;">
{{ csrf_field() }}
<input type="hidden" name="thread_id" value="{{ $threads->id }}">
<input type="hidden" name="parent" value="{{ $comment->id }}">
<input type="text" name="content" class="form-control" placeholder="Add reply...">
<input type="submit" class="btn btn-primary btn-xs" value="Add Reply">
</form>
<br>
#endif
Ask me if you need further detail

Related

Modal in Modal Bootstrap 5 CSS

I make modal in the modal, time in the press of the modal button in the modal, do not appear the intended capital, even blank
Has anyone ever done this and the solution?
Transaksi
<h5><i></i> Create Transaction </h5>
<i></i> Transaction
#csrf
<i></i> Member</a>
<button type="button" class="btn btn-sm btn-primary " data-bs-toggle="modal" data-bs-target="#choosePackageModal">
<i class="bi bi-plus"></i> Paket</a>
</button>
</div>
<div class="mb-3">
<label for="transaction_date" class="form-label">Tanggal Transaksi</label>
<input type="date" name="date" value="{{ date('Y-m-d') }}" class="form-control #error('date') is-invalid #enderror" id="date">
#error('date')
<div class="invalid-feedback">
{{ $message }}
</div>
#enderror
</div>
<div class="mb-3">
<label for="deadline" class="form-label">Estimasi Selesai</label>
<input type="date" name="deadline" value="{{ date('Y-m-d', strtotime(date('Y-m-d') . ' + 3 day')) }}" class="form-control #error('deadline') is-invalid #enderror">
#error('deadline')
<span class="invalid-feedback">
{{ $message }}
</span>
#enderror
</div>
<div class="mb-3">
<label for="additional_cost" class="form-label">Biaya Tambahan</label>
<input type="number" name="additional_cost" value="{{ old('additional_cost') }}" class="form-control #error('additional_cost') is-invalid #enderror">
#error('additional_cost')
<span class="invalid-feedback">
{{ $message }}
</span>
#enderror
</div>
<div class="mb-3">
<label for="paid" class="form-label">Pembayaran</label>
<input type="number" name="paid" value="{{ old('paid') }}" class="form-control #error('paid') is-invalid #enderror">
#error('paid')
<span class="invalid-feedback">
{{ $message }}
</span>
#enderror
</div>
<div class="mb-3">
<label for="status" class="form-label">Status Bayar</label>
<select class="form-select form-select mb-3" aria-label=".form-select example" name="status">
<option selected>-- Select status --</option>
<option value="Sudah Dibayar">Bayar Sekarang</option>
<option value="Belum Dibayar">Belum Bayar</option>
</select>
</div>
<button type="submit" class="btn btn-sm btn-primary"><i class="bi bi-plus"></i> Create</button>
</form>
</div>
</div>
</div>
<h5><i></i> Pilih Member </h5>
<i></i> Member
#
Nama Pelanggan
Alamat
Gender
No. Telp
Pilih
#php
$no = 1;
#endphp
#foreach ($members as $member)
{{ $no++; }}
{{ $member->name }}
{{ $member->address }}
{{ $member->gender }}
{{ $member->phone }}
<i></i>
#endforeach
My hope is that when the + Member button is clicked, then the intended capital appears, but when I click, the page becomes blank

How can I prevent new line when I am using loop and form?

How can I prevent new line when I am using loop and form.I have tried style="display: inline-block;" but it didn't work. I would want buttons to be in one line ,next to each other.
<div>
#foreach (range(1, 12) as $item)
#if(App\Models\Seats::find($item))
<form method="post" action="{{ route('seats.destroy', $item) }}">
#csrf
#method('delete')
<input type="hidden" name="id" value="{{ $item }}"/>
<input type="hidden" name="row_seats" value="4"/>
<input type="hidden" name="seat_id" value="{{ $item }}"/>
<button type="submit" style="margin-left:10px; width:60px;" class="btn btn-danger">{{$item}}</button>
</form>
#else
<form method="post" action="/seats">
#csrf
<input type="hidden" name="id" value="{{ $item }}"/>
<input type="hidden" name="row_seats" value="4"/>
<input type="hidden" name="seat_id" value="{{ $item }}"/>
<button type="submit" style="width:60px; margin-left:10px;" class="btn btn-success">{{ $item}}</button>
</form>
#endif
#endforeach
</div>
I found decision of the problem this is my code if someone have the same problem. I used div with with display:flex
<div style="margin-left:90px;" class="row pt-1 pb-1 ">
<div style="display: flex;">
4 Ред
#foreach (range(1, 12) as $item)
#if(App\Models\Seats::find($item))
<form style="display: inline-block;" method="post" action="{{ route('seats.destroy', $item) }}">
#csrf
#method('delete')
<!-- <input type="hidden" name="user_id" value="{{Auth::user()->id}}"/> -->
<input type="hidden" name="id" value="{{ $item }}"/>
<input type="hidden" name="row_seats" value="4"/>
<input type="hidden" name="seat_id" value="{{ $item }}"/>
<button type="submit" style="margin-left:10px; width:80px; display:inline;" class="btn btn-danger">{{$item}}</button>
</form>
#else
<form style="display: inline;" method="post" action="/seats">
#csrf
<!-- <input type="hidden" name="user_id" value="{{Auth::user()->id}}"/> -->
<input type="hidden" name="id" value="{{ $item }}"/>
<input type="hidden" name="row_seats" value="4"/>
<input type="hidden" name="seat_id" value="{{ $item }}"/>
<button type="submit" style="width:80px; margin-left:10px;" class="btn btn-success">{{ $item}}</button>
</form>
#endif
#endforeach
</div>
</div>

HTML page, load 10 items, then wait for scroll and load more

I have a list of so many items that loading them all at the time is very uncomfortable and takes a long time. I would like to implement a script that loads only 10 items and waits for scroll to load more. However I have no idea how to code it.
This is how my page looks:
{% extends "layout.html" %}
{% block content %}
<div class="container">
{% for i in range(data|length) %}
<div class="re-container">
<div class="re-container-item">
<div class="re-item">
<image class="re-img" src="{{url_for('static', filename='img/' + data.UNIQUE_RE_NUMBER[i].replace('/', '-') + '.jpg')}}" onerror="if (this.src != 'static/img/default.jpg') this.src = 'static/img/default.jpg';"></image>
</div>
</div>
<div class="re-container-item">
<div class="re-item">
<p>{{ data.LOCATION[i] }}</p>
<p class="re-date"> {{ data.MIN_DATE[i] }} - {{ data.MAX_DATE[i] }} </p>
</div>
<div class="re-item">
<p>{{ data.ADDRSS[i] }}</p>
</div>
<div class="re-item">
<p><a class="article-title" href="{{url_for('/dashapp/', ure=data.UNIQUE_RE_NUMBER[i])}}">{{data.UNIQUE_RE_NUMBER[i]}}</a></p>
<p class="re-price">{{data.RE_PRICE[i]}}</p>
<hr>
</div>
<div class="re-item">
<p>{{data.RE_INFO[i]}}</p>
</div>
{% if current_user.is_authenticated %}
<div class="re-item">
<form action="{{ url_for('saved') }}" method="POST">
<input type="hidden" value="{{ data.LOCATION[i] }}" name="location">
<input type="hidden" value="{{ data.UNIQUE_RE_NUMBER[i] }}" name="unique">
<input type="hidden" value="{{ data.ADDRSS[i] }}" name="address">
<input type="hidden" value="{{ data.RE_INFO[i] }}" name="info">
<input type="hidden" value="{{ data.RE_HREF[i] }}" name="href">
<input type="hidden" value="{{ data.TYP_ID[i] }}" name="typ">
<input type="hidden" value="{{ data.RE_POLOHA[i] }}" name="poloha">
<input type="hidden" value="{{ data.RE_DRUH[i] }}" name="druh">
<input type="hidden" value="{{ data.RE_TYP[i] }}" name="re-typ">
<input type="hidden" value="" name="dispozice">
<input type="hidden" value="" name="f_druh">
<input type="hidden" value="" name="pplocha">
<input type="hidden" value="" name="plocha">
<input type="hidden" value="" name="l_druh">
<input type="hidden" value="" name="site">
<input type="hidden" value="{{ data.MIN_DATE[i] }}" name="min_d">
<button class="re-button" type="submit">Save</button>
</form>
</div>
{% endif %}
<div class="re-item">
<p>Poloha domu/vily: {{data.RE_POLOHA[i]}}</p>
</div>
<div class="re-item">
<p>Druh domu/vily: {{data.RE_DRUH[i]}}</p>
</div>
<div class="re-item">
<p>Typ domu/vily: {{data.RE_TYP[i]}}</p>
</div>
<div class="re-item">
{% if data.RE_HREF[i] != None %}
More here
{% endif %}
</div>
</div>
</div>
{% endfor %}
</div>
{% endblock content %}
Does anyone have some suggestion?
You can use the loading attribute in HTML. Just like below.
<img src="niceCats.jpg" loading="lazy">
I would advise you to read this aricle that CSSTrick put up on their page. Note this attribute is not supported on any safari version for IOS.

Post comments in Django Homepage

i have this code with me as part of a tutorial i was following for blog post. I have a comment section on my detailview, I want it to be shown in the homepage as well. I tried with some codes, but the problem is same comments are showing in all posts (only) in homepage.
please take a look at the code.
I have a comment snippet which I use it in my homepage.
<!-- Comment showing section -->
<div class="main-comment-section">
<div class="container-fluid mt-2">
<div class="form-group row">
<!-- Comment Form -->
<form class="comment-form" method="post" action=".">{% csrf_token %}
<div class="form-group">
<textarea name="content" cols="60" rows="2" maxlength="1000" required="" id="id_content"></textarea>
</div>
<button type="submit" value="submit" class="btn-sm btn-outline-light" style="color: black;">Comment</button>
</form>
<!-- Comment Form end -->
</div>
</div>
{{ comments.count }} Comment{{ comments|pluralize }}
{% for comment in comments %}
<blockquote class="blockquote">
<img style="float:left; clear: left;" class="rounded-circle article-img" height="10" width="10" src="{{ comment.user.profile.profile_pic.url }}"><h6>{{ comment.user.first_name|capfirst }} {{ comment.user.last_name|capfirst }}</h6><br>
<p style="font-size: 8px;">{{ comment.timestamp }}</p>
<p style="font-size: 14px;" class="mb-3">{{ comment.content }}</p>
<a type="button" name="button" class="reply-btn ml-4"><p style="font-size: 13px;"> Reply</p></a>
{% if request.user == comment.user %}
Delete</td>
{% endif %}
</blockquote>
{{ comment.reply.count }}
<div class="replied-comments col-md-5" style="display: none;">
{% for reply in comment.replies.all %} <!--replies is the related name in the model-->
<blockquote class="blockquote">
<img style="float:left; clear: left;" class="rounded-circle article-img" height="50" width="50" src="{{ reply.user.profile.profile_pic.url }}"><h6>{{ reply.user.first_name|capfirst }} {{ reply.user.last_name|capfirst }}</h6><br>
<p style="font-size: 13px;" class="mb-3">{{ reply.content }}</p>
</blockquote>
{% endfor %}
<div class="form-group row">
<form class="reply-form" method="post" action=".">{% csrf_token %}
<input type="hidden" name="comment_id" value="{{ comment.id }}">
<div class="form-group">
<textarea name="content" cols="60" rows="2" maxlength="1000" required="" id="id_content"></textarea>
</div>
<input type="submit" value="submit" class="btn-sm btn-outline-light" style="color: black;">
</form>
</div>
</div>
{% endfor %}
</div>
PS. The comment is correctly showing in the detail view, but in homepage its showing all the comments of all posts in every posts.
Have the following changes,
views.py
...
comments = Comment.objects.filter(post=posts_list.first()) # Delete this line
context = {
'page':page,
'posts':posts,
'tag':tag,
'actions':actions,
'comments': comments, # Delete this too
}
return render(request, 'posts/users/myhome.html', context)
Change posts/comment_section.html too,
<!-- Comment showing section -->
<div class="main-comment-section">
<div class="container-fluid mt-2">
<div class="form-group row">
<!-- Comment Form -->
<form class="comment-form" method="post" action=".">{% csrf_token %}
<div class="form-group">
<textarea name="content" cols="60" rows="2" maxlength="1000" required="" id="id_content"></textarea>
</div>
<button type="submit" value="submit" class="btn-sm btn-outline-light" style="color: black;">Comment</button>
</form>
<!-- Comment Form end -->
</div>
</div>
{% if not post.comment_set.all %}
No comments to display.
{% endif %}
{% for comment in post.comment_set.all %}
<blockquote class="blockquote">
<img style="float:left; clear: left;" class="rounded-circle article-img" height="10" width="10" src="{{ comment.user.profile.profile_pic.url }}"><h6>{{ comment.user.first_name|capfirst }} {{ comment.user.last_name|capfirst }}</h6><br>
<p style="font-size: 8px;">{{ comment.timestamp }}</p>
<p style="font-size: 14px;" class="mb-3">{{ comment.content }}</p>
<a type="button" name="button" class="reply-btn ml-4"><p style="font-size: 13px;"> Reply</p></a>
{% if request.user == comment.user %}
Delete</td>
{% endif %}
</blockquote>
{{ comment.reply.count }}
<div class="replied-comments col-md-5" style="display: none;">
{% for reply in comment.replies.all %} <!--replies is the related name in the model-->
<blockquote class="blockquote">
<img style="float:left; clear: left;" class="rounded-circle article-img" height="50" width="50" src="{{ reply.user.profile.profile_pic.url }}"><h6>{{ reply.user.first_name|capfirst }} {{ reply.user.last_name|capfirst }}</h6><br>
<p style="font-size: 13px;" class="mb-3">{{ reply.content }}</p>
</blockquote>
{% endfor %}
<div class="form-group row">
<form class="reply-form" method="post" action=".">{% csrf_token %}
<input type="hidden" name="comment_id" value="{{ comment.id }}">
<div class="form-group">
<textarea name="content" cols="60" rows="2" maxlength="1000" required="" id="id_content"></textarea>
</div>
<input type="submit" value="submit" class="btn-sm btn-outline-light" style="color: black;">
</form>
</div>
</div>
{% endfor %}
</div>

How should the Dom be in this jQuery code?

I have this HTML:
<div class="cart-plus-minus">
<input class="cart-plus-minus-box quantity" id="{{ $item->id }}" name="quantity[{{ $i }}]" value="{{ $item->quantity }}" type="text">
<input name="product_id[{{ $i }}]" value="{{ $item->id }}" type="hidden">
<div class="dec qtybutton" data-id="{{ $item->id }}">
<i class="zmdi zmdi-chevron-down"></i>
</div>
<div class="inc qtybutton" data-id="{{ $item->id }}">
<i class="zmdi zmdi-chevron-up"></i>
</div>
</div>
I have this jQuery code that it does not work:
$('div.cart-plus-minus > div.dec').on('click', function () {
var id = $(this).attr('data-id');
alert(id);
});
How should the dom be If I want to click on dec button and it displays an alert?
I have tried all and it does not work yet.
Thanks!