Skip to content

Commit 8d04406

Browse files
committed
feat(menu): add Context Menu feature POC
1 parent 128600e commit 8d04406

File tree

3 files changed

+141
-5
lines changed

3 files changed

+141
-5
lines changed

src/app/examples/grid-additem.component.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ <h2>{{title}}</h2>
2828

2929
<div class="col-sm-12">
3030
<angular-slickgrid gridId="grid2" [columnDefinitions]="columnDefinitions" [gridOptions]="gridOptions"
31-
[dataset]="dataset"
32-
(onAngularGridCreated)="angularGridReady($event)">
31+
[dataset]="dataset" (sgOnContextMenu)="onContextMenu($event.detail.eventData, $event.detail.args)"
32+
(onAngularGridCreated)="angularGridReady($event)">
3333
</angular-slickgrid>
3434
</div>
3535
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
.duration-bg {
2+
background-color: #e9d4f1 !important;
3+
}
4+
5+
.grey {
6+
color: grey;
7+
}
8+
.orange {
9+
color: orange;
10+
}
11+
.red {
12+
color: red;
13+
}
14+
15+
#contextMenu {
16+
position:absolute;
17+
background: #ffffff;
18+
border: 1px solid #b8b8b8;
19+
border-radius: 2px;
20+
padding: 5px;
21+
display: inline-block;
22+
min-width: 150px;
23+
z-index: 99999;
24+
25+
.title {
26+
display: inline-block;
27+
font-size: 16px;
28+
width: calc(100% - 10px);
29+
border-bottom: solid 1px #d6d6d6;
30+
margin-bottom: 5px;
31+
}
32+
33+
li {
34+
border: 1px solid transparent;
35+
padding: 4px 4px 4px 14px;
36+
list-style: none;
37+
cursor: pointer;
38+
39+
&:hover {
40+
border: 1px solid #cccccc;
41+
}
42+
}
43+
}

src/app/examples/grid-additem.component.ts

+96-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,33 @@
11
import { Component, OnInit, Injectable, ViewEncapsulation } from '@angular/core';
2-
import { AngularGridInstance, Column, Editors, FieldType, Formatters, GridOption, GridService, OnEventArgs } from './../modules/angular-slickgrid';
2+
import {
3+
AngularGridInstance,
4+
Column,
5+
Editors,
6+
FieldType,
7+
Formatter,
8+
Formatters,
9+
GridOption,
10+
GridService,
11+
OnEventArgs
12+
} from './../modules/angular-slickgrid';
13+
14+
// custom formatter to display priority (from 1 to 3) loop through that count and display them as x number of icon(s)
15+
const customPriorityFormatter: Formatter = (row, cell, value, columnDef, dataContext) => {
16+
let output = '';
17+
const count = +(value >= 3 ? 3 : value);
18+
const color = count === 3 ? 'red' : (count === 1 ? 'grey' : 'orange');
19+
const icon = `<i class="fa fa-fire ${color}" aria-hidden="true"></i>`;
20+
21+
for (let i = 1; i <= count; i++) {
22+
output += icon;
23+
}
24+
return output;
25+
};
326

427
@Component({
5-
styles: ['.duration-bg { background-color: #e9d4f1 !important }'],
28+
templateUrl: './grid-additem.component.html',
29+
styleUrls: ['./grid-additem.component.scss'],
630
encapsulation: ViewEncapsulation.None,
7-
templateUrl: './grid-additem.component.html'
831
})
932
@Injectable()
1033
export class GridAddItemComponent implements OnInit {
@@ -25,6 +48,7 @@ export class GridAddItemComponent implements OnInit {
2548
<ul>
2649
<li>Example, click on button "Highlight Rows with Duration over 50" to see row styling changing. <a href="https://github.com/ghiscoding/Angular-Slickgrid/wiki/Dynamically-Add-CSS-Classes-to-Item-Rows" target="_blank">Wiki doc</a></li>
2750
</ul>
51+
<li>Context Menu... Right+Click on any row to show a Context Menu allowing you to change the "Priority" field</li>
2852
</ul>
2953
`;
3054

@@ -52,6 +76,48 @@ export class GridAddItemComponent implements OnInit {
5276
this.grid.invalidate();
5377
this.grid.render();
5478
*/
79+
80+
const data = angularGrid.dataView.getItems();
81+
82+
const contextMenuTitle = 'Set priority';
83+
const contextMenuList = [
84+
{ option: 1, label: 'Low' },
85+
{ option: 2, label: 'Medium' },
86+
{ option: 3, label: 'High' },
87+
];
88+
89+
let liOptions = '';
90+
for (let i = 0; i < contextMenuList.length; i++) {
91+
if (contextMenuList.hasOwnProperty(i)) {
92+
const contextItem = contextMenuList[i];
93+
liOptions += `<li data="${contextItem.option}">${contextItem.label}</li>`;
94+
}
95+
}
96+
97+
const htmlString = `<ul id="contextMenu">
98+
<span class="title">${contextMenuTitle}</span>
99+
${liOptions}
100+
</ul>`;
101+
102+
// create context menu, hide it & append it to the body
103+
const contextElm = $(htmlString);
104+
contextElm.css('display', 'none');
105+
contextElm.appendTo($('body'));
106+
107+
contextElm.click((e: any) => {
108+
if (!$(e.target).is('li')) {
109+
return;
110+
}
111+
if (!this.grid.getEditorLock().commitCurrentEdit()) {
112+
return;
113+
}
114+
const row = contextElm.data('row');
115+
data[row].priority = $(e.target).attr('data');
116+
this.grid.updateRow(row);
117+
this.angularGrid.gridService.setSelectedRows([row]);
118+
119+
// callback, user might want to do something else
120+
});
55121
}
56122

57123
ngOnInit(): void {
@@ -115,6 +181,15 @@ export class GridAddItemComponent implements OnInit {
115181
formatter: Formatters.dateIso, sortable: true,
116182
type: FieldType.date
117183
},
184+
{
185+
id: 'priority',
186+
name: 'Priority',
187+
field: 'priority',
188+
minWidth: 100,
189+
filterable: true,
190+
sortable: true,
191+
formatter: customPriorityFormatter,
192+
},
118193
{
119194
id: 'effort-driven', name: 'Effort Driven', field: 'effortDriven',
120195
formatter: Formatters.checkmark,
@@ -153,6 +228,7 @@ export class GridAddItemComponent implements OnInit {
153228
percentCompleteNumber: randomPercent,
154229
start: new Date(randomYear, randomMonth, randomDay),
155230
finish: new Date(randomYear, (randomMonth + 1), randomDay),
231+
priority: i % 5 ? 1 : (i % 2 ? 2 : 3),
156232
effortDriven: (i % 5 === 0)
157233
};
158234
}
@@ -174,6 +250,7 @@ export class GridAddItemComponent implements OnInit {
174250
percentCompleteNumber: randomPercent,
175251
start: new Date(randomYear, randomMonth, randomDay),
176252
finish: new Date(randomYear, (randomMonth + 2), randomDay),
253+
priority: 1,
177254
effortDriven: true
178255
};
179256
this.angularGrid.gridService.addItem(newItem, { position: insertPosition });
@@ -249,4 +326,20 @@ export class GridAddItemComponent implements OnInit {
249326
scrollGridTop() {
250327
this.angularGrid.slickGrid.navigateTop();
251328
}
329+
330+
onContextMenu(e) {
331+
e.preventDefault();
332+
const cell = this.angularGrid.slickGrid.getCellFromEvent(e);
333+
const contextElm = $('#contextMenu');
334+
335+
// reposition the context menu where we right+clicked
336+
contextElm
337+
.data('row', cell.row)
338+
.css('top', e.pageY)
339+
.css('left', e.pageX)
340+
.show();
341+
342+
// hide the context menu once user choose an option
343+
$('body').one('click', () => contextElm.hide());
344+
}
252345
}

0 commit comments

Comments
 (0)