Fixing taxonomy terms picker with spaces and commas

This commit is contained in:
Sebastien Ros
2014-07-24 17:00:21 -07:00
parent c8a28a9534
commit 31d67803c8
2 changed files with 5 additions and 25 deletions

View File

@@ -2,26 +2,6 @@
/* Helper functions
**********************************************************************/
var parseTerms = function (text) {
var tuples = text.split(",");
var terms = [];
for (var i = 0; i < tuples.length; i++) {
var tuple = tuples[i].split("\t");
var value = parseInt(tuple[1]);
var levels = parseInt(tuple[2]);
var tag = {
label: tuple[0],
value: value,
levels: !isNaN(levels) ? levels : 0,
disabled: tuple[3] == "True"
};
terms.push(tag);
}
return terms;
};
var addTag = function ($plugin, label) {
$plugin.tagit("add", label);
};
@@ -168,13 +148,13 @@
/* Initialization
**********************************************************************/
$(".terms-editor").each(function () {
var allTerms = parseTerms($(this).data("all-terms"));
var selectedTerms = parseTerms($(this).data("selected-terms"));
var allTerms = $(this).data("all-terms");
var selectedTerms = $(this).data("selected-terms");
var $tagit = $("> ul", this).tagit({
tagSource: allTerms,
initialTags: selectedTerms,
triggerKeys: ['enter', 'comma', 'tab'],
triggerKeys: ['enter', 'tab'], // default is ['enter', 'space', 'comma', 'tab'] but we remove comma and space to allow them in the terms
allowNewTags: $(this).data("allow-new-terms"),
tagsChanged: onTagsChanged,
caseSensitive: false,

View File

@@ -26,8 +26,8 @@
}
}
@{
var allTerms = string.Join(",", Model.Terms.Select(x => string.Format("{0}\t{1}\t{2}\t{3}", x.Name, x.Id, x.GetLevels(), IsTermDisabled(x))));
var selectedTerms = string.Join(",", Model.Terms.Where(x => x.IsChecked).Select(x => string.Format("{0}\t{1}", x.Name, x.Id)));
var allTerms = Newtonsoft.Json.JsonConvert.SerializeObject(Model.Terms.Select(x => new { label = x.Name, value = x.Id, levels = x.GetLevels(), disabled = IsTermDisabled(x)}));
var selectedTerms = Newtonsoft.Json.JsonConvert.SerializeObject(Model.Terms.Where(x => x.IsChecked).Select(x => new { label = x.Name, value = x.Id, levels = 0, disabled = true }));
}
<fieldset class="taxonomy-wrapper" data-name-prefix="@Html.FieldNameFor(m => m)" data-id-prefix="@Html.FieldIdFor(m => m)">
<legend @if(Model.Settings.Required) { <text>class="required"</text> }>@Model.DisplayName.CamelFriendly()</legend>