Use a generic collection directive
The sfCollection directive enables you to display a collection of items and provides you with the functionality to select single or multiple of those items.
The following tutorial demonstrates how to add a generic collection directive in a widget designer's view. Keep in mind that you are free to use selectors and directives outside of widget designers.
Add generic collection directive
To add the generic collection directive:
-
Sitefinity CMS automatically registers the scripts you need and, if no other designer view with explicitly set priority exists, Sitefinity CMS sets your designer view priority 1. In case you need to have full control over the scripts that are loaded or you want to set custom priority, you can alternatively create your own
DesignerView.YourView.jsonfile. If you have aJSONfile that matches the convention (even if empty), this automatic scripts registration will not occur. In yourDesignerView.<YourView>.jsonfile, add ascriptsarray. As a result, the file content looks similar to the following:JSON{ "priority": 1, "components" : ["sf-collection"] } -
In your file, right before the definition of your custom view controller, place the following code:
angular.module('designer').requires.push('sfCollection'); -
Your designer controller exposes the following scope items, so that it binds them to the
sfCollectiondirective.JavaScriptangular.module('designer') .controller('SimpleCtrl', ['$scope', 'propertyService', 'serverContext', function ($scope, propertyService, serverContext) { $scope.items = [{ Id: '1', Title: "Folder1" }, { Id: '2', Title: "Folder2" }, { Id: '3', Title: "Folder3" }]; }]); -
In your
DesignerView.<YourView>.cshtmlfile, place the following tag where you want to render thesfCollectiondirective:HTML+Razor<sf-collection sf-data="items" sf-model="[selectedItem]" sf-multiselect="[false/true]" sf-identifier="[Title]" sf-deselectable="[false/true]" sf-template-url="[path to your template]"> </sf-collection>The
sf-collectiontag exposes the following attributes:-
sf-data
Represents the items loaded by thesfCollectiondirective. This is a required attribute. -
sf-model
Provides the selected item or items when in single or multiple selection mode, respectively. -
sf-multiselect
Configures the directive in single or multiple selection mode. This attribute is optional. -
sf-identifier
Specifies which property value is to be exposed in thesf-modelcollection. If you do not specify this attribute, the sfCollection directive exposes the item itself in thesf-modelcollection. -
sf-deselectable
Specifies whether the selected item must be removed from the array of selected items. Set this value totrueif you want to remove the item from the collection. -
sf-template-url
By default, thesfCollectiondirective does not expose any visual representation. To set the HTML template view, you must provide value for thesf-template-urlattribute. For example, if your template view is namedSfCollectionTemplate.htmland it is located in the same folder as theDesignerView.<YourView>.cshtml, the value of the attribute looks similar to this:
sf-template-url="SfCollectionTemplate.html"
For more information, see Create custom templates.
-
Subscribe to selection event
To provide notification when item is selected, the sfCollection directive emits a custom selection event. To subscribe to the selection event, use the following code in your designer's controller:
$scope.$on('sf-collection-item-selected', function (event, item) { });
Create custom template
In the markup of your custom sfCollection template, add the following code snippet:
<div id="grid">
<li ng-repeat="item in items" class="col-md-4" ng-click="select(item)">
{{::item.Title}}
</li>
</div>
<div id="list">
<li ng-repeat="item in items" ng-click="select(item)">
{{::item.Title}}
</li>
</div>
<div>
<button ng-click="switchToGrid()">Grid</button>
<button ng-click="switchToList()">List</button>
</div>
<style>
.sf-collection-grid > div#list {
display: none;
}
.sf-collection-list > div#grid {
display: none;
}
</style>
The sfCollection directive’s scope provides 2 additional methods for switching between the Grid and List modes:
switchToGrid()switchToList()
To use these 2 methods, you must provide 2 CSS files in the custom sfCollection template:
sf-collection-grid- the CSS applied whensfCollectionis in Grid modesf-collection-list- the CSS applied whensfCollectionis in List mode.