|
14 | 14 | <input type="text" v-model="filters[column].model"/> |
15 | 15 | </div> |
16 | 16 | </th> |
| 17 | + <th v-if="delete"> |
17 | 18 | </thead> |
18 | 19 | <thead class="regular-header" :class="{ transparent: scrolledPast }"> |
19 | 20 | <tr> |
|
27 | 28 | <input type="text" v-model="filters[column].model"/> |
28 | 29 | </div> |
29 | 30 | </th> |
| 31 | + <th v-if="delete"> |
| 32 | + </th> |
30 | 33 | </tr> |
31 | 34 | </thead> |
32 | 35 | <tfoot> |
|
41 | 44 | <td v-for="footerCell in footerRow" track-by="$index"> |
42 | 45 | {{footerCell}} |
43 | 46 | </td> |
| 47 | + <td v-if="delete"></td> |
44 | 48 | </tr> |
45 | 49 | <tr v-if="actionsArePresent" class="action-row"> |
46 | 50 | <td class="smart-control-bar" colspan="{{span}}"> |
|
70 | 74 | </slot> |
71 | 75 | </div> |
72 | 76 | </td> |
| 77 | + <td v-if="delete"> |
| 78 | + <button id="delete-{{key}}" @click="remove(key)">Delete</button> |
| 79 | + </td> |
73 | 80 | </tr> |
74 | 81 | <tr v-if="addRow" class="row-new"> |
75 | 82 | <td v-if="actionsArePresent"><!-- to match the toggle checkboxes --></td> |
|
82 | 89 | <option v-for="(value, label) in inputList[col]" value="{{value}}" class="input-label">{{label}}</option> |
83 | 90 | </select> |
84 | 91 | </td> |
| 92 | + <td v-if="delete"></td> |
85 | 93 | </tr> |
86 | 94 | </tbody> |
87 | 95 | </table> |
|
121 | 129 | required: false, |
122 | 130 | default: '_id' |
123 | 131 | }, |
| 132 | + 'delete': { |
| 133 | + type: Boolean, |
| 134 | + default: false |
| 135 | + }, |
124 | 136 | footer: { |
125 | 137 | required: false |
126 | 138 | }, |
|
141 | 153 | actions: [Object, Array], |
142 | 154 | endpoint: { |
143 | 155 | type: String, |
144 | | - default: 'http://api.randomuser.me/?results=2&seed=abc' |
| 156 | + default: 'http://localhost:8080' |
145 | 157 | }, |
146 | 158 | labelCol: { |
147 | 159 | type: String, |
|
218 | 230 | return false |
219 | 231 | }, |
220 | 232 | tableHeader () { |
| 233 | + // WARNING: must not (and cannot) depend on processedSmartBody! |
| 234 | +
|
221 | 235 | if (this.body === undefined) { |
222 | 236 | this.body = [] |
223 | 237 | } |
224 | | - // must not depend on processedSmartBody |
225 | 238 | // if object return object |
226 | 239 | if (this.header !== undefined && !Array.isArray(this.header)) { |
227 | 240 | return this.header |
|
248 | 261 | }, |
249 | 262 | mainCol () { |
250 | 263 | // choose an appropriate default label column |
251 | | - var bodyKeys = Object.keys(this.processedSmartBody) |
252 | | - const firstEntry = this.processedSmartBody[bodyKeys[0]] |
253 | | - const firstEntryKeys = Object.keys(firstEntry) |
254 | | - if (firstEntryKeys.indexOf(this.labelCol) === -1) { |
255 | | - return firstEntryKeys[0] |
| 264 | + if (Object.keys(this.tableHeader).indexOf(this.labelCol) === -1) { |
| 265 | + return Object.keys(this.tableHeader)[0] |
256 | 266 | } else { |
257 | 267 | return this.labelCol |
258 | 268 | } |
|
285 | 295 | // filter unwanted columns |
286 | 296 | let finalRow = {} |
287 | 297 | Object.keys(this.tableHeader).forEach(col => { |
288 | | - let realColValue = col.split('.').reduce((o,i)=>o[i], row) |
| 298 | + let realColValue = {} |
| 299 | + if (/\+/.test(col)) { |
| 300 | + // it's a composite column will return an object |
| 301 | + col.split('+').forEach(d=>{ |
| 302 | + realColValue[d] = d.split('.').reduce((o,i)=>o[i], row) |
| 303 | + }) |
| 304 | + } else { |
| 305 | + realColValue = col.split('.').reduce((o,i)=>o[i], row) |
| 306 | + } |
289 | 307 | finalRow[col] = realColValue |
290 | 308 | }) |
291 | 309 | let idValue = this.idCol.split('.').reduce((o,i)=>o[i], row) |
|
310 | 328 | return smartBody |
311 | 329 | }, |
312 | 330 | span () { |
313 | | - return Object.keys(this.tableHeader).length + 1 |
| 331 | + return Object.keys(this.tableHeader).length + 1 + (this.delete ? 1 : 0) |
314 | 332 | } |
315 | 333 | }, |
316 | 334 | beforeCompile () { |
|
369 | 387 | saveNewRow () { |
370 | 388 | if (this.canSaveNewRow) { |
371 | 389 | this.$dispatch('before-request') |
372 | | - this.$http.get(this.endpoint, {action: 'addRow', resource: this.newRow}).then((response) => { |
| 390 | + this.$http.post(this.endpoint, {action: 'addRow', resource: this.newRow}).then((response) => { |
373 | 391 | this.$set('error', false) |
374 | 392 | this.$set('body', response.data.body) |
375 | 393 | this.$dispatch('successful-request') |
|
442 | 460 | toggleAllRows () { |
443 | 461 | if (this.toggleAll === false) { |
444 | 462 | this.toggleAll = true |
445 | | - this.selection = Object.keys(this.body) |
| 463 | + this.selection = Object.keys(this.processedSmartBody) |
446 | 464 | } else { |
447 | 465 | this.toggleAll = false |
448 | 466 | this.selection = [] |
|
463 | 481 | }, |
464 | 482 | doCommand (command) { |
465 | 483 | this.$dispatch('before-request') |
466 | | - this.$http.get(this.endpoint, command).then((response) => { |
467 | | - if (response.data.body !== undefined || response.data.body === {}) { |
468 | | - this.$set('body', response.data.body) |
| 484 | +
|
| 485 | + // special case |
| 486 | + if (/^(_remove|_delete)$/i.test(command.action)) { |
| 487 | + this.$http.delete(this.endpoint, command).then(onSuccess, onFailure) |
| 488 | + } else { |
| 489 | + this.$http.get(this.endpoint, command).then(onSuccess, onFailure) |
| 490 | + } |
| 491 | +
|
| 492 | + function onSuccess(response) { |
| 493 | + if (response.data[this.bodyField] !== undefined || response.data[this.bodyField] === {}) { |
| 494 | + this.$set('body', response.data[this.bodyField]) |
469 | 495 | this.$set('footer', response.data.footer) |
470 | 496 | } |
471 | 497 | this.$dispatch('successful-request') |
472 | 498 | this.$dispatch('after-request') |
473 | 499 | this.$set('error', false) |
474 | | - }, (response) => { |
| 500 | + } |
| 501 | +
|
| 502 | + function onFailure(response) { |
475 | 503 | this.$set('error', { status: response.status, data: response.data.error }) |
476 | 504 | this.$dispatch('failed-request') |
477 | 505 | this.$dispatch('after-request') |
478 | | - }) |
| 506 | + } |
| 507 | + }, |
| 508 | + remove (id) { |
| 509 | + this.$dispatch('before-request') |
| 510 | +
|
| 511 | + this.$http.delete(this.endpoint + '/' + id).then(onSuccess, onFailure) |
| 512 | +
|
| 513 | + function onSuccess(response) { |
| 514 | + if (response.data[this.bodyField] !== undefined || response.data[this.bodyField] === {}) { |
| 515 | + this.$set('body', response.data[this.bodyField]) |
| 516 | + this.$set('footer', response.data.footer) |
| 517 | + } |
| 518 | + this.$dispatch('successful-request') |
| 519 | + this.$dispatch('after-request') |
| 520 | + this.$set('error', false) |
| 521 | + } |
| 522 | +
|
| 523 | + function onFailure(response) { |
| 524 | + this.$set('error', { status: response.status, data: response.data.error }) |
| 525 | + this.$dispatch('failed-request') |
| 526 | + this.$dispatch('after-request') |
| 527 | + } |
479 | 528 | }, |
480 | 529 | isEditable (col) { |
481 | 530 | if (this.editable === false) { |
|
499 | 548 | this.modalEdit = { |
500 | 549 | id: id, |
501 | 550 | col: col, |
502 | | - currentValue: this.body[id][col], |
503 | | - previousValue: this.body[id][col], |
| 551 | + currentValue: this.processedSmartBody[id][col], |
| 552 | + previousValue: this.processedSmartBody[id][col], |
504 | 553 | type: this.editType(col) |
505 | 554 | } |
506 | 555 | this.$broadcast('modalEdit', this.modalEdit) |
|
0 commit comments