fix processing of initial tags

This commit is contained in:
John Anderson 2019-01-03 02:25:12 -05:00
parent 28a02e9943
commit f3cfc17a52

View File

@ -158,34 +158,41 @@ $(document).ready(function() {
}); });
// API backed tags // API backed tags
var tags = $('#id_tags').val() === "" ? [] : $('#id_tags').val().split(/,\s*/); var tags = $('#id_tags');
if (tags.length > 0 && tags.val().length > 0){
tags = $('#id_tags').val().split(/,\s*/);
} else {
tags = [];
}
tag_objs = $.map(tags, function (tag) { tag_objs = $.map(tags, function (tag) {
return { return {
id: tag, id: tag,
text: tag, text: tag,
selected: true
} }
}); });
// Replace the django issued text input with a select element // Replace the django issued text input with a select element
$('#id_tags').replaceWith('<select name="tags" id="id_tags" class="form-control"></select>'); $('#id_tags').replaceWith('<select name="tags" id="id_tags" class="form-control"></select>');
$('#id_tags').select2({ $('#id_tags').select2({
tags: tag_objs, tags: true,
data: function(params) { data: tag_objs,
// paging
var offset = params.page * 50 || 0;
var parameters = {
q: params.term,
brief: 1,
limit: 50,
offset: offset,
};
return parameters;
},
multiple: true, multiple: true,
allowClear: true, allowClear: true,
placeholder: "Tags", placeholder: "Tags",
ajax: { ajax: {
delay: 250, delay: 250,
url: "/api/extras/tags/", url: "/api/extras/tags/",
data: function(params) {
// paging
var offset = params.page * 50 || 0;
var parameters = {
q: params.term,
brief: 1,
limit: 50,
offset: offset,
};
return parameters;
},
processResults: function (data) { processResults: function (data) {
var results = $.map(data.results, function (obj) { var results = $.map(data.results, function (obj) {
return { return {
@ -204,16 +211,14 @@ $(document).ready(function() {
} }
} }
}); });
$('#id_tags').val(tags).trigger("change");
$('#id_tags').closest('form').submit(function(event){ $('#id_tags').closest('form').submit(function(event){
// django-taggit can only accept a single comma seperated string value // django-taggit can only accept a single comma seperated string value
var value = $('#id_tags').val(); var value = $('#id_tags').val();
var final_tags = "";
if (value.length > 0){ if (value.length > 0){
final_tags = value.join(', '); var final_tags = value.join(', ');
$('#id_tags').val(null).trigger('change');
var option = new Option(final_tags, final_tags, true, true);
$('#id_tags').append(option).trigger('change');
} }
$('#id_tags').val(null);
var option = new Option(final_tags, final_tags, true, true);
$('#id_tags').append(option);
}); });
}); });