I have the following templates in Flask (but I don't think that's a flask problem:
Contact.html
<button type="button" class="btn btn-secondary btn-sm" data-target="#score" data-toggle="collapse" data-placement="right">Score</button>
<div id="score" class="collapse">
<span class='dot green'></span> 1
</div>
Compare.html
<!-- I only added the key elements there are some other "formatting" parts.-->
{% include "contact.html" %}
{% include "contact.html" %}
As you see in Contact.html I use a collpase. Ref: https://www.w3schools.com/bootstrap/bootstrap_collapse.asp
My problem is that I compare two contacts and therefore they both have that div with id score.
If I now click on the first Button to reveal the score of the first contact then obviously both scores are revealed. But I only want to reveal the score of the contact I clicked.
My idea is instead of data-target="#score" to say something like: Take the next #score element but I don't know how I can do that.
I figure it out how to it. Basically it's easy in my case.
Because each contact has an unique id, I can use this to build the id like this:
<!-- Generate ID -->
{% set score_id = "score" + contact.idx|string %}
<!-- Use the ID in the collapse-->
<button type="button" class="btn btn-secondary btn-sm" data-target="#{{score_id}}" data-toggle="collapse" data-placement="right">Score</button>
<div id="{{score_id}}" class="collapse">
<span class='dot {{color}}'></span> {{ contact_score }}
</div>
Then only one will expanded if clicked on the Button. Therefore thanks #Anees Ijaz for the comment. The comment pushed me in the right direction.
Related
Using Bootstrap5, Django and Crispy Forms to create a calculator application.
Application works as expected however I want to make changes to the appearance of the forms.
I've already removed the required field asterisk by simply adding the following to the CSS file:
.asteriskField {
display: none;
}
But now I want to place the label inside the field which requires a different approach that I'm currently unable to understand despite extensive research.
Having used Inspect in Chrome I can see that the label is of course a separate element but one housed within the field to achieve the given effect.
Current Form
Desired result
Settings.py (excerpt)
CRISPY_TEMPLATE_PACK = 'bootstrap5'
Forms.py (excerpt)
class InvestmentForm(forms.Form):
starting_amount = forms.ChoiceField(choices=STARTING_AMOUNT)
deposit_amount = forms.FloatField()
trade_in_value = forms.FloatField()
number_of_years = forms.FloatField()
credit_score = forms.ChoiceField(choices=CREDIT_SCORE)
state = forms.ChoiceField(
label='State',
choices=[(zipcode, zipcode) for zipcode in StateTax.objects.values_list('zipcode', flat=True).distinct()]
)
Index.html (excerpt)
<div class="row justify-content-center">
<h5 class="text-center"> Enter details below to see your estimate</h5>
</div>
<div class="col-md-8 mx-auto">
<form action="" method="POST">
{% csrf_token %}
{{ form|crispy }}
<br>
<button class="btn btn-primary" type="submit">Calculate</button>
</form>
<br>
</div>
</div>
You could use crispy-bootstrap5 which introduces floating labels. Just follow the installation guidelines and add a floating input field in your crispy Layout like:
FloatingField("first_name"),
i have a django template that uses for loop to print comments i want to show the input field and a button when the edit link is clicked how do i do that.
and also when the edit button is pressed i wanna get the value from that specific input field. how do i do that?
{% for comment in comments %}
<div class="comment mdl-color-text--grey-700">
<header class="comment__header">
<img src="images/co1.jpg" class="comment__avatar">
<div class="comment__author">
<strong>{{comment.User_name}}</strong>
<span>{{comment.date}}</span>
</div>
</header>
<div class="comment__text">
{{comment.text}}
</div>
<!-- FAB button , class "mdl-button--fab"-->
<a href="javascript:delete_comment('{{comment.text}}','{{comment.blogslug}}')">
<button class="mdl-button mdl-js-button mdl-button--fab">
<i class="material-icons">delete</i>
</button>
</a>
<!--when this link is clicked bellow edit_comment div should appear -->
<a href="">
<button class="mdl-button mdl-js-button mdl-button--fab">
<i class="material-icons">edit</i>
</button>
</a>
<!-- and also when i click the edit button how can i get value from the input -->
<div id="edit_comment" style="visibility: hidden;">
<input type="text" name="edit_comment">
<button>edit</button>
</div>
</div>
{% endfor %}
so the problem is there are going to many other comments of this type because they are printed using a loop.
Firstly, why are you wrapping your button in an <a> tag? Unnecessary.
Get rid of visibility: hidden. Use display: none or a class like Bootstrap's d-none.
I suggest d-none because it allows you to add the class and remove it without worrying about the inherited display property, i.e. display: flex or display: block.
https://getbootstrap.com/docs/4.0/utilities/display/
Write a custom function with an event listener.
Inside your template loop:
<button class="mdl-button mdl-js-button mdl-button--fab" onclick="handleClick(this)">
<i class="material-icons">edit</i>
</button>
ASSUMING YOU HAVE REMOVED THE <a> TAG
JavaScript:
let handleClick = function(param) {
let editCommentDiv = param.parentNode.lastChild;
editCommentDiv.style.display = "none"
};
This is by no means the best way to do it. I highly suggest you use BootStrap's d-none class. Also, what you should actually do is assign an ID based on the for loop to both the button and to the div, i.e. id=editcommentdiv_{{ forloop.counter }} this way you won't need to use DOM to navigate to the last element to get the edit div, and can just target the div directly via ID.
In some Angular 7 template I have two button (click) nested. The second one is unclikable : the first one only answer the (click).
How may I set the second button clickable ?
This html code comes from an Angular 5 course : https://openclassrooms.com/fr/courses/4668271-developpez-des-applications-web-avec-angular/5091266-creez-une-application-complete-avec-angular-et-firebase
<button
class="list-group-item"
*ngFor="let book of books; let id = index"
(click)="onViewBook(id)">
<h3 class="list-group-item-heading">
Titre : {{ book.title }}
<!-- ignored code starts here -->
<button class="btn btn-default pull-right"
(click)="onDeleteBook(book)">
<span class="glyphicon glyphicon-minus"></span>
</button>
<!-- end of ignored code -->
</h3>
<p class="list-group-item-text">
Auteur : {{ book.author }}</p>
</button>
Nesting buttons is not valid in HTML like commented before by #ConnorsFan.
Instead, change the first button to div. This way your UX + UI will work perfectly.
<div
class="list-group-item"
*ngFor="let book of books; let id = index"
(click)="onViewBook(id)">
<h3 class="list-group-item-heading">
Titre : {{ book.title }}
<!-- ignored code starts here -->
<button class="btn btn-default pull-right"
(click)="onDeleteBook(book)">
<span class="glyphicon glyphicon-minus"></span>
</button>
<!-- end of ignored code -->
</h3>
<p class="list-group-item-text">
Auteur : {{ book.author }}</p>
</div>
HTML:
<button type="button" class="btn btn-primary">Notifications
<span class="badge badge-light"></span>
</button>
python:
def notif():
not_num = False
if count > 0:
return count
else
return not_num
def count():
return 8
Can I get a way to call "notif" function into HTML body?
What I want is to do an "if" inside an HTML button that tells me how many notifications I have and if I don´t have any, it doesn´t show me anything.
You probably need to use built-in if template tag.
Something like:
{% if notifications %}
<button type="button" class="btn btn-primary">Notifications ({{ notifications|length }})
<span class="badge badge-light"></span>
</button>
{% else %}
No notifications.
{% endif %}
This way it will show you the button if you have notifications.
For more info read this
You can also create custom template tags for more complex problems, read this
urls.py contains the line:
url(r'^topics/$', views.topics, name='topics'),
views.py contains the code:
def topics(request):
context = locals()
return render(request, 'topics.html', context)
The following bootstrap code is used to open "topics.html"
<p><a class="btn btn-warning" href="/topics/" role="button">More »</a></p>
The above code renders "topics.html" correctly which is a table.
Now, I want the cursor to go to a specific row( Ex: first row) in the table.
It's not clear for me what cursor you mean, but may be you can assign a unique id to each row like
<tr id="row-1"><tr id="row-2"><tr id="row-3"><tr id="row-4">
then you can jump to that id using
<a class="btn btn-warning" href="/topics/#row-1" role="button"></a>
<a class="btn btn-warning" href="/topics/#row-2" role="button"></a>
...
it may look a little bit basic, but with the few clues you set i think it could be a start...