How to display user- defined number of fields of a form? - html

I have a created a form using HTML and Jinja2 to configure a CSV file. The user is asked to change the name and assign a type to each column name. The CSV file can have any number of columns, therefore, I have created the form through looping Currently, I use the bootstrap grid and divide into three columns and rows depending on the number of columns.
<form action="/updateFile" method="POST" enctype="multipart/form-data">
<div class="row">
<div class="col-sm-12">
<p class="form-group">
{% for x in title %}
{% if loop.index == 1 %} <div class="row"> {% endif %}
<div class="col-xs-12 col-sm-12 col-lg-4 col-md-4">
<div class="row">
<div class="col-sm-6">
<label> Field {{ loop.index }} : {{ x }} </label>
<input type="text" title="Name should only contain letters or numbers" class="form-control"
name="field-{{ loop.index }}" value="{{ x }}" maxlength="10" pattern="[a-zA-Z0-9_ ]+" required>
</div>
<div class="col-sm-6 pull-right">
<label> </label>
<select class="form-control " name="choice-{{ loop.index }}">
<option value="mcqsr">MCQ-Single Response</option>
<option value="mcqmr">MCQ-Multiple Response</option>
<option value="num">Numerical</option>
<option value="tim">Temporal</option>
<option value="lat">Survey Site - Latitude</option>
<option value="lng">Survey Site - Longitude</option>
<option value="temp">Survey Site - Temporal</option>
</select>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<label> </label>
<input type="text" title="Name should only contain letters or numbers" class="form-control"
name="question-{{ loop.index }}" value=" Question of {{ x }}" pattern="[a-zA-Z0-9_ ]+" required>
</div>
</div>
</div>
{% if loop.index % 3 == 0 %} </div><p> </p><p> </p><div class="row"> {% endif %}
<input type="hidden" name="title-{{ loop.index }}" value= {{x}}>
{% endfor %}
</div>
</div>
<div class="row">
<div class="col-sm-12 text-center">
<input type="hidden" name="filename" value= {{filename}}>
<input type="hidden" name="numofField" value= {{numofField}}>
<button class="btn btn-primary" type="submit" value="Update">Submit</button>
</div>
</div>
</div>
</form>
I would like to include an option the user to choose the number of variables he/she wants to choose. Similar to the e-commerce sites or photo albums, they generally give you an option to "show 25" at a time, and give you the option of changing that to "show 50", "show 100", or "show all" in order to allow the user have the control in the amount of clutter they will be able to navigate through in the page. I could find few examples like datatables.js but they only work on tables. I could not find a way how to go about it?

If you access to javascript you can write logic something like this.
let container = document.getElementById('container');
for (let i=0; i<200; i++) {
let div = document.createElement('div');
div.className = 'box';
container.appendChild(div);
}
var items = document.getElementsByClassName('box');
function show(howMany) {
showAll();
let count = parseInt(howMany);
for(let i = 0; i<items.length;i++){
if(i<count){
continue
} else {
items[i].style.display = 'none';
}
}
}
function showAll() {
for(let i = 0; i<items.length;i++){
items[i].style.display = 'inline-block';
}
}
.box{
display: inline-block;
height: 25px;
width: 25px;
background: tomato;
margin-right: 4px;
}
<button onclick="show('50')">show 50</button>
<button onclick="show('100')">show 100</button>
<button onclick="showAll()">show all</button>
<div id="container"></div>

Related

tippy.js html content / multiline text

I am having problems with tippy.js tooltip and multiline html content. I cannot get the line breaks to work at all. HTML template below:
<div id="div_id_criticality" class="form-group">
<label for="id_criticality" class="requiredField">
Criticality<span class="asteriskField">*</span>
</label>
<span class="formHelp" tippy-message="tippy-criticality" id="formHelp-criticality"><i class="fas fa-question-circle"></i></span>
<div>
<select name="criticality" class="select form-control" required="" id="id_criticality">
<option value="">-- Select criticality --</option>
{% for key, value in criticality.items %}
<option value="{{ key }}">{{ value }}</option>
{% endfor %}
</select>
</div>
</div>
<div id="tippy-criticality" class="tippy-multiline" style="display: none;">
Line 1<br>
Line 2<br>
Line 3<br>
Line 4
</div>
<script>
tippy('#formHelp-criticality', {
content(reference) {
const id = reference.getAttribute('tippy-message');
const template = document.getElementById(id);
return template.innerHTML;
allowHTML=true;
}
});
</script>
I also have CSS file containing:
.tippy-multiline {
white-space:pre !important;
}
Also tried:
.tippy-multiline {
white-space:pre-wrap !important;
}
What happens is that the tooltip shows single line of text "Line 1< br>Line 2< br>Line 3< br>Line 4".
What I am missing here?

html sending data from radio selection

I have above html code. Which has table with rows and each row has selection radio button. There is confirm button. I need to send the row data for selected radio option on button click. How should I find what was selected and send that rows data on button click. I want to use javascript but I am very novice in it. Please suggest.
{% extends "base.html" %} {% block title %}Login{% endblock %} {% block content
%}
<html>
<div class="container ">
<h1>You are proposing trade for:: {{desired_item_details[2]}}</h1>
<style>
th, td
{
border:1px solid blue;
padding:10px;
text-align:left;
}
table
{
border-collapse: collapse;
font-size:12px;
text-align: left;
}
</style>
<!-- Content here -->
</div>
<body>
<div class="home">
<br><br><br>
<label>Please Choose Your Proposed Items::</label>
<table>
<tr>
<th>Item#</th>
<th>Game Type</th>
<th>Title</th>
<th>Condition</th>
</tr>
{% for item in proposed_items %}
<tr>
<td>{{item[0]}}</td>
<td>{{item[1]}}</td>
<td>{{item[2]}}</td>
<td>{{item[3]}}</td>
<td>
<input value="{{item[0]}}" id="type_radio_1" name="select" type="radio" /> Select
</td>
</tr>
{% endfor %}
</table>
</div>
<div class="col">
<a class="btn btn-primary btn-lg" role="button">Confirm</a>
</div>
</body>
</html>
{% endblock %}
Here is a simple solution for your question.
document.getElementById("confirmButton").onclick = ( function(event) {
for(var i = 1; i < 4; i++) {
var radioButton = document.getElementById("type_radio_"+i)
if(radioButton.checked){
alert("You have selected" + radioButton.value);
}
}
});
<input value="{{item[0]}}" id="type_radio_1" name="select" type="radio" /><label for="type_radio_1">Select</label>
<input value="{{item[1]}}" id="type_radio_2" name="select" type="radio" /><label for="type_radio_2">Select</label>
<input value="{{item[2]}}" id="type_radio_3" name="select" type="radio" /><label for="type_radio_3">Select</label>
<input type="button" class="btn btn-primary btn-lg" id="confirmButton" value="Confirm">
You can easily do this using a loop and a condition to check whether the current radio button's id in the loop is selected or not. I have changed your confirm button which was a "a" HTML element to a "input" element.
If you want to change the id of the radio input field dynamically, try to use the below code.
document.getElementById("confirmButton").onclick = ( function(event) {
for(var i = 0; i < 3; i++) {
var radioButton = document.getElementById("{{item["+i+"]}}")
if(radioButton.checked){
alert("You have selected" + radioButton.value);
}
}
});
<input value="{{item[0]}}" id="{{item[0]}}" name="select" type="radio" /><label for="{{item[0]}}">Select</label>
<input value="{{item[1]}}" id="{{item[1]}}" name="select" type="radio" /><label for="{{item[1]}}">Select</label>
<input value="{{item[2]}}" id="{{item[2]}}" name="select" type="radio" /><label for="{{item[2]}}">Select</label>
<input type="button" class="btn btn-primary btn-lg" id="confirmButton" value="Confirm">
Thanks and best regards!

Make a Django post request to ManyToManyField

This is my List in the HTML document
I have a Django post view, when I hit send I want the selected users to be posted to the students field.
As you can see I have tried to do the instance and user thing from this url but I can't figure it out!
HTML:
{% for elev in elever %}
<div class="custom-control custom-checkbox">
<span class="d-flex align-items-center">
<div style="margin-right: 10px">
<div class="circle" style="background-color: {{ elev.color }}">
<span class="initials" , style="color: #ffffff"> {{ elev.user|capfirst|first }}
</span>
</div>
</div>
<div style="width: 300px">
<span class="h6 mb-0" data-filter-by="text">{{elev.user|capfirst}}</span>
</div>
<div class="checkboxx"> <input type="checkbox" name="Studyplan-Canview" value= "{{ student.name }}">
<style>
.checkerName {
margin-left: 10px
}
.checkboxx {
align-content: flex-end;
margin-left: 300px
}
</style> </div>
</span>
</label>
</div>
{% endfor %}
The View:
#post request checker
#login_required
def skapa_studieplan(request):
if request.method == 'POST':
name = request.POST.get('Studyplan-Name')
description = request.POST.get('Studyplan-Description')
parent_assignment = Assignment.objects.get(pk=request.POST.get('Studyplan-PAID'))
users = UserProfile.objects.all()
instance = Studyplan.objects.create(request.POST.get('Studyplan-Canview'))
for user in users:
instance.canview.add(user)
studyplan = Studyplan(name=name, description=description, parent_assignment=parent_assignment, canview=instance)
studyplan.save()
return redirect('allaStudieplaner')

HTML, CSS, flask_wtf how to change form field locations

I'm working on my first webapp. I need a simple form for the user to submit a few values. I got it working, but I would like the data entry fields to be on a row instead of in a column.
You can see bootstrap used in the snippets below. I don't think that is involved because the fields render in the same locations without using it.
base.html:
{% extends 'bootstrap/base.html' %}
{% block title %}
...
{% endblock %}
{% block navbar %}
...
{% endblock %}
{% block content %}
<div class="container">
...
{# application content needs to be provided in the app_content block #}
{% block app_content %}{% endblock %}
</div>
{% endblock %}
forms.py:
from flask_wtf import FlaskForm
from wtforms import StringField, IntegerField, PasswordField, BooleanField, SubmitField
from wtforms.validators import DataRequired, IPAddress, NumberRange, length
class SnmpQueryForm(FlaskForm):
ip = StringField('IP Address', validators=[IPAddress()])
mask = IntegerField('Mask bits',
validators=[DataRequired(),
NumberRange(22, 32)]
)
snmpcmstr = StringField('Community String',
validators=[DataRequired(),
length(max=20)]
)
submit = SubmitField('Submit Query')
template.html:
{% extends "base.html" %}
{% import 'bootstrap/wtf.html' as wtf %}
{% block app_content %}
<h1>Enter SNMP Query Information</h1>
<form action="", method="post">
<div class="row">
<div class="col-md-2">
{{ wtf.quick_form(form) }}
</div>
</div>
</form>
<br>
<br>
<div class="container">
...
Here is the result:
I would like the three fields to be on the same line (in the same row). Any suggestions?
Thank you
Just to add the solution I used for posterity:
All I had to do was change Lakindu's POC code to:
<div class="col-md-4">
<div class="form-group">
{{wtf.form_field(form.ip, class="form-control", placeholder="Enter IP") }}
</div>
</div>
for each input field and add:
<div class="col-md-4">
<div class="input submit">
<input type="submit" value="Submit">
</div>
</div>
for the submit button.
Thank you for the quick help.
This is a proof of concept of what you trying to achieve.
You can use col-md-4 in a row and a container class in order to get three input fields to a single row.
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<form action="" , method="post">
<div class="container">
<div class="row">
<div class="col-md-4">
<div class="form-group">
<label>IP address</label>
<input type="text" class="form-control">
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label>Mask bits</label>
<input type="text" class="form-control">
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label>Community string</label>
<input type="text" class="form-control">
</div>
</div>
</div>
</div>
</form>

How to handle multiple forms with dynamic radio buttons on same page using python and html?

Good Day!
I'm having a unique problem where I have to create a HTML page which has 3 forms, in which the radio buttons are generated dynamically based on the input of the previous form. I'm using Python 3.6 and HTML.
Parts of my code:
from the app:
selection1 = sorted(list(Some_Table['var1'].unique()))
if request.method == 'POST':
if request.form['submit1']:
selection2 = sorted(list(pandas.DataFrame(Some_Table[Some_Table['var1'] == request.form['Header1']])['var2'].unique()))
return render_template('view.html', selection2 = selection2, selection1=selection1)
if request.form['submit2']:
selection3 = sorted(list(pandas.DataFrame(Some_Table[Some_Table['var2'] == request.form['Header2']])['var3'].unique()))
return render_template('view.html', selection3 = selection3, selection2 = selection2, selection1=selection1)
return render_template('view.html', selection1=selection1)
from the html script:
<div2>
<form action="" method="post">
<p> {% for c1 in selection1 %}
<input type="radio" name="Header1" value={{c1}}> {{c1}} </input><br>
{% endfor %} </p>
<p> <input type=submit name=submit1 value="Show Table1"> </p>
</form>
</div2>
<div2 style="margin-top: 250px; ">
<form action="" method="post">
<p> {% for c2 in selection2 %}
<input type="radio" name="Header2" value={{c2}}> {{c2}} </input><br>
{% endfor %} </p>
<p> <input type=submit name=submit2 value="Show Table2"> </p>
</form>
</div2>
<div2 style="margin-top: 500px; ">
<form action="" method="post">
<p> {% for c3 in selection3 %}
<input type="radio" name="Header3" value={{c3}}> {{c3}} </input><br>
{% endfor %} </p>
<p> <input type=submit name=submit3 value="Show Table3"> </p>
</form>
</div2>
JAVASCRIPT
for (i = 0; i < 20; i++) {
var radioBtn = $('<input type="radio" name="rbtnCount" />');
radioBtn.appendTo('#target');
}
HTML
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="target"></form>