1
1
<template lang="jade">
2
- .row
3
- .col-md-6
4
- vue-form-generator(:schema='schema', :model='model', :options='formOptions')
5
- .col-md-6
6
- pre(v-if='model') {{{ model | prettyJSON }}}
2
+ .row
3
+ .col-md-10.col-md-offset-1
4
+ data-table(:rows="rows", :selected="selected", :select="selectRow")
5
+
6
+ .row(v-if="model")
7
+ .col-md-6
8
+ .buttons.text-center
9
+ button.btn.btn-default.new(@click="newModel") New
10
+ button.btn.btn-primary.save(@click="saveModel") Save
11
+ button.btn.btn-danger.delete(@click="deleteModel") Delete
12
+
13
+ vue-form-generator(:schema='schema', :model='model', :options='formOptions', :multiple="selected.length > 1", v-ref:form, :is-new-model="isNewModel")
14
+
15
+
16
+ .col-md-6
17
+ pre(v-if='model') {{{ model | prettyJSON }}}
7
18
8
19
</template >
9
20
10
21
<script >
11
22
import Vue from " vue" ;
12
23
import VueFormGenerator from " ../src" ;
24
+ import DataTable from " ./dataTable.vue" ;
25
+
13
26
import Schema from " ./schema" ;
27
+ import { users } from " ./data" ;
28
+ import { filters } from " ./utils" ;
29
+
30
+ import {each , isFunction , cloneDeep , merge } from ' lodash' ;
14
31
15
32
export default {
16
33
components: {
17
- " VueFormGenerator" : VueFormGenerator .component
34
+ " VueFormGenerator" : VueFormGenerator .component ,
35
+ DataTable
18
36
},
19
37
20
- filters: {
21
- prettyJSON : function (json ) {
22
- if (json) {
23
- json = JSON .stringify (json, undefined , 4 );
24
- json = json .replace (/ &/ g , ' &' ).replace (/ </ g , ' <' ).replace (/ >/ g , ' >' );
25
- return json .replace (/ ("(\\ u[a-zA-Z0-9 ] {4} | \\ [^ u] | [^ \\ "] )* "(\s * :)? | \b (true| false| null)\b | -? \d + (?:\. \d * )? (?:[eE][+\- ] ? \d + )? )/ g , function (match ) {
26
- var cls = ' number' ;
27
- if (/ ^ "/ .test (match)) {
28
- if (/ :$ / .test (match)) {
29
- cls = ' key' ;
30
- } else {
31
- cls = ' string' ;
32
- }
33
- } else if (/ true| false/ .test (match)) {
34
- cls = ' boolean' ;
35
- } else if (/ null/ .test (match)) {
36
- cls = ' null' ;
37
- }
38
- return ' <span class="' + cls + ' ">' + match + ' </span>' ;
39
- });
40
- }
41
- }
42
- },
38
+ filters: filters,
43
39
44
40
data () {
45
41
return {
46
- model: {
47
- id: 1 ,
48
- name: " John Doe" ,
49
- type: " personal" ,
50
- password: " J0hnD03!x4" ,
51
- skills: [
52
- " Javascript" ,
53
- " VueJS"
54
- ],
55
-
56
- language: " en-GB" ,
57
- age: 35 ,
58
- dob: 348966000000 ,
59
- rank: 6 ,
60
- address: {
61
- country: " United Kingdom" ,
62
- city: " London" ,
63
- street: " SW1A 5 Parliament St" ,
64
- geo: {
65
- lat: 51.501015 ,
66
- lng: - 0.126005
67
- }
68
- },
69
- role: " admin" ,
70
- created: 1461834815864 ,
71
- bio: " Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam non lacus porttitor, pellentesque odio sit amet, hendrerit felis. In turpis mauris, viverra a lacinia nec, fringilla ut nisi. Curabitur rutrum mattis risus, at dapibus nisl tempus et. Interdum et malesuada fames ac ante ipsum primis in faucibus. Phasellus eget elementum lorem. Pellentesque tempor nec ante ut molestie. Suspendisse imperdiet tempus hendrerit. Morbi a dignissim augue." ,
72
- status: true ,
73
-
74
- },
42
+ isNewModel: false ,
43
+
44
+ selected: [],
45
+
46
+ model: null ,
47
+
48
+ rows: users,
75
49
76
50
schema: Schema,
77
51
78
52
formOptions: {
79
53
validateAfterLoad: true ,
80
- validateAfterChanged: true
54
+ validateAfterChanged: true ,
55
+ validateBeforeSave: true
56
+ }
57
+ }
58
+ },
59
+
60
+ methods: {
61
+ selectRow (event , row , add ) {
62
+ if ( (add || (event && event .ctrlKey ))) {
63
+ if (this .selected .indexOf (row) != - 1 )
64
+ this .selected .$remove (row);
65
+ else
66
+ this .selected .push (row);
67
+ } else {
68
+ this .clearSelection ();
69
+ this .selected .push (row);
70
+ }
71
+ this .generateModel ();
72
+ },
73
+
74
+ clearSelection () {
75
+ this .selected .splice (0 );
76
+ this .generateModel ();
77
+ },
78
+
79
+ generateModel () {
80
+ if (this .selected .length == 1 ) {
81
+ this .model = cloneDeep (this .selected [0 ]);
81
82
}
83
+ else if (this .selected .length > 1 ) {
84
+ this .model = VueFormGenerator .schema .mergeMultiObjectFields (Schema, this .selected );
85
+ }
86
+ else
87
+ this .model = null ;
88
+ },
89
+
90
+ newModel () {
91
+ console .log (" Create new model..." );
92
+ this .selected .splice (0 );
93
+ let newRow = VueFormGenerator .schema .createDefaultObject (Schema, { id: this .getNextID () })
94
+ this .isNewModel = true ;
95
+ this .model = newRow;
96
+
97
+ let el = document .querySelector (" div.form input:nth-child(1):not([readonly]):not(:disabled)" );
98
+ if (el)
99
+ el .focus ()
100
+
101
+ },
102
+
103
+ saveModel () {
104
+ console .log (" Save model..." );
105
+ if (this .formOptions .validateBeforeSave === false || this .validate ()) {
106
+ this .mergeModelValues ();
107
+
108
+ if (this .isNewModel ) {
109
+ this .rows .push (this .model );
110
+ this .selectRow (null , this .model , false );
111
+ }
112
+
113
+ } else {
114
+ // Validation error
115
+ }
116
+ },
117
+
118
+ mergeModelValues () {
119
+ let model = this .model ;
120
+ if (model && this .selected .length > 0 ) {
121
+ each (this .selected , (row ) => {
122
+ merge (row, model);
123
+ });
124
+ }
125
+ },
126
+
127
+ deleteModel () {
128
+ if (this .selected .length > 0 ) {
129
+ each (this .selected , (row ) => {
130
+ this .rows .$remove (row);
131
+ })
132
+ this .clearSelection ();
133
+ }
134
+ },
135
+
136
+ getNextID () {
137
+ let id = 0 ;
138
+
139
+ each (this .rows , (row ) => {
140
+ if (row .id > id)
141
+ id = row .id ;
142
+ });
143
+
144
+ return ++ id;
145
+ },
146
+
147
+ validate () {
148
+ return this .$refs .form .validate ();
82
149
}
150
+
151
+
83
152
},
84
153
85
154
ready () {
95
164
96
165
html {
97
166
font-family : " Open Sans" ;
98
- font-size : 14px ;
167
+ font-size : 14px ;
99
168
}
100
169
101
170
* {
104
173
box-sizing : border-box ;
105
174
}
106
175
107
- pre {
108
- overflow : auto ;
109
-
110
- .string { color: #885800; }
111
- .number { color: blue; }
112
- .boolean { color: magenta; }
113
- .null { color: red; }
114
- .key { color: green; }
115
- }
176
+ pre {
177
+ overflow : auto ;
178
+
179
+ .string { color: #885800; }
180
+ .number { color: blue; }
181
+ .boolean { color: magenta; }
182
+ .null { color: red; }
183
+ .key { color: green; }
184
+ }
116
185
117
186
</style >
0 commit comments