Jay,
The trigger-action is only going to occur when the user clicks the list item menu (i.e. the three dots)
layerList.on("trigger-action", function(event) {
...
});
You use the LayerList listItemCreatedFunction property when you want to do something for each item that is created in the Layerlist.
Here is a sample that when you toggle the parent item the console.logs it's visibility:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">
<title>LayerList widget with actions - 4.10</title>
<style>
html,
body,
#viewDiv {
padding: 0;
margin: 0;
height: 100%;
width: 100%;
overflow: hidden;
}
</style>
<link rel="stylesheet" href="https://js.arcgis.com/4.10/esri/css/main.css">
<script src="https://js.arcgis.com/4.10/"></script>
<script>
require([
"esri/Map",
"esri/views/MapView",
"esri/layers/GroupLayer",
"esri/layers/MapImageLayer",
"esri/widgets/LayerList"
], function(
Map, MapView, GroupLayer, MapImageLayer, LayerList
) {
var USALayer = new MapImageLayer({
url: "http://sampleserver6.arcgisonline.com/arcgis/rest/services/USA/MapServer",
title: "US Sample Data"
});
var censusLayer = new MapImageLayer({
url: "http://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer",
title: "US Sample Census",
visible: false
});
var map = new Map({
basemap: "dark-gray",
layers: [USALayer, censusLayer]
});
var view = new MapView({
center: [-98.5795, 39.8282],
zoom: 5,
container: "viewDiv",
map: map
});
var uniqueParentItems = [];
function defineActions(event) {
var item = event.item;
if(!item.parent){
if(!uniqueParentItems.includes(item.title)){
uniqueParentItems.push(item.title);
item.watch("visible", function(event){
console.info(event);
});
}
}
}
view.when(function() {
var layerList = new LayerList({
view: view,
listItemCreatedFunction: defineActions
});
view.ui.add(layerList, "top-right");
});
});
</script>
</head>
<body>
<div id="viewDiv"></div>
</body>
</html>