Skip to content
import FeatureTemplatesViewModel from "@arcgis/core/widgets/FeatureTemplates/FeatureTemplatesViewModel.js";
Inheritance:
FeatureTemplatesViewModelAccessor
Example
const templatesVM = new FeatureTemplatesViewModel({
layers: layers
});
const featureTemplates = new FeatureTemplates({
viewModel: templatesVM
container: "templatesDiv"
});

Constructors

Constructor

Constructor
Parameters
ParameterTypeDescriptionRequired
properties
See the properties table for a list of all the properties that may be passed into the constructor.

Properties

Any properties can be set, retrieved or listened to. See the Watch for changes topic.

declaredClass

readonlyinherited Property
Type
string
Inherited from: Accessor

The name of the class. The declared class name is formatted as esri.folder.className.

disabled

Property
Type
boolean
Since
ArcGIS Maps SDK for JavaScript 4.28

Used to disable the associated user interface. This does not prevent the view model from functioning programmatically. Methods invoked programmatically still work as expected.

Default value
false

filterFunction

Property
Type
Filter | null | undefined

function can be defined to help filter template items within the widget. A custom function can be used to aid when searching for templates. It takes a function which passes in an object containing a name property of the template item.

featureTemplatesFilterFunction

Example
// Filter and display templates only if their names contain the word `Street`
function myFilterFunction(filter) {
let containsName = filter.label.includes("Street");
return containsName;
}
// Create the FeatureTemplates widget
const templates = new FeatureTemplates({
container: "templatesDiv",
visibleElements = {
filter: false // does not display the default feature templates filter
},
layers: [featureLayer], // in this example, one layer is used
filterFunction: myFilterFunction
});

groupBy

Property
Type
GroupByType | null | undefined

It is possible to group template items. This can aid in managing various template items and how they display within the widget. The values are discussed below.

TypeDescriptionExample
layerThis is the default grouping. Groups template items by layers.featureTemplatesGroupByLayer
geometryGroups template items by geometry type.FeatureTemplatesGroupByGeometry
noneThe widget displays everything in one list with no grouping.featureTemplatesGroupByLayer
GroupByFunctionCustom function that takes an object containing a FeatureTemplate and FeatureLayer.FeatureTemplatesGroupByCustomGroupFunction
Default value
"layer"
Example
// This example shows using a function to check if
// the layer title contains the word 'military'. If so,
// return a group of items called "All Military Templates"
function customGroup(grouping) {
// Consolidate all military layers
if (grouping.layer.title.toLowerCase().indexOf("military") > -1) {
return "All Military Templates"
}
// Otherwise, group by layer title
return grouping.layer.title;
}
// Create the FeatureTemplates widget
templates = new FeatureTemplates({
container: "templatesDiv",
layers: layers,
groupBy: customGroup
});

items

readonly Property
Type
(TemplateItem | TemplateItemGroup)[]

The template items or grouped template items.

layers

Property
Type
Array<LayerWithFeatureTemplatesUnion | SubtypeGroupLayer>

An array of FeatureLayers that are associated with the widget. The order in which these layers are set in the array dictates how they display within the widget.

The widget is designed to only display layers that are enabled for editing. It will not display layers that are enabled to only edit attributes.

Example
// The layers to display within the widget
let militaryUnits = new FeatureLayer({
url: "http://sampleserver6.arcgisonline.com/arcgis/rest/services/Military/FeatureServer/2"
});
let militaryHostile = new FeatureLayer({
url: "http://sampleserver6.arcgisonline.com/arcgis/rest/services/Military/FeatureServer/6"
});
let layers = [militaryUnits, militaryHostile];
// Create FeatureTemplates widget
templates = new FeatureTemplates({
container: "templatesDiv",
layers: layers
});

state

readonly Property
Type
FeatureTemplatesViewModelState

The widget's state. Possible values are in the table below.

ValueDescription
readyDependencies are met and has valid property values.
loadingLayers are still loading and not ready yet.
disabledNo layers are available to load.
Default value
"disabled"

Methods

MethodSignatureClass
emit
inherited
emit<Type extends EventNames<this>>(type: Type, event?: this["@eventTypes"][Type]): boolean
hasEventListener
inherited
hasEventListener<Type extends EventNames<this>>(type: Type): boolean
on
inherited
on<Type extends EventNames<this>>(type: Type, listener: EventedCallback<this["@eventTypes"][Type]>): ResourceHandle
refresh(): void
select(item: TemplateItem | null | undefined): void

emit

inherited Method
Signature
emit <Type extends EventNames<this>>(type: Type, event?: this["@eventTypes"][Type]): boolean
Type parameters
<Type extends EventNames<this>>
Inherited from: EventedMixin

Emits an event on the instance. This method should only be used when creating subclasses of this class.

Parameters
ParameterTypeDescriptionRequired
type
Type

The name of the event.

event
this["@eventTypes"][Type]

The event payload.

Returns
boolean

true if a listener was notified

hasEventListener

inherited Method
Signature
hasEventListener <Type extends EventNames<this>>(type: Type): boolean
Type parameters
<Type extends EventNames<this>>
Inherited from: EventedMixin

Indicates whether there is an event listener on the instance that matches the provided event name.

Parameters
ParameterTypeDescriptionRequired
type
Type

The name of the event.

Returns
boolean

Returns true if the class supports the input event.

on

inherited Method
Signature
on <Type extends EventNames<this>>(type: Type, listener: EventedCallback<this["@eventTypes"][Type]>): ResourceHandle
Type parameters
<Type extends EventNames<this>>
Inherited from: EventedMixin

Registers an event handler on the instance. Call this method to hook an event with a listener.

Parameters
ParameterTypeDescriptionRequired
type
Type

An event or an array of events to listen for.

listener
EventedCallback<this["@eventTypes"][Type]>

The function to call when the event fires.

Returns
ResourceHandle

Returns an event handler with a remove() method that should be called to stop listening for the event(s).

PropertyTypeDescription
removeFunctionWhen called, removes the listener from the event.
Example
view.on("click", function(event){
// event is the event handle returned after the event fires.
console.log(event.mapPoint);
});

refresh

Method
Signature
refresh (): void

This method updates the template items with the provided filter.

Returns
void

select

Method
Signature
select (item: TemplateItem | null | undefined): void

Selects the template item to use.

See also
Parameters
ParameterTypeDescriptionRequired
item

The template item to select.

Returns
void

Events

select

Event

Fires when a template item is selected. This occurs when the select() method is called.

See also
bubbles composed cancelable
Example
// Listen for when a template item is selected
templates.on("select", function(evtTemplate) {
// Access the selected template item's attributes
attributes = evtTemplate.template.prototype.attributes;
// Create a new feature with the selected template at cursor location
const handler = view.on("click", function(event) {
handler.remove(); // remove click event handler.
event.stopPropagation(); // Stop click event propagation
if (event.mapPoint) {
// Create a new feature with the selected template item.
editFeature = new Graphic({
geometry: event.mapPoint,
attributes: {
"IncidentType": attributes.IncidentType
}
});
// Setup the applyEdits parameter with adds.
const edits = {
addFeatures: [editFeature]
};
featureLayer.applyEdits(params).then(function(editsResult) {
if (editsResult.addFeatureResults.length > 0) {
console.log("Created a new feature.")
}
});
}
});
});

Type definitions

FeatureTemplatesViewModelState

Type definition
Type
"disabled" | "loading" | "ready"

SelectEvent

Type definition

item

Property
Type
TemplateItem | null | undefined

The selected template item.

template

Property
Type
FeatureTemplate | SharedTemplateMetadata | null | undefined

The feature template associated with the template item.