Skip to content

Commit d09e2fb

Browse files
feat: support more JavaScript than just function in grid_options (#2)
1 parent 6177f24 commit d09e2fb

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "a309447d",
6+
"metadata": {},
7+
"source": [
8+
"# Use javascript in `grid_options` by using `__js__:` prefix"
9+
]
10+
},
11+
{
12+
"cell_type": "code",
13+
"execution_count": null,
14+
"id": "614ccb50",
15+
"metadata": {},
16+
"outputs": [],
17+
"source": [
18+
"from ipyaggrid import Grid\n",
19+
"\n",
20+
"grid = Grid(\n",
21+
" css_rules=\"\"\"\n",
22+
" .ag-cell-not-inline-editing.price-high {\n",
23+
" background-color: red;\n",
24+
" color: white;\n",
25+
" }\n",
26+
" .my-tooltip {\n",
27+
" background-color: white;\n",
28+
" border: 1px solid black;\n",
29+
" padding: 8px;\n",
30+
" }\n",
31+
" \"\"\",\n",
32+
" grid_data=[\n",
33+
" {\"make\": \"Toyota\", \"model\": \"Celica\", \"price\": 35000},\n",
34+
" {\"make\": \"Ford\", \"model\": \"Mondeo\", \"price\": 32000},\n",
35+
" {\"make\": \"Porsche\", \"model\": \"Boxster\", \"price\": 72000},\n",
36+
" ],\n",
37+
" grid_options={\n",
38+
" 'columnDefs': [\n",
39+
" {\"headerName\": \"Make\", \"field\": \"make\", \"editable\": True},\n",
40+
" {\"headerName\": \"Model\", \"field\": \"model\", \"editable\": True},\n",
41+
" {\"headerName\": \"Price\", \"field\": \"price\", \"editable\": True, 'type': 'numericColumn',\n",
42+
" 'cellClass': \"\"\"function(params) {\n",
43+
" return params.value >32000 ? 'price-high' : '';\n",
44+
" }\"\"\",\n",
45+
" \"tooltipField\": 'price',\n",
46+
" \"tooltipComponent\": \"\"\"__js__: class {\n",
47+
" init(params) {\n",
48+
" const eGui = this.eGui = document.createElement('div');\n",
49+
" \n",
50+
" if (params.value > 32000) {\n",
51+
" eGui.classList.add('my-tooltip');\n",
52+
" eGui.innerHTML = 'value to high'; \n",
53+
" }\n",
54+
" }\n",
55+
" \n",
56+
" getGui() {\n",
57+
" return this.eGui;\n",
58+
" }\n",
59+
" }\"\"\",\n",
60+
" }\n",
61+
" ],\n",
62+
" 'tooltipShowDelay': 0,\n",
63+
" 'tooltipHideDelay': 2000,\n",
64+
" },\n",
65+
")\n",
66+
"\n",
67+
"grid"
68+
]
69+
},
70+
{
71+
"cell_type": "code",
72+
"execution_count": null,
73+
"id": "2ffb64ae",
74+
"metadata": {},
75+
"outputs": [],
76+
"source": []
77+
}
78+
],
79+
"metadata": {
80+
"kernelspec": {
81+
"display_name": "Python 3 (ipykernel)",
82+
"language": "python",
83+
"name": "python3"
84+
},
85+
"language_info": {
86+
"codemirror_mode": {
87+
"name": "ipython",
88+
"version": 3
89+
},
90+
"file_extension": ".py",
91+
"mimetype": "text/x-python",
92+
"name": "python",
93+
"nbconvert_exporter": "python",
94+
"pygments_lexer": "ipython3",
95+
"version": "3.9.13"
96+
}
97+
},
98+
"nbformat": 4,
99+
"nbformat_minor": 5
100+
}

js/src/widget_json.js

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ JSONfunc.parse = function(str, helpers) {
2020
} else if (valueCompact.substring(0, 8) === 'helpers.') {
2121
r = eval(value);
2222
return r;
23+
} else if (valueCompact.startsWith('__js__:')) {
24+
return Function(`"use strict"; return (${value.slice(7)})`)();
2325
}
2426
return value;
2527
});

0 commit comments

Comments
 (0)