71d3cabca4869fd75406371559333d3800d61b7e
.redirects.gollum
| ... | ... | @@ -17,3 +17,4 @@ Many2One不需要填写依赖和允许跨App安装.md: Many2One跨App安装.md |
| 17 | 17 | 基础APP/云边协同(iidp-cloud-edge).md: 基础APP/云边协同(sie-iidp-cloud-edge).md |
| 18 | 18 | 常见问题/mi-update-front-end-extension-does-not-work.md: 常见问题/MI前端扩展不生效问题和Oracle驱动加载失败问题.md |
| 19 | 19 | 常见问题/seed-data.md: 常见问题/重置种子数据.md |
| 20 | +常见问题/视图扩展.md: 常见问题/后端视图扩展.md |
\345\270\270\350\247\201\351\227\256\351\242\230/\345\220\216\347\253\257\350\247\206\345\233\276\346\211\251\345\261\225.md
| ... | ... | @@ -0,0 +1,394 @@ |
| 1 | +# 后端视图扩展 |
|
| 2 | + |
|
| 3 | +在开发中,常常需要对已有的视图进行扩展,例如增加字段、添加标签页等。以下是一个详细的示例和说明,展示如何通过 JSONPath 对视图进行扩展。 |
|
| 4 | + |
|
| 5 | +## 1. 语法示例 |
|
| 6 | + |
|
| 7 | +```json |
|
| 8 | +{ |
|
| 9 | + "jsonpath": [ |
|
| 10 | + { |
|
| 11 | + "has_not": "tars", |
|
| 12 | + "expr": "tbar.打印", |
|
| 13 | + "position": "after", |
|
| 14 | + "body": [ |
|
| 15 | + { |
|
| 16 | + "action": "create", |
|
| 17 | + "auth": "create", |
|
| 18 | + "name": "无单据生成", |
|
| 19 | + "actionAfter": "refreshTable" |
|
| 20 | + } |
|
| 21 | + ] |
|
| 22 | + }, |
|
| 23 | + { |
|
| 24 | + "expr": "columns.repairDate", |
|
| 25 | + "position": "before", |
|
| 26 | + "body": [ |
|
| 27 | + { |
|
| 28 | + "displayName": "报修单号2扩展", |
|
| 29 | + "name": "reportNo2", |
|
| 30 | + "groupConf": { |
|
| 31 | + "name": "故障信息", |
|
| 32 | + "id": "repairInfoGroup" |
|
| 33 | + } |
|
| 34 | + } |
|
| 35 | + ] |
|
| 36 | + }, |
|
| 37 | + { |
|
| 38 | + "expr": "buttons.编辑", |
|
| 39 | + "position": "before", |
|
| 40 | + "body": [ |
|
| 41 | + { |
|
| 42 | + "action": "delete", |
|
| 43 | + "auth": "delete", |
|
| 44 | + "name": "删除" |
|
| 45 | + } |
|
| 46 | + ] |
|
| 47 | + } |
|
| 48 | + ] |
|
| 49 | +} |
|
| 50 | + |
|
| 51 | +``` |
|
| 52 | + |
|
| 53 | +**语法介绍** |
|
| 54 | + |
|
| 55 | +- **`has_not`**: 判断某个字段或属性是否存在。如果不存在,则执行创建属性。 |
|
| 56 | +- **`expr`**: 指定要扩展的位置,支持简略语法: |
|
| 57 | + - `columns.name`、`buttons.name`、`tbar.name`:通过 `name` 属性定位。 |
|
| 58 | + - 示例:`tabs[?(@.name=='item_info_tab')]..columns.desc`。 |
|
| 59 | +- **`position`**: 指定插入位置,可选值: |
|
| 60 | + - `after`:插入在指定位置之后。 |
|
| 61 | + - `before`:插入在指定位置之前。 |
|
| 62 | + - `replace`:替换内容。 |
|
| 63 | + - `attributes`:添加属性。 |
|
| 64 | +- **`body`**: 扩展的内容,可以是字段、按钮、标签页等。 |
|
| 65 | + |
|
| 66 | + |
|
| 67 | + |
|
| 68 | +## 2. 编写 A 视图 |
|
| 69 | + |
|
| 70 | +声明菜单,view 指定视图的类型 |
|
| 71 | + |
|
| 72 | +```json |
|
| 73 | +"mom_material_menu": { |
|
| 74 | + "name": "mom_material_menu", |
|
| 75 | + "display_name": "物料", |
|
| 76 | + "model": "Material", |
|
| 77 | + "view": "material_grid,material_search,material_form", |
|
| 78 | + "sequence": 2, |
|
| 79 | + "active": true, |
|
| 80 | + "parent_ids": { |
|
| 81 | + "@ref": "mom_config_menu" |
|
| 82 | + } |
|
| 83 | +} |
|
| 84 | +``` |
|
| 85 | + |
|
| 86 | +### 2.1 声明 A 的表单视图,这里定义了一个标签页 |
|
| 87 | + |
|
| 88 | +```json |
|
| 89 | +{ |
|
| 90 | + "material_form": { |
|
| 91 | + "body": { |
|
| 92 | + "columns": [], |
|
| 93 | + "tabs": [ |
|
| 94 | + { |
|
| 95 | + "header": "基本资料", |
|
| 96 | + "rowspan": 1, |
|
| 97 | + "name": "item_info_tab", |
|
| 98 | + "body": { |
|
| 99 | + "type": "formPart", |
|
| 100 | + "model": "Material", |
|
| 101 | + "columns": [] |
|
| 102 | + } |
|
| 103 | + } |
|
| 104 | + { |
|
| 105 | + "header": "设计资料", |
|
| 106 | + "rowspan": 1, |
|
| 107 | + "name": "item_design_info_tab", |
|
| 108 | + "body": { |
|
| 109 | + "type": "formPart", |
|
| 110 | + "model": "Material", |
|
| 111 | + "columns": [ |
|
| 112 | + ] |
|
| 113 | + } |
|
| 114 | + } |
|
| 115 | + ], |
|
| 116 | + "type": "form" |
|
| 117 | + }, |
|
| 118 | + "mode": "primary", |
|
| 119 | + "model": "Material", |
|
| 120 | + "name": "物料-表单", |
|
| 121 | + "type": "form" |
|
| 122 | + } |
|
| 123 | +} |
|
| 124 | +``` |
|
| 125 | + |
|
| 126 | +### 2.2 在B app声明扩展视图 |
|
| 127 | + |
|
| 128 | +```json |
|
| 129 | +{ |
|
| 130 | + "views": { |
|
| 131 | + "aps_buyer_tab_form_ext": { |
|
| 132 | + "name": "mom-item扩展-采购资料tab", |
|
| 133 | + "model": "Material", |
|
| 134 | + "type": "form", |
|
| 135 | + "mode": "extension", # 填扩展类型 |
|
| 136 | + "inherit_ids": { |
|
| 137 | + "@ref": "siemes.material_form" # 指定对应 siemes app 要扩展的视图 |
|
| 138 | + }, |
|
| 139 | + "body": { |
|
| 140 | + "jsonpath": [ |
|
| 141 | + { |
|
| 142 | + "expr": "tabs[?(@.name=='item_info_tab')]..columns.desc", # 扩展的位置,通过 name 找到 tab 里面的字段 |
|
| 143 | + "position": "before", # 扩展的位置 |
|
| 144 | + "body": [ |
|
| 145 | + "zhichengluxiam" # 扩展的内容,新增一个字段 |
|
| 146 | + ] |
|
| 147 | + }, |
|
| 148 | + { |
|
| 149 | + "expr": "tabs[?(@.name=='item_info_tab')]..columns.shengchantiqianqi", |
|
| 150 | + "position": "after", |
|
| 151 | + "body": [ |
|
| 152 | + "gongyicanshufenlei" |
|
| 153 | + ] |
|
| 154 | + }, |
|
| 155 | + { |
|
| 156 | + "has_not": "tabs", # 判断是否有 tabs |
|
| 157 | + "expr": "tabs[?(@.name=='item_design_info_tab')]", # 找到指定的 tab |
|
| 158 | + "position": "after", |
|
| 159 | + "body": [ # 扩展的内容 |
|
| 160 | + { |
|
| 161 | + "header": "采购资料", |
|
| 162 | + "body": { |
|
| 163 | + "type": "formPart", |
|
| 164 | + "model": "Material", |
|
| 165 | + "columns": [ |
|
| 166 | + "caigouziliao", |
|
| 167 | + "caigouyuan", |
|
| 168 | + "caigoutiqianqi" |
|
| 169 | + ] |
|
| 170 | + } |
|
| 171 | + } |
|
| 172 | + ] |
|
| 173 | + } |
|
| 174 | + ] |
|
| 175 | + } |
|
| 176 | + } |
|
| 177 | + } |
|
| 178 | +} |
|
| 179 | +``` |
|
| 180 | + |
|
| 181 | +## 3. 示例: |
|
| 182 | +1. 扩展表单,给表单的一个字段后面新增一个字段 |
|
| 183 | +2. 给表单的tab页新增一个字段 |
|
| 184 | +```json |
|
| 185 | +{ |
|
| 186 | + "views": { |
|
| 187 | + "test_eam_fault_maintenance_order_form_ext": { |
|
| 188 | + "mode": "extension", |
|
| 189 | + "inherit_ids": { |
|
| 190 | + "@ref": "newSdkApp.test_eam_fault_maintenance_order_form" |
|
| 191 | + }, |
|
| 192 | + "model": "test_eam_fault_maintenance_order", |
|
| 193 | + "name": "故障工单-表单", |
|
| 194 | + "type": "form", |
|
| 195 | + "body": { |
|
| 196 | + "jsonpath": [ |
|
| 197 | + { |
|
| 198 | + "expr": "columns.repairDate", |
|
| 199 | + "position": "after", |
|
| 200 | + "body": [ |
|
| 201 | + { |
|
| 202 | + "displayName": "报修单号2扩展", |
|
| 203 | + "name": "reportNo2", |
|
| 204 | + "groupConf": { |
|
| 205 | + "name": "故障信息", |
|
| 206 | + "id": "repairInfoGroup" |
|
| 207 | + } |
|
| 208 | + } |
|
| 209 | + ] |
|
| 210 | + }, |
|
| 211 | + { |
|
| 212 | + "expr": "tabs[?(@.name=='maintenanceInformationTab')]..columns.responseTime", |
|
| 213 | + "position": "after", |
|
| 214 | + "body": [ |
|
| 215 | + { |
|
| 216 | + "displayName": "报修单号acceptedBy扩展", |
|
| 217 | + "name": "reportNo2" |
|
| 218 | + } |
|
| 219 | + ] |
|
| 220 | + } |
|
| 221 | + ] |
|
| 222 | + } |
|
| 223 | + } |
|
| 224 | + } |
|
| 225 | +} |
|
| 226 | + |
|
| 227 | +``` |
|
| 228 | + |
|
| 229 | +## 4. 常用 `expr` 示例 |
|
| 230 | +- **新增一个字段**: |
|
| 231 | + |
|
| 232 | + ```json |
|
| 233 | + { |
|
| 234 | + "expr": "columns.fieldName", |
|
| 235 | + "position": "after", |
|
| 236 | + "body": [ |
|
| 237 | + { |
|
| 238 | + "name": "newField", |
|
| 239 | + "displayName": "新字段" |
|
| 240 | + } |
|
| 241 | + ] |
|
| 242 | + } |
|
| 243 | + ``` |
|
| 244 | + |
|
| 245 | +- **替换一个字段**: |
|
| 246 | + |
|
| 247 | + ```json |
|
| 248 | + { |
|
| 249 | + "expr": "columns.fieldName", |
|
| 250 | + "position": "replace", |
|
| 251 | + "body": [ |
|
| 252 | + { |
|
| 253 | + "name": "replacedField", |
|
| 254 | + "displayName": "替换字段" |
|
| 255 | + } |
|
| 256 | + ] |
|
| 257 | + } |
|
| 258 | + ``` |
|
| 259 | + |
|
| 260 | +- **新增一个按钮**: |
|
| 261 | + |
|
| 262 | + ```json |
|
| 263 | + { |
|
| 264 | + "expr": "buttons.删除", |
|
| 265 | + "position": "after", |
|
| 266 | + "body": [ |
|
| 267 | + { |
|
| 268 | + "name": "newButton", |
|
| 269 | + "displayName": "新按钮" |
|
| 270 | + } |
|
| 271 | + ] |
|
| 272 | + } |
|
| 273 | + ``` |
|
| 274 | + |
|
| 275 | +- **新增一个标签页**: |
|
| 276 | + |
|
| 277 | + ```json |
|
| 278 | + { |
|
| 279 | + "has_not": "tabs", |
|
| 280 | + "expr": "tabs[?(@.name=='item_design_info_tab')]", |
|
| 281 | + "position": "after", |
|
| 282 | + "body": [ |
|
| 283 | + { |
|
| 284 | + "header": "采购资料", |
|
| 285 | + "body": { |
|
| 286 | + "type": "formPart", |
|
| 287 | + "model": "Material", |
|
| 288 | + "columns": [ |
|
| 289 | + "caigouziliao", |
|
| 290 | + "caigouyuan", |
|
| 291 | + "caigoutiqianqi" |
|
| 292 | + ] |
|
| 293 | + } |
|
| 294 | + } |
|
| 295 | + ] |
|
| 296 | + } |
|
| 297 | + ``` |
|
| 298 | + |
|
| 299 | +## 5. JsonPath语法 |
|
| 300 | + |
|
| 301 | +JsonPath的语法相对简单,它采用开发语言友好的表达式形式,如果你了解类C语言,对JsonPath就不会感到不适应。 |
|
| 302 | +JsonPath语法要点: |
|
| 303 | + |
|
| 304 | +- `$` 表示文档的根元素 |
|
| 305 | +- `@` 表示文档的当前元素 |
|
| 306 | +- `.node_name` 或 `['node_name']` 匹配下级节点 |
|
| 307 | +- `[index]` 检索数组中的元素 |
|
| 308 | +- `[start:end:step]` 支持数组切片语法 |
|
| 309 | +- `*` 作为通配符,匹配所有成员 |
|
| 310 | +- `..` 子递归通配符,匹配成员的所有子元素 |
|
| 311 | +- `(<expr>)` 使用表达式 |
|
| 312 | +- `?(<boolean expr>)`进行数据筛选 |
|
| 313 | + |
|
| 314 | +下表将列举所有支持的语法,并对XPath进行比较: |
|
| 315 | + |
|
| 316 | +| XPath | JsonPath | 说明 | |
|
| 317 | +| ----- | ------------------ | ------------------------------------------------------------ | |
|
| 318 | +| `/` | `$` | 文档根元素 | |
|
| 319 | +| `.` | `@` | 当前元素 | |
|
| 320 | +| `/` | `.`或`[]` | 匹配下级元素 | |
|
| 321 | +| `..` | `N/A` | 匹配上级元素,JsonPath不支持此操作符 | |
|
| 322 | +| `//` | `..` | 递归匹配所有子元素 | |
|
| 323 | +| `*` | `*` | 通配符,匹配下级元素 | |
|
| 324 | +| `@` | `N/A` | 匹配属性,JsonPath不支持此操作符 | |
|
| 325 | +| `[]` | `[]` | 下标运算符,根据索引获取元素,**XPath索引从1开始,JsonPath索引从0开始** | |
|
| 326 | +| `|` | `[,]` | 连接操作符,将多个结果拼接成数组返回,可以使用索引或别名 | |
|
| 327 | +| `N/A` | `[start:end:step]` | 数据切片操作,XPath不支持 | |
|
| 328 | +| `[]` | `?()` | 过滤表达式 | |
|
| 329 | +| `N/A` | `()` | 脚本表达式,使用底层脚本引擎,XPath不支持 | |
|
| 330 | +| `()` | `N/A` | 分组,JsonPath不支持 | |
|
| 331 | + |
|
| 332 | +注意: |
|
| 333 | + |
|
| 334 | +- JsonPath的索引从0开始计数 |
|
| 335 | +- JsonPath中字符串使用单引号表示,例如:`$.store.book[?(@.category=='reference')]`中的`'reference'` |
|
| 336 | + |
|
| 337 | +## JsonPath示例 |
|
| 338 | + |
|
| 339 | +下面是相应的JsonPath的示例,代码来源于[https://goessner.net/articles/JsonPath/](https://links.jianshu.com/go?to=https%3A%2F%2Fgoessner.net%2Farticles%2FJsonPath%2F),JSON文档如下: |
|
| 340 | + |
|
| 341 | + |
|
| 342 | + |
|
| 343 | +```json |
|
| 344 | +{ |
|
| 345 | + "store": { |
|
| 346 | + "book": [{ |
|
| 347 | + "category": "reference", |
|
| 348 | + "author": "Nigel Rees", |
|
| 349 | + "title": "Sayings of the Century", |
|
| 350 | + "price": 8.95 |
|
| 351 | + }, { |
|
| 352 | + "category": "fiction", |
|
| 353 | + "author": "Evelyn Waugh", |
|
| 354 | + "title": "Sword of Honour", |
|
| 355 | + "price": 12.99 |
|
| 356 | + }, { |
|
| 357 | + "category": "fiction", |
|
| 358 | + "author": "Herman Melville", |
|
| 359 | + "title": "Moby Dick", |
|
| 360 | + "isbn": "0-553-21311-3", |
|
| 361 | + "price": 8.99 |
|
| 362 | + }, { |
|
| 363 | + "category": "fiction", |
|
| 364 | + "author": "J. R. R. Tolkien", |
|
| 365 | + "title": "The Lord of the Rings", |
|
| 366 | + "isbn": "0-395-19395-8", |
|
| 367 | + "price": 22.99 |
|
| 368 | + } |
|
| 369 | + ], |
|
| 370 | + "bicycle": { |
|
| 371 | + "color": "red", |
|
| 372 | + "price": 19.95 |
|
| 373 | + } |
|
| 374 | + } |
|
| 375 | +} |
|
| 376 | +``` |
|
| 377 | + |
|
| 378 | +接下来我们看一下如何对这个文档进行解析: |
|
| 379 | + |
|
| 380 | +| XPath | JsonPath | Result | |
|
| 381 | +| ---------------------- | ------------------------------------------ | ---------------------------------------- | |
|
| 382 | +| `/store/book/author` | `$.store.book[*].author` | 所有book的author节点 | |
|
| 383 | +| `//author` | `$..author` | 所有author节点 | |
|
| 384 | +| `/store/*` | `$.store.*` | store下的所有节点,book数组和bicycle节点 | |
|
| 385 | +| `/store//price` | `$.store..price` | store下的所有price节点 | |
|
| 386 | +| `//book[3]` | `$..book[2]` | 匹配第3个book节点 | |
|
| 387 | +| `//book[last()]` | `$..book[(@.length-1)]`,或 `$..book[-1:]` | 匹配倒数第1个book节点 | |
|
| 388 | +| `//book[position()<3]` | `$..book[0,1]`,或 `$..book[:2]` | 匹配前两个book节点 | |
|
| 389 | +| `//book[isbn]` | `$..book[?(@.isbn)]` | 过滤含isbn字段的节点 | |
|
| 390 | +| `//book[price<10]` | `$..book[?(@.price<10)]` | 过滤`price<10`的节点 | |
|
| 391 | +| `//*` | `$..*` | 递归匹配所有子节点 | |
|
| 392 | + |
|
| 393 | + |
|
| 394 | + |
\345\270\270\350\247\201\351\227\256\351\242\230/\350\247\206\345\233\276\346\211\251\345\261\225.md
| ... | ... | @@ -1,394 +0,0 @@ |
| 1 | -# 后端视图扩展 |
|
| 2 | - |
|
| 3 | -在开发中,常常需要对已有的视图进行扩展,例如增加字段、添加标签页等。以下是一个详细的示例和说明,展示如何通过 JSONPath 对视图进行扩展。 |
|
| 4 | - |
|
| 5 | -## 1. 语法示例 |
|
| 6 | - |
|
| 7 | -```json |
|
| 8 | -{ |
|
| 9 | - "jsonpath": [ |
|
| 10 | - { |
|
| 11 | - "has_not": "tars", |
|
| 12 | - "expr": "tbar.打印", |
|
| 13 | - "position": "after", |
|
| 14 | - "body": [ |
|
| 15 | - { |
|
| 16 | - "action": "create", |
|
| 17 | - "auth": "create", |
|
| 18 | - "name": "无单据生成", |
|
| 19 | - "actionAfter": "refreshTable" |
|
| 20 | - } |
|
| 21 | - ] |
|
| 22 | - }, |
|
| 23 | - { |
|
| 24 | - "expr": "columns.repairDate", |
|
| 25 | - "position": "before", |
|
| 26 | - "body": [ |
|
| 27 | - { |
|
| 28 | - "displayName": "报修单号2扩展", |
|
| 29 | - "name": "reportNo2", |
|
| 30 | - "groupConf": { |
|
| 31 | - "name": "故障信息", |
|
| 32 | - "id": "repairInfoGroup" |
|
| 33 | - } |
|
| 34 | - } |
|
| 35 | - ] |
|
| 36 | - }, |
|
| 37 | - { |
|
| 38 | - "expr": "buttons.编辑", |
|
| 39 | - "position": "before", |
|
| 40 | - "body": [ |
|
| 41 | - { |
|
| 42 | - "action": "delete", |
|
| 43 | - "auth": "delete", |
|
| 44 | - "name": "删除" |
|
| 45 | - } |
|
| 46 | - ] |
|
| 47 | - } |
|
| 48 | - ] |
|
| 49 | -} |
|
| 50 | - |
|
| 51 | -``` |
|
| 52 | - |
|
| 53 | -**语法介绍** |
|
| 54 | - |
|
| 55 | -- **`has_not`**: 判断某个字段或属性是否存在。如果不存在,则执行创建属性。 |
|
| 56 | -- **`expr`**: 指定要扩展的位置,支持简略语法: |
|
| 57 | - - `columns.name`、`buttons.name`、`tbar.name`:通过 `name` 属性定位。 |
|
| 58 | - - 示例:`tabs[?(@.name=='item_info_tab')]..columns.desc`。 |
|
| 59 | -- **`position`**: 指定插入位置,可选值: |
|
| 60 | - - `after`:插入在指定位置之后。 |
|
| 61 | - - `before`:插入在指定位置之前。 |
|
| 62 | - - `replace`:替换内容。 |
|
| 63 | - - `attributes`:添加属性。 |
|
| 64 | -- **`body`**: 扩展的内容,可以是字段、按钮、标签页等。 |
|
| 65 | - |
|
| 66 | - |
|
| 67 | - |
|
| 68 | -## 2. 编写 A 视图 |
|
| 69 | - |
|
| 70 | -声明菜单,view 指定视图的类型 |
|
| 71 | - |
|
| 72 | -```json |
|
| 73 | -"mom_material_menu": { |
|
| 74 | - "name": "mom_material_menu", |
|
| 75 | - "display_name": "物料", |
|
| 76 | - "model": "Material", |
|
| 77 | - "view": "material_grid,material_search,material_form", |
|
| 78 | - "sequence": 2, |
|
| 79 | - "active": true, |
|
| 80 | - "parent_ids": { |
|
| 81 | - "@ref": "mom_config_menu" |
|
| 82 | - } |
|
| 83 | -} |
|
| 84 | -``` |
|
| 85 | - |
|
| 86 | -### 2.1 声明 A 的表单视图,这里定义了一个标签页 |
|
| 87 | - |
|
| 88 | -```json |
|
| 89 | -{ |
|
| 90 | - "material_form": { |
|
| 91 | - "body": { |
|
| 92 | - "columns": [], |
|
| 93 | - "tabs": [ |
|
| 94 | - { |
|
| 95 | - "header": "基本资料", |
|
| 96 | - "rowspan": 1, |
|
| 97 | - "name": "item_info_tab", |
|
| 98 | - "body": { |
|
| 99 | - "type": "formPart", |
|
| 100 | - "model": "Material", |
|
| 101 | - "columns": [] |
|
| 102 | - } |
|
| 103 | - } |
|
| 104 | - { |
|
| 105 | - "header": "设计资料", |
|
| 106 | - "rowspan": 1, |
|
| 107 | - "name": "item_design_info_tab", |
|
| 108 | - "body": { |
|
| 109 | - "type": "formPart", |
|
| 110 | - "model": "Material", |
|
| 111 | - "columns": [ |
|
| 112 | - ] |
|
| 113 | - } |
|
| 114 | - } |
|
| 115 | - ], |
|
| 116 | - "type": "form" |
|
| 117 | - }, |
|
| 118 | - "mode": "primary", |
|
| 119 | - "model": "Material", |
|
| 120 | - "name": "物料-表单", |
|
| 121 | - "type": "form" |
|
| 122 | - } |
|
| 123 | -} |
|
| 124 | -``` |
|
| 125 | - |
|
| 126 | -### 2.2 在B app声明扩展视图 |
|
| 127 | - |
|
| 128 | -```json |
|
| 129 | -{ |
|
| 130 | - "views": { |
|
| 131 | - "aps_buyer_tab_form_ext": { |
|
| 132 | - "name": "mom-item扩展-采购资料tab", |
|
| 133 | - "model": "Material", |
|
| 134 | - "type": "form", |
|
| 135 | - "mode": "extension", # 填扩展类型 |
|
| 136 | - "inherit_ids": { |
|
| 137 | - "@ref": "siemes.material_form" # 指定对应 siemes app 要扩展的视图 |
|
| 138 | - }, |
|
| 139 | - "body": { |
|
| 140 | - "jsonpath": [ |
|
| 141 | - { |
|
| 142 | - "expr": "tabs[?(@.name=='item_info_tab')]..columns.desc", # 扩展的位置,通过 name 找到 tab 里面的字段 |
|
| 143 | - "position": "before", # 扩展的位置 |
|
| 144 | - "body": [ |
|
| 145 | - "zhichengluxiam" # 扩展的内容,新增一个字段 |
|
| 146 | - ] |
|
| 147 | - }, |
|
| 148 | - { |
|
| 149 | - "expr": "tabs[?(@.name=='item_info_tab')]..columns.shengchantiqianqi", |
|
| 150 | - "position": "after", |
|
| 151 | - "body": [ |
|
| 152 | - "gongyicanshufenlei" |
|
| 153 | - ] |
|
| 154 | - }, |
|
| 155 | - { |
|
| 156 | - "has_not": "tabs", # 判断是否有 tabs |
|
| 157 | - "expr": "tabs[?(@.name=='item_design_info_tab')]", # 找到指定的 tab |
|
| 158 | - "position": "after", |
|
| 159 | - "body": [ # 扩展的内容 |
|
| 160 | - { |
|
| 161 | - "header": "采购资料", |
|
| 162 | - "body": { |
|
| 163 | - "type": "formPart", |
|
| 164 | - "model": "Material", |
|
| 165 | - "columns": [ |
|
| 166 | - "caigouziliao", |
|
| 167 | - "caigouyuan", |
|
| 168 | - "caigoutiqianqi" |
|
| 169 | - ] |
|
| 170 | - } |
|
| 171 | - } |
|
| 172 | - ] |
|
| 173 | - } |
|
| 174 | - ] |
|
| 175 | - } |
|
| 176 | - } |
|
| 177 | - } |
|
| 178 | -} |
|
| 179 | -``` |
|
| 180 | - |
|
| 181 | -## 3. 示例: |
|
| 182 | -1. 扩展表单,给表单的一个字段后面新增一个字段 |
|
| 183 | -2. 给表单的tab页新增一个字段 |
|
| 184 | -```json |
|
| 185 | -{ |
|
| 186 | - "views": { |
|
| 187 | - "test_eam_fault_maintenance_order_form_ext": { |
|
| 188 | - "mode": "extension", |
|
| 189 | - "inherit_ids": { |
|
| 190 | - "@ref": "newSdkApp.test_eam_fault_maintenance_order_form" |
|
| 191 | - }, |
|
| 192 | - "model": "test_eam_fault_maintenance_order", |
|
| 193 | - "name": "故障工单-表单", |
|
| 194 | - "type": "form", |
|
| 195 | - "body": { |
|
| 196 | - "jsonpath": [ |
|
| 197 | - { |
|
| 198 | - "expr": "columns.repairDate", |
|
| 199 | - "position": "after", |
|
| 200 | - "body": [ |
|
| 201 | - { |
|
| 202 | - "displayName": "报修单号2扩展", |
|
| 203 | - "name": "reportNo2", |
|
| 204 | - "groupConf": { |
|
| 205 | - "name": "故障信息", |
|
| 206 | - "id": "repairInfoGroup" |
|
| 207 | - } |
|
| 208 | - } |
|
| 209 | - ] |
|
| 210 | - }, |
|
| 211 | - { |
|
| 212 | - "expr": "tabs[?(@.name=='maintenanceInformationTab')]..columns.responseTime", |
|
| 213 | - "position": "after", |
|
| 214 | - "body": [ |
|
| 215 | - { |
|
| 216 | - "displayName": "报修单号acceptedBy扩展", |
|
| 217 | - "name": "reportNo2" |
|
| 218 | - } |
|
| 219 | - ] |
|
| 220 | - } |
|
| 221 | - ] |
|
| 222 | - } |
|
| 223 | - } |
|
| 224 | - } |
|
| 225 | -} |
|
| 226 | - |
|
| 227 | -``` |
|
| 228 | - |
|
| 229 | -## 4. 常用 `expr` 示例 |
|
| 230 | -- **新增一个字段**: |
|
| 231 | - |
|
| 232 | - ```json |
|
| 233 | - { |
|
| 234 | - "expr": "columns.fieldName", |
|
| 235 | - "position": "after", |
|
| 236 | - "body": [ |
|
| 237 | - { |
|
| 238 | - "name": "newField", |
|
| 239 | - "displayName": "新字段" |
|
| 240 | - } |
|
| 241 | - ] |
|
| 242 | - } |
|
| 243 | - ``` |
|
| 244 | - |
|
| 245 | -- **替换一个字段**: |
|
| 246 | - |
|
| 247 | - ```json |
|
| 248 | - { |
|
| 249 | - "expr": "columns.fieldName", |
|
| 250 | - "position": "replace", |
|
| 251 | - "body": [ |
|
| 252 | - { |
|
| 253 | - "name": "replacedField", |
|
| 254 | - "displayName": "替换字段" |
|
| 255 | - } |
|
| 256 | - ] |
|
| 257 | - } |
|
| 258 | - ``` |
|
| 259 | - |
|
| 260 | -- **新增一个按钮**: |
|
| 261 | - |
|
| 262 | - ```json |
|
| 263 | - { |
|
| 264 | - "expr": "buttons.删除", |
|
| 265 | - "position": "after", |
|
| 266 | - "body": [ |
|
| 267 | - { |
|
| 268 | - "name": "newButton", |
|
| 269 | - "displayName": "新按钮" |
|
| 270 | - } |
|
| 271 | - ] |
|
| 272 | - } |
|
| 273 | - ``` |
|
| 274 | - |
|
| 275 | -- **新增一个标签页**: |
|
| 276 | - |
|
| 277 | - ```json |
|
| 278 | - { |
|
| 279 | - "has_not": "tabs", |
|
| 280 | - "expr": "tabs[?(@.name=='item_design_info_tab')]", |
|
| 281 | - "position": "after", |
|
| 282 | - "body": [ |
|
| 283 | - { |
|
| 284 | - "header": "采购资料", |
|
| 285 | - "body": { |
|
| 286 | - "type": "formPart", |
|
| 287 | - "model": "Material", |
|
| 288 | - "columns": [ |
|
| 289 | - "caigouziliao", |
|
| 290 | - "caigouyuan", |
|
| 291 | - "caigoutiqianqi" |
|
| 292 | - ] |
|
| 293 | - } |
|
| 294 | - } |
|
| 295 | - ] |
|
| 296 | - } |
|
| 297 | - ``` |
|
| 298 | - |
|
| 299 | -## 5. JsonPath语法 |
|
| 300 | - |
|
| 301 | -JsonPath的语法相对简单,它采用开发语言友好的表达式形式,如果你了解类C语言,对JsonPath就不会感到不适应。 |
|
| 302 | -JsonPath语法要点: |
|
| 303 | - |
|
| 304 | -- `$` 表示文档的根元素 |
|
| 305 | -- `@` 表示文档的当前元素 |
|
| 306 | -- `.node_name` 或 `['node_name']` 匹配下级节点 |
|
| 307 | -- `[index]` 检索数组中的元素 |
|
| 308 | -- `[start:end:step]` 支持数组切片语法 |
|
| 309 | -- `*` 作为通配符,匹配所有成员 |
|
| 310 | -- `..` 子递归通配符,匹配成员的所有子元素 |
|
| 311 | -- `(<expr>)` 使用表达式 |
|
| 312 | -- `?(<boolean expr>)`进行数据筛选 |
|
| 313 | - |
|
| 314 | -下表将列举所有支持的语法,并对XPath进行比较: |
|
| 315 | - |
|
| 316 | -| XPath | JsonPath | 说明 | |
|
| 317 | -| ----- | ------------------ | ------------------------------------------------------------ | |
|
| 318 | -| `/` | `$` | 文档根元素 | |
|
| 319 | -| `.` | `@` | 当前元素 | |
|
| 320 | -| `/` | `.`或`[]` | 匹配下级元素 | |
|
| 321 | -| `..` | `N/A` | 匹配上级元素,JsonPath不支持此操作符 | |
|
| 322 | -| `//` | `..` | 递归匹配所有子元素 | |
|
| 323 | -| `*` | `*` | 通配符,匹配下级元素 | |
|
| 324 | -| `@` | `N/A` | 匹配属性,JsonPath不支持此操作符 | |
|
| 325 | -| `[]` | `[]` | 下标运算符,根据索引获取元素,**XPath索引从1开始,JsonPath索引从0开始** | |
|
| 326 | -| `|` | `[,]` | 连接操作符,将多个结果拼接成数组返回,可以使用索引或别名 | |
|
| 327 | -| `N/A` | `[start:end:step]` | 数据切片操作,XPath不支持 | |
|
| 328 | -| `[]` | `?()` | 过滤表达式 | |
|
| 329 | -| `N/A` | `()` | 脚本表达式,使用底层脚本引擎,XPath不支持 | |
|
| 330 | -| `()` | `N/A` | 分组,JsonPath不支持 | |
|
| 331 | - |
|
| 332 | -注意: |
|
| 333 | - |
|
| 334 | -- JsonPath的索引从0开始计数 |
|
| 335 | -- JsonPath中字符串使用单引号表示,例如:`$.store.book[?(@.category=='reference')]`中的`'reference'` |
|
| 336 | - |
|
| 337 | -## JsonPath示例 |
|
| 338 | - |
|
| 339 | -下面是相应的JsonPath的示例,代码来源于[https://goessner.net/articles/JsonPath/](https://links.jianshu.com/go?to=https%3A%2F%2Fgoessner.net%2Farticles%2FJsonPath%2F),JSON文档如下: |
|
| 340 | - |
|
| 341 | - |
|
| 342 | - |
|
| 343 | -```json |
|
| 344 | -{ |
|
| 345 | - "store": { |
|
| 346 | - "book": [{ |
|
| 347 | - "category": "reference", |
|
| 348 | - "author": "Nigel Rees", |
|
| 349 | - "title": "Sayings of the Century", |
|
| 350 | - "price": 8.95 |
|
| 351 | - }, { |
|
| 352 | - "category": "fiction", |
|
| 353 | - "author": "Evelyn Waugh", |
|
| 354 | - "title": "Sword of Honour", |
|
| 355 | - "price": 12.99 |
|
| 356 | - }, { |
|
| 357 | - "category": "fiction", |
|
| 358 | - "author": "Herman Melville", |
|
| 359 | - "title": "Moby Dick", |
|
| 360 | - "isbn": "0-553-21311-3", |
|
| 361 | - "price": 8.99 |
|
| 362 | - }, { |
|
| 363 | - "category": "fiction", |
|
| 364 | - "author": "J. R. R. Tolkien", |
|
| 365 | - "title": "The Lord of the Rings", |
|
| 366 | - "isbn": "0-395-19395-8", |
|
| 367 | - "price": 22.99 |
|
| 368 | - } |
|
| 369 | - ], |
|
| 370 | - "bicycle": { |
|
| 371 | - "color": "red", |
|
| 372 | - "price": 19.95 |
|
| 373 | - } |
|
| 374 | - } |
|
| 375 | -} |
|
| 376 | -``` |
|
| 377 | - |
|
| 378 | -接下来我们看一下如何对这个文档进行解析: |
|
| 379 | - |
|
| 380 | -| XPath | JsonPath | Result | |
|
| 381 | -| ---------------------- | ------------------------------------------ | ---------------------------------------- | |
|
| 382 | -| `/store/book/author` | `$.store.book[*].author` | 所有book的author节点 | |
|
| 383 | -| `//author` | `$..author` | 所有author节点 | |
|
| 384 | -| `/store/*` | `$.store.*` | store下的所有节点,book数组和bicycle节点 | |
|
| 385 | -| `/store//price` | `$.store..price` | store下的所有price节点 | |
|
| 386 | -| `//book[3]` | `$..book[2]` | 匹配第3个book节点 | |
|
| 387 | -| `//book[last()]` | `$..book[(@.length-1)]`,或 `$..book[-1:]` | 匹配倒数第1个book节点 | |
|
| 388 | -| `//book[position()<3]` | `$..book[0,1]`,或 `$..book[:2]` | 匹配前两个book节点 | |
|
| 389 | -| `//book[isbn]` | `$..book[?(@.isbn)]` | 过滤含isbn字段的节点 | |
|
| 390 | -| `//book[price<10]` | `$..book[?(@.price<10)]` | 过滤`price<10`的节点 | |
|
| 391 | -| `//*` | `$..*` | 递归匹配所有子节点 | |
|
| 392 | - |
|
| 393 | - |
|
| 394 | - |