Compare commits

...

1 Commits

Author SHA1 Message Date
Richard Schreiber
6db9b1711b add client-side csv-file for answer options 2022-11-15 12:56:21 +01:00
2 changed files with 23 additions and 0 deletions

View File

@@ -118,6 +118,10 @@
<button type="button" class="btn btn-default" data-formset-add>
<i class="fa fa-plus"></i> {% trans "Add a new option" %}</button>
</p>
<p>
{% trans "or add options from a csv file" %}
<input type="file" id="add-options-csv" accept=".csv" aria-label="{% trans "csv file with answer options" %}">
</p>
</div>
</div>
</fieldset>

View File

@@ -119,6 +119,25 @@ var form_handlers = function (el) {
form_handlers($(event.target));
});
el.find("#add-options-csv").on("change", function(event) {
var formset = $(this).closest('.formset');
var addOptionButton = formset.find("button[data-formset-add]");
for (const file of event.target.files) {
const reader = new FileReader();
reader.addEventListener('load', (event) => {
event.target.result.split(/\r?\n/).forEach(function(line) {
if (!line) return;
var values = line.split(/[;,\t]/);
addOptionButton.trigger("click");
formset.find("[data-formset-body]>*:last-child").find("[type=text], textarea").each(function(i) {
if (values[i]) this.value = values[i];
});
});
});
reader.readAsText(file);
}
});
// Vouchers
el.find("#voucher-bulk-codes-generate").click(function () {
var num = $("#voucher-bulk-codes-num").val();