Bootstrap - Table Within Modal Within Table - django

I'm using Django with Bootstrap and have a problem with a modal window. I have a table and within that I have one column where the cell is a link to the modal window. The modal window should then display another table relevant to the cell you selected.
The problem is I'm trying to keep the modal div inside of a for loop because that's how I am determining which row of the table has been clicked. I've read a little on modals and seen other people have a problem when their modal div was inside table tags, so I think that's where my issue might be coming from.
At the moment, the main table displays properly and the modal window opens when i click on the cell as intended. It takes the information from the for loop properly.
However, when I put a table within the modal div it doesn't display in the modal window, it displays underneath the main table whether the modal has been activated or not. The modal window then only displays the header and footer with an empty body. If I get rid of the table inside the modal and use something like p's instead it works but I would prefer to use the table.
If I move the modal div outside of where it is then it works with the table in the modal window, but the problem is I need it within the for loop because that's how I know which row of the table has been clicked.
Can anyone tell me if there is a better way of doing this than what I am doing, like passing a variable based on which row has been picked if possible, or if there is a way to keep the modal div where it is and still display the table in the modal window?
The HTML is below.
<table class="weekly table-hover main-manager-display" style="text-align: center; width: 100%">
<thead>
</thead>
<tbody style="color: white">
{% for week in team_selection %}
<tr>
{% for item in week|slice:":10" %}
<td>{{ item }}</td>
{% endfor %}
<td><a data-toggle="modal" href="#maxModal" style="color: white">{{ week.10 }}</a></td>
<td>{{ week.11 }}</td>
</tr>
<div class="modal fade" id="maxModal" role="dialog">
<div class="modal-dialog">
<div class="modal-content" style="background-color: #c4dfe6">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title" style="text-align: center; color: #1c4d66">Week {{ week.0 }} Best Possible Team</h4>
</div>
<div class="modal-body">
<table class="table modal-table">
<thead>
<tr>
<th>Position</th>
<th>Player</th>
<th>Points</th>
</tr>
</thead>
<tbody>
<tr>
<td>Test1</td>
<td>Test2</td>
<td>Test3</td>
</tr>
</tbody>
</table>
</div>
<div class="modal-footer">
<button type="button" class="btn" data-dismiss="modal" style="background-color: #c4dfe6; color: #1c4d66">Close</button>
</div>
</div>
</div>
</div>
{% endfor %}
</tbody>
</table>

For future reference, I got this to work by using the code below as an alternative to the <table> tag.
<style>
.table {
display: table;
}
.row {
display: table-row;
}
.cell {
display: table-cell;
}
</style
<div class="modal-body">
<div class="table">
<div class="row">
<div class="cell"></div>
</div>
</div>
</div>

Related

bootstrap 4 collapse (accordion table) in django

I've posted the code for a table in django templates that is generated dynamically using an array from views.py. I've added a bootstrap4 collapse, that runs when a chevron button is clicked. However, it shows ALL of the hidden collapses instead of just the collapsible data for that given row (see img below).
I know that I can set ids dynamically, but I haven't had any luck passing functions to the "data-target" attribute.
<table class="table table-hover">
<thead class>
<tr>
<th style="width:5%"></th>
<th>Sample</th>
<th>Reference</th>
<th>Cost</th>
<th>Sum</th>
</tr>
</thead>
<tbody>
<div class="container" id="accordion">
<div class="card">
{% for r in result %}
<tr>
<td>
<button type="button" class="fa fa-chevron-right rotate" data-toggle="collapse"
data-target="#demo" onclick="">
</button>
</td>
<td> {{ r.split.0 }} </td> <!-- sample word -->
<td> {{ r.split.2 }}</td> <!-- ref word -->
<td> {{ r.split.4 }}</td> <!-- cost -->
<td> {{ r.split.3 }}</td> <!-- sum -->
</tr>
<tr>
<td id="demo" class="collapse" colspan="5" >
<!-- COLLAPSE CONTENT -->
</td>
</tr>
{% endfor %}
</div>
</div>
</tbody>
</table>
The "toogle button" have this HTML code:
<button type="button" class="fa fa-chevron-right rotate" data-toggle="collapse"
data-target="#demo">
and the collapsible content is the following:
<td id="demo" class="collapse">[...]</td>
It's normal every toggle button show or hide all collapsible contents in your page, because all these elements are related to the same #demo identifier.
You have to make sure the id of collapsible content is unique accross all the document, and ensure the corresponding button references the same unique id. Maybe use your result id (from context variable) to do something like that:
<button type="button" class="fa fa-chevron-right rotate" data-toggle="collapse"
data-target="#demo-{{ r.pk }}">
<td id="demo-{{ r.pk }}" class="collapse">[...]</td>
EDIT: Of course, you have to adapt it to YOUR data. In this example, I imagine your list result contain many model instances, so in each result r, the value r.pk is unique.
In your template, if results contain something else, you have to make sure a unique str or int is extracted from each value to uniquify the idyou write into your HTML.
Maybe it will be demo-{{ r.split.6 }} or demo-{{ r.a_unique_attr_in_my_object }} or demo-{{ r.slugify }}.
Try to put the content you want to hide under the " COLLAPSE CONTENT ".
like this:
<table class="table table-hover">
<thead class>
<tr>
<th style="width:5%"></th>
<th>Sample</th>
<th>Reference</th>
<th>Cost</th>
<th>Sum</th>
</tr>
</thead>
<tbody>
<div class="container" id="accordion">
<div class="card">
{% for r in result %}
<tr>
<td>
<button type="button" class="fa fa-chevron-right rotate" data-toggle="collapse"
data-target="#demo" onclick="">
</button>
</td>
</tr>
<tr>
<td id="demo" class="collapse" colspan="5" >
<td> {{ r.split.0 }} </td> <!-- sample word -->
<td> {{ r.split.2 }}</td> <!-- ref word -->
<td> {{ r.split.4 }}</td> <!-- cost -->
<td> {{ r.split.3 }}</td> <!-- sum -->
</td>
</tr>
{% endfor %}
</div>
</div>
</tbody>
</table>
By the way, don't forget the Bootstrap CDN

Flask Radio buttons with choice for every user in table from database

I tried to create a html table from flask jinja2 template. I'm able to get all the data from database and make a table with rows and columns and radio buttons. But I'm unable to get choice for every user or row that is added to the table.
Unable to figure out how to do that. Should I make use of javascript,jquery or ajax to give radio button choice for every user or is there a way to solve this in flask template.
My code is as below:
<form class="form-horizontal" action="/check_approve" method="post" id="admin_approve">
<div class="table-responsive">
<table class="table table-bordered table-striped table-highlight">
<thead>
<tr>
<th>UserName</th>
<th>Privileges</th>
<th>Approve</th>
<th>Reject</th>
</tr>
</thead>
<tbody>
{% for item in data %}
<tr>
<td>{{item[0]}}<input type="hidden" name="empid" value={{item[0]}}></td>
<td>
<select name="Privileges" form="admin_approve">
<option value="empty"> </option>
<option value="HR">HR</option>
<option value="Compensation Analyst">Compensation Analyst</option>
<option value="HR Manager">HR Manager</option>
</select>
</td>
<td>
<div class='radio'>
<label><input type='radio' id='choice1' name='approve' value="approve" checked></label>
</div>
</td>
<td>
<div class='radio'>
<label><input type='radio' id='choice2' name='reject' value="reject" checked></label>
</div>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<div class="wrapper">
<button type="submit" form ="admin_approve" class="btn btn-primary">Submit</button>
</div>
</form>
Below is the attached screenshot. Appreciate your help.
table

django how to implement a dynamic (bootstrap) modal

I'm trying to change the interaction of a page so that additional info about a product appears in a modal rather than an element that is shown/hidden when a button is clicked.
The bit I'm not sure about is making the modal adapt it's contents to each product. (I'm not sure if django must prepare 3 different modals first of all at the backend - if this is possible or whether there is a simpler way?)
Previously I had the template:
<div class="item price-2 col-md-4 col-12 text-center best-buy">
<div class="item-inner">
<div class="heading">
<h3 class="title"><b>Cheapest</b></h3>
</div>
{% include 'components/quote_summary_item.html' with quote=first_quote %}
</div><!--//item-inner-->
</div><!--//item-->
{% include 'components/quote_detail.html' with quote=first_quote %}
Where quote_summary_item.html is:
<div>
<div class="quotes-summary"><img src="{{ quote.ImageUrl }}" alt=""></div>
<p></p>
<p >.........
</p>
<a class="btn btn-quote-primary" href="{% url 'users:profile %}">Select</a></div>
<div class="content">
<p><b>Your Quote Details</b></p>
<p>{{ quote.Name }}<br/>
{{ quote.Type }} <br/>
.....
</p>
<button class="btn btn-cta-secondary" data-id="{{ quote.priceId }}">Info</button>
</div><!--//content-->
and quote_detail is:
<div class="quote" id="quote{{ quote.priceId }}" style="display:none">
<div >
<p><b>About this quote:</b></p>
{{ quote.synopsisText|safe }}
</div>
<div class="row">
<div class="col">
<table class="table">
<thead class="til-table-head">
<tr>
<th>Information</th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td>{{ quote.Something }}</td>
</tr>
<tr>
<td>Name</td>
<td>{{ quote.Name }}</td>
</tr>
<tr>
<td></td>
<td>{{ quote.payType }}</td>
</tr>
.......
</tbody>
</table>
</div>
</div>
</div>
And the .js used was:
<script type="text/javascript">
$(document).ready(function () {
$('.btn-cta-secondary').click(function (e) {
var id = $(e.currentTarget).data('id')
$('.quote').hide()
$('#quote' + id).show()
})
})
</script>
I've set up a modal template wrapper around quotes_detail.html however I'm not sure how to change the .js needed to pass in the relevant quote data to something like:
{% include 'components/quotes_modal.html' with quote=my_quote %}
and pass in the relevant quote based on the button id clicked.
There are two decent ways to do this. Implement multiple modals if there aren't too many of them or use ajax to retrieve the contents if there are lots of them.
There is no need to have a .js file as all the required js is built into bootstrap.
Control each modal with eg a button:
<button type="button" class="btn btn-cta-secondary" data-toggle="modal" data-target="#{{ quote.priceId }}" data-id="{{ quote.priceId }}">More Info</button>
Then include the modal template in the relevant page:
{% include 'components/quote_detail.html' with quote=second_quote %}
{% include 'components/quote_detail.html' with quote=first_quote %}
with components/quote_details.html
<div class="modal fade" id="{{ quote.priceId }}" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel"
aria-hidden="true">
.....
with the rest as per bootstrap docs.
To do this via ajax have a look at https://www.abidibo.net/blog/2015/11/18/modal-django-forms-bootstrap-4/

Elegant way to select <tr> when a child <a> is active

Suppose I have the following partial template:
<script type="text/x-handlebars" data-template-name="_folderList">
<table id="folder-list">
<thead>
<tr class="PLEASE_MAKE_ME_ACTIVE_IF_SELECTED_ROW">
<th>Name</th>
<th>Date Created</th>
</tr>
</thead>
<tbody>
{{#each folder in controller}}
<tr>
<td>
{{#linkTo "folder" folder }}
<i class="icon-folder-close"></i> {{ folder.folder_name }}
{{/linkTo}}
</td>
<td style="padding-left: 15px;">
{{ prettyDate folder.created_date}}
</td>
</tr>
{{/each}}
</tbody>
</table>
</script>
Notice the class I've added to the table row element. How can I add an "active" class to this row when it's child link is the active link? Ember automatically adds the active link when the current route matches the element in the collection. I'm trying to find the elegant, "Ember way" of doing this without resorting to jQuery hacks.
While I'd still like to see an answer to the question as I originally stated it, the workaround I went with for now is not to use tables at all. I'm already using Bootstrap based CSS layouts for the rest of my site, so I decided to use a fluid layout instead of tables:
<script type="text/x-handlebars" data-template-name="_folderList">
<div id="folder-list">
<div class="container-fluid list-header">
<div class="row-fluid">
<div class="span5">Name</div>
<div class="span7">Date Created</div>
</div>
</div>
{{#each folder in controller}}
{{#linkTo "folder" folder }}
<div class="container-fluid list-item">
<div class="row-fluid">
<div class="span5">
<i class="icon-folder-close"></i> {{ folder.folder_name }}
</div>
<div class="span7">
{{ prettyDate folder.created_date}}
</div>
</div>
</div>
{{/linkTo}}
{{/each}}
</div>
</script>
This allows me to wrap the whole "row" with the anchor using {{#linkTo}} and style the whole row accordingly.
You can use bindAttr to toggle the classname. For example if you hold the state in an isSelected property in your controller then in the template.
<tr {{bindAttr class='isSelected:active'}}>
Where active is the css class name.

Action Helper in Ember JS not calling function

<a class="btn btn-primary" href="#" {{action "toggleStatus"}}><i class="icon-plus icon-white"></i> Add Staff</a>
This is in a view template called stafflist.handlebars. The content is as follows
<div class="page-header">
<h3>{{staff.title}} <small>List of users who has access to this application</small></h3>
</div>
<div class="commands pull-right">
sta
<a class="btn btn-primary" href="#" {{action "toggleStatus"}}><i class="icon-user icon-white"></i> {{staff.btnTitle}}</a>
</div>
<div class="pull-left">
<input type="text" class="search-query" placeholder="Search">
</div>
<table class="table table-striped table-bordered">
<thead>
<tr>
<td>NAME</td>
<td>ID</td>
</tr>
<thead>
<tbody>
{{#each staff}}
<tr>
<td>{{this.fname}}</td>
<td>{{this.id}}</td>
</tr>
{{/each}}
</tbody>
</table>
The View file is as below
App.StaffListView = Em.View.extend({
templateName:'sellap-web/~templates/stafflist',
staffBinding:'App.staffController',
toggleStatus: function() {
alert('Hurray');
}
});
When clicking the button, the action is never calling the alert. What is going wrong here. I am using ember-skeleton to compile.
Found the culprit. My App.create has a method called init. I changed the name to something else and now it works fine. I didnt call the super.