简体   繁体   中英

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

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>&nbsp;</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>&nbsp;</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>&nbsp;</p><p>&nbsp;</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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM