The CreatorCon Call for Content is officially open! Get started here.

KB Categories widget from Share - sorting alphabetically by category label

jlaue
Mega Sage

Hello - 

I am using the following widget from Share:  

https://share.servicenow.com/app.do#/detailV2/3ef9df2713f67a000de935528144b0ee/overview

This widget displays sub-categories for the Knowledge Base Categories (KB Categories) widget.  I am trying to sort the sub-categories by their "label" field, without much luck.  I tried to add the orderBy to the HTML code and that did not work.  I was hoping someone could please help. 

Here is the code:

 

HTML:

 

<script>
<div ng-repeat-start="cat in data.categories[cat.sys_id]" | orderBy: "label" class="list-group-item">
<a class="category" ng-class="{'active':cat.open}" style="display:block;"><span><div style="display:inline" ng-click="c.catSelect(cat.sys_id)">{{cat.title}}</div><div ng-click="cat.open=!cat.open" class="pull-right glyphicon" ng-class="{'glyphicon-chevron-down': cat.open, 'glyphicon-chevron-right': !cat.open}" ng-if="c.hasChildren(cat)" ></div></span></a>
<div ng-if="data.showHelp" style="font-size:10px">{{cat.tooltip}}</div>
</div>
<div ng-if="c.hasChildren(cat) && cat.open" ng-repeat-end ng-include="'nestedCategories'" | orderBy: "label" class="darken list-group"></div>
</script>

<div class="panel panel-primary b">
<div class="panel-heading">
${Knowledge Categories}<a class="panel-title"></a>
</div>

<div class="list-group">
<div ng-repeat-start="cat in data.categories['top']" class="list-group-item">
<a class="category" ng-class="{'active':cat.open}" style="display:block;"><span><div style="display:inline;margin-left:5px;" ng-click="c.catSelect(cat.sys_id)">{{cat.title}}</div><div ng-click="cat.open=!cat.open" class="pull-right glyphicon" ng-class="{'glyphicon-chevron-down': cat.open, 'glyphicon-chevron-right': !cat.open}" ng-if="c.hasChildren(cat)" ></div></span></a>
<div ng-if="data.showHelp" style="font-size:10px">{{cat.tooltip}}</div>
</div>
<div ng-if="c.hasChildren(cat) && cat.open" ng-repeat-end ng-include="'nestedCategories'" class="darken list-group"></div>
</div>
</div>

 

Server Script:

(function() {
data.category_id = $sp.getParameter("kb_category");

data.categories = {};
data.kb = options.knowledge_base ? options.knowledge_base : $sp.getValue("kb_knowledge_base");

if (!data.kb)
data.kb = "a7e8a78bff0221009b20ffffffffff17"; //For preview purposes

var kc = new GlideRecord('kb_category');
kc.addActiveQuery();
kc.orderBy('short_description');
kc.query();

while (kc.next()) {
//Dont show category if empty
if ($sp.getKBCategoryArticles(kc.getUniqueValue()) == 0)
continue;

var cat = {};
cat.title = kc.label.getDisplayValue();
cat.open = isSubCat(kc.sys_id,data.category_id) || data.category_id == kc.sys_id;
cat.sys_id = kc.getUniqueValue();

//Check if category has parent or is top category
if (kc.parent_table == 'kb_category')
cat.parent = kc.getValue('parent_id');
else if (kc.parent_id == data.kb)
cat.parent = "top";

if (!data.categories[cat.parent])
data.categories[cat.parent] = [];

data.categories[cat.parent].push(cat);
}

function isSubCat(cat,subcat) { //Check if a category is a subcategory to another
if (!subcat)
return false;

var kc = new GlideRecord('kb_category');

//Get our subcategory
if (kc.get(subcat)) {
//See if the main category is one of our parents
if (JSUtil.notNil(kc.parent_id)) {
if (kc.parent_id == cat)
return true;
return isSubCat(cat,kc.parent_id);
}
return false;
}
}

})();

 

Client Script:

 

function($scope,spUtil,$location) {
/* widget controller */
var c = this;

c.catSelect = function(item) {
//alert(item);
var url = "?id=kb_category&kb_category=" + item;
$location.url(url).replace();
}

c.hasChildren = function(cat) {
return (c.data.categories.hasOwnProperty(cat.sys_id));
}

}

 

 

Thanks!!

1 REPLY 1

jlaue
Mega Sage

I figured it out, I missed this line on the Server Script:

 

kc.orderBy('short_description');

 

Updated it to:   kc.orderBy('label');

 

All good now.