Project

General

Profile

Form List - Customers » History » Version 3

Lê Sĩ Quý, 08/29/2025 06:39 PM

1 1 Lê Sĩ Quý
# Form List - Customers
2
3
{{TOC}}
4
5
## JSON Schema
6
7
Form List không dùng FormIO vì không có input data, đơn thuần hiển thị danh sách dữ liệu từ spreadsheet. **Nếu dữ liệu chỉ giới hạn truy cập bởi ít người hoặc không cần public thì có thể quản lý data trực tiếp bằng spreadsheet.**
8
9
## HTML Template
10
11
``` html
12
<!DOCTYPE html>
13
<html lang="en">
14
15
<head>
16
    <meta name="viewport"
17
        content="user-scalable=no, initial-scale=1.0001, maximum-scale=1.0001, width=device-width, minimal-ui shrink-to-fit=no">
18
    <meta charset="utf-8" name="viewport" content="width=device-width, initial-scale=1.0" />
19
    <title>Northwind</title>
20
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/awesome-notifications/3.1.0/style.min.css">
21
    <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/w2ui@2.0.0/w2ui-2.0.min.css">
22
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
23
    <script src="https://cdnjs.cloudflare.com/ajax/libs/awesome-notifications/3.1.0/modern.var.min.js"></script>
24
    <script src="https://cdn.jsdelivr.net/npm/localstorage-slim"></script>
25
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
26
    <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>
27
</head>
28
29
<body>
30
    <div id="toolbar"></div>
31
    <div style="position: relative; height: 800px;">
32
        <div id="master" style="display: inline-block; width: 100%; height: 100%;"></div>
33
    </div>
34
35
    <style>
36
        .category-img {
37
            object-fit: cover;
38
            width: 100%;
39
            max-height: 100%;
40
        }
41
    </style>
42
43
    <script type="module">
44
        import { w2toolbar, w2grid } from "https://cdn.jsdelivr.net/npm/w2ui@2.0.0/w2ui-2.0.es6.min.js";
45
        var notifier = new AWN({ option: "top-right" });
46
        var sid = ls.get("sid");
47
        var email = ls.get("email");
48
        var $ds = {};
49
        var master;
50
51
        $(function () {
52
            new w2toolbar({
53
                box: "#toolbar",
54
                name: "toolbar",
55
                items: [
56
                    {
57
                        type: "menu", id: "sales", icon: "w2ui-icon-info", text: "Sales",
58
                        items: [
59
                            { id: "customers", text: "Customers", icon: "fa fa-user" },
60
                            { id: "orders", text: "Orders", icon: "fa fa-file-text" },
61
                        ]
62
                    },
63
                    { type: 'break' },
64
                    {
65
                        type: "menu", id: "operations", icon: "w2ui-icon-settings", text: "Operations",
66
                        items: [
67
                            { id: "employees", text: "Employees", icon: "fa fa-address-card" },
68
                            { id: "products", text: "Products", icon: "fa fa-archive" },
69
                            { id: "categories", text: "Product Categories", icon: "fa fa-sitemap" },
70
                            { id: "suppliers", text: "Suppliers", icon: "fa fa-user" },
71
                            { id: "shippers", text: "Shippers", icon: "fa fa-shopping-cart" }
72
                        ]
73
                    },
74
                    { type: "spacer" },
75
                    {
76
                        type: "menu", id: "auth", text: email,
77
                        items: [
78
                            { text: "Logout", id: "logout", icon: "fa fa-sign-out" }
79
                        ]
80
                    }
81
                ],
82
                onClick(event) {
83
                    switch (event.target) {
84
                        case "sales:customers": {
85
                            window.open("<?= base ?>?url=/form/customers", "_top");
86
                            break;
87
                        }
88
                        case "sales:orders": {
89
                            window.open("<?= base ?>?url=/form/orders", "_top");
90
                            break;
91
                        }
92
                        case "operations:employees": {
93
                            window.open("<?= base ?>?url=/form/employees", "_top");
94
                            break;
95
                        }
96
                        case "operations:products": {
97
                            window.open("<?= base ?>?url=/form/products", "_top");
98
                            break;
99
                        }
100
                        case "operations:categories": {
101
                            window.open("<?= base ?>?url=/form/categories", "_top");
102
                            break;
103
                        }
104
                        case "operations:suppliers": {
105
                            window.open("<?= base ?>?url=/form/suppliers", "_top");
106
                            break;
107
                        }
108
                        case "operations:shippers": {
109
                            window.open("<?= base ?>?url=/form/shippers", "_top");
110
                            break;
111
                        }
112
                        case "auth:logout": {
113
                            ls.remove("sid");
114
                            ls.remove("email");
115
                            window.open("<?= base ?>?url=/form/login&callback=<?!= url ?>", "_top");
116
                            break;
117
                        }
118
                    }
119
                }
120
            });
121
            master = new w2grid({
122
                name: "master",
123
                box: "#master",
124
                header: "Customers",
125
                show: {
126
                    header: true,
127
                    footer: true,
128
                    toolbar: true,
129
                    toolbarAdd: true,
130
                    toolbarEdit: true
131
                },
132
                columns: [
133
                    { field: "recid", hidden: true },
134
                    { field: "CompanyName", text: "Company Name", size: "20%", sortable: true },
135
                    { field: "CustomerID", text: "Customer ID", size: "10%", sortable: true },
136
                    { field: "Contact Name", text: "ContactName", size: "20%", sortable: true },
137
                    { field: "Address", text: "Address", size: "20%", sortable: true },
138
                    { field: "City", text: "City", size: "10%", sortable: true },
139
                    { field: "Region", text: "Region", size: "10%", sortable: true },
140
                    { field: "Country", text: "Country", size: "10%", sortable: true }
141
                ],
142
                searches: [
143
                    { type: "text", field: "CompanyName", label: "Company Name" },
144
                    { type: "text", field: "CustomerID", label: "Customer ID" },
145
                    { type: "text", field: "ContactName", label: "Contact Name" },
146
                    { type: "text", field: "Address", label: "Address" },
147
                    { type: "text", field: "City", label: "City" },
148
                    { type: "text", field: "Region", label: "Region" },
149
                    { type: "text", field: "Country", label: "Country" }
150
                ],
151
                onClick(event) {
152
                    let record = this.get(event.detail.recid);
153
154
                    if (record) {
155
                        // Do nothing
156
                    }
157
                },
158
                onAdd: function (event) {
159
                    window.open('<?= base ?>?url=/form/crud-customer', '_blank');
160
                },
161
                onEdit: function (event) {
162
                    let recid = event.detail.recid;
163
                    recid && window.open('<?= base ?>?url=/form/crud-customer/' + recid, '_blank');
164
                },
165
            });
166
167
            if (sid) {
168
                google.script.run
169
                    .withSuccessHandler(onCheckSidSuccess)
170
                    .withFailureHandler(onInvalidSid)
171
                    .checkSid(sid);
172
            }
173
            else {
174
                onInvalidSid();
175
            }
176
        });
177
178
        function onInvalidSid(err) {
179
            if (err) {
180
                notifier.warning(err.message);
181
            }
182
            else {
183
                let url = "<?!= base ?>?url=/form/login&callback=<?!= url ?>";
184
                notifier.modal(`<b>You need to login to access this page.</b><br><a href="${url}">👉 <b>Login</b></a>`)
185
            }
186
        }
187
188
        function onCheckSidSuccess(valid) {
189
            if (valid) {
190
                google.script.run.withSuccessHandler((ds) => {
191
                    $ds = ds;
192
                    google.script.run.withSuccessHandler((parents => {
193
                        master.clear();
194
                        let recs = [];
195
                        parents.forEach((parent) => {
196
                            let obj = JSON.parse(parent.Document);
197
                            obj.recid = parent.Id;
198
                            recs.push(obj);
199
                        });
200
                        master.add(recs);
201
                    }))
202 3 Lê Sĩ Quý
                        .runSql("Default", "where G = 0 and H = 'Customers' options no_format");
203 1 Lê Sĩ Quý
                })
204
                    .loadDatasources("<?!= datasource ?>");
205
206
            } else {
207
                onInvalidSid();
208
            }
209
        }
210
    </script>
211
212
</body>
213
214
</html>
215
```
216
217
## Giải thích Code
218
219
Form List Customers sử dụng cácthư viện sau:
220
- [jQuery](https://jquery.com/) để khởi tạo grid sau khi page load xong.
221
- [localstorage-slim](https://github.com/digitalfortress-tech/localstorage-slim) để đọc sid & email để bỏ qua bước login nếu trước đó đã login thành công. Trường hợp login không thành công thì sẽ thông báo và điều hướng đến trang login.
222
- [Notyf](https://carlosroso.com/notyf/) hiển thị thông báo.
223
- [w2ui](https://w2ui.com/web/home) cung cấp các thành phần UI cơ bản và nâng cao, ví dụ: toolbar và grid, dialog...
224
- Các thư viện đều được load từ CDN để tối ưu tốc độ, đơn giản hóa HTML Templated. Các bạn cần có kiến thức trong phần yêu cầu để hiểu logic của code JS + templated nhúng trong page. 
225
- Trong page này gọi đến
226
  - Hàm backend **checkSid** sid lưu trữ có khớp với giá trị trên server, nếu đúng thì sẽ sử dụng email trả về, còn sai sẽ điều hướng đến trang login
227
  - Hàm backend **loadDatasources** để tải các dữ liệu tham chiếu phía client, ví dụ danh sách Products
228
  - Hàm backend **runSql** thực hiện query nâng cao trên Datasource chỉ định, ví dụ chỉ hiển thị ra danh sách Orders được tạo bởi Saler đang login
229
230
*Vì Customers được lưu trong dạng document với cơ chế NoSQL with Google Forms nên chúng ta phải có bước tách các thuộc tính của Employee rồi mới thực hiện việc binding thay vì binding trực tiếp như Categories, Shippers*
231
232
``` javascript
233
let obj = JSON.parse(parent.Document);
234
```
235
236
## Demo
237 2 Lê Sĩ Quý
- Link: https://script.google.com/macros/s/AKfycbwIjB-hULVZdfCtsXFPg4Af_8WoKx2AFf85KMVwnsO_WkeAXW3zarT6vZNFVfwccz1_sA/exec?url=/form/orders
238 1 Lê Sĩ Quý
- Account: user@northwind.com
239
- Password: user