You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
To add a contextual menu to our ListView we will insert another Viewfield in the webpart code at the position of our choice, for instance after the “Lastname”:
We use the render method of IViewField. Inside we create our ECB component and as a property we handover the rowitem which is clicked.
20
-
We could also handover functions that shall be executed inside the context menu component but be able to be bound to the parent component, that is the webpart/component which contains the ListView.
1
+
# ListView: Add a contextual menu
2
+
21
3
## The ContextualMenu component
22
-
We now need to create the ECB component which represents our context menu. Therefore we will use a combination of an [IconButton](https://developer.microsoft.com/en-us/fabric#/components/button#Variants) and a [ContextualMenu](https://developer.microsoft.com/en-us/fabric#/components/contextualmenu) from the Office UI Fabric components.
23
-
The code for this additional component looks like this:
4
+
5
+
In order to create a contextual menu for your list view, you first need to create a new component which will use a combination of an [IconButton](https://developer.microsoft.com/en-us/fabric#/components/button#Variants) and [ContextualMenu](https://developer.microsoft.com/en-us/fabric#/components/contextualmenu) controls from the Office UI Fabric React.
onClick: () =>console.error('Disabled action should not be clickable.')
65
+
}
66
+
]
67
+
}} />
68
+
</div>
69
+
);
87
70
}
88
71
89
72
private handleClick(source:string, event) {
90
-
alert(source + 'clicked');
73
+
alert(`${source} clicked`);
91
74
}
92
75
}
93
76
```
94
-
One of the things to mention is that in the (totally simplified for demo reasons here) onClick function we hand over one attribute of the clicked item that we have in the components' properties so we can process it in the function.
95
-
Another trick is to give an empty iconName for the menuIconProps. This is because once you attach a menuProps Attribute to any Kind of Office UI fabric button a ChevronDown selector on the right side of the button will occur by default.
96
-
With the menuIconProps Attribute you can adjust this, that is when specifying an empty Name you can remove it. This is what we want because we only want to have our “MoreVertical” icon which are the three dots in a vertical order.
97
-
To place this a bit more centric, we have small CSS manipulation as well:
98
-
```SCSS
99
-
.ecb {
100
-
position:absolute;
101
-
top: -3px;
102
-
.ecbbuttondiv {
103
-
padding-left: 12px;
104
-
}
77
+
78
+
## The ListView column
79
+
80
+
Once the ECB component is created, you can add the contextual menu to the `ListView` control. In order to do this, you have to insert another `Viewfield` in code at the position of our choice. For instance after the `Lastname`:
To extend the ListView control with a [ContextualMenu](https://developer.microsoft.com/en-us/fabric#/components/contextualmenu) refer to [ListView.ContextualMenu](./ListView.ContextualMenu.md)
57
+
!!! note "Extend ListView with a ContextualMenu"
58
+
To extend the `ListView` control with a [ContextualMenu](https://developer.microsoft.com/en-us/fabric#/components/contextualmenu) refer to [ListView.ContextualMenu](./ListView.ContextualMenu).
0 commit comments