Skip to content

Commit 740ed17

Browse files
Merge pull request #71 from what-crud/70_add-onChange-method-support-to-crud-fields
70 add on change method support to crud fields
2 parents b2967df + 77d1be7 commit 740ed17

File tree

6 files changed

+141
-5
lines changed

6 files changed

+141
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<h1 align="center">Vue CRUD</h1>
55
<div align="center">
66
<a><img alt="license" src="https://img.shields.io/badge/license-MIT-brightgreen.svg"></a>
7-
<a><img alt="version" src="https://img.shields.io/badge/version-v0.14.0-yellow.svg"></a>
7+
<a><img alt="version" src="https://img.shields.io/badge/version-v0.14.1-yellow.svg"></a>
88
<a><img alt="build" src="https://travis-ci.org/what-crud/vue-crud.svg?branch=master"></a>
99
<a><img alt="PRs" src="https://img.shields.io/badge/PRs-welcome-brightgreen.svg"></a>
1010
</div>

docs/guide/crud/field-options.md

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,113 @@ fieldsInfo () {
141141
::: tip INFO
142142
If `url` and `path.default` property has been set in **src/config/api.js** file, only the rest of path is required.
143143
:::
144+
145+
## `onChange`
146+
*Function*, optional. Function will be triggered after field value change in item details form. Function should have 2 arguments:
147+
- field value after change,
148+
- list with fields configuration
149+
150+
Example (slugify title od post):
151+
152+
```vue
153+
<template>
154+
<crud
155+
:prefix="prefix"
156+
:path="path"
157+
:paths="paths"
158+
:page-title="pageTitle"
159+
:fields-info="fieldsInfo"
160+
:details-title="$t('detailsTitle')"
161+
>
162+
</crud>
163+
</template>
164+
165+
<script>
166+
import Crud from '@/utils/crud/components/Crud.vue'
167+
168+
const slugify = (text) => {
169+
const a = 'ąàáäâćęèéëêìíïîłńòóöôśùúüûźżñçßÿœæŕśńṕẃǵǹḿǘẍźḧ·/_,:;'
170+
const b = 'aaaaaceeeeeiiiilnoooosuuuuzzncsyoarsnpwgnmuxzh------'
171+
const p = new RegExp(a.split('').join('|'), 'g')
172+
173+
return text.toString().toLowerCase()
174+
.replace(/\s+/g, '-')
175+
.replace(p, c => b.charAt(a.indexOf(c)))
176+
.replace(/&/g, '-and-')
177+
.replace(/[^\w-]+/g, '')
178+
.replace(/--+/g, '-')
179+
.replace(/^-+/, '')
180+
.replace(/-+$/, '')
181+
}
182+
183+
export default {
184+
data () {
185+
return {
186+
prefix: 'crud/blog',
187+
path: 'posts',
188+
paths: {
189+
i: 'blog/posts',
190+
st: 'blog/posts',
191+
u: 'blog/posts'
192+
},
193+
pageTitle: 'blog.posts'
194+
}
195+
},
196+
computed: {
197+
fieldsInfo () {
198+
return [
199+
{
200+
text: this.$t('fields.id'),
201+
name: 'id',
202+
details: false
203+
},
204+
{
205+
type: 'input',
206+
column: 'title',
207+
text: this.$t('fields.title'),
208+
name: 'title',
209+
multiedit: false,
210+
onChange: (value, fields) => {
211+
fields.find(field => field.name === 'slug').value = slugify(value)
212+
}
213+
},
214+
{
215+
type: 'input',
216+
column: 'slug',
217+
text: this.$t('fields.slug'),
218+
name: 'slug',
219+
multiedit: false,
220+
required: false,
221+
table: false
222+
}
223+
]
224+
}
225+
},
226+
components: {
227+
Crud
228+
},
229+
i18n: {
230+
messages: {
231+
pl: {
232+
detailsTitle: 'Post',
233+
fields: {
234+
id: 'Id',
235+
title: 'Tytuł',
236+
slug: 'Slug'
237+
}
238+
},
239+
en: {
240+
detailsTitle: 'Post',
241+
fields: {
242+
id: 'Id',
243+
title: 'Title',
244+
slug: 'Slug'
245+
}
246+
}
247+
}
248+
}
249+
}
250+
251+
</script>
252+
253+
```

examples/cms/routes/app/routes/blog/routes/posts/Index.vue

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,21 @@
1414
<script>
1515
import Crud from '@/utils/crud/components/Crud.vue'
1616
17+
const slugify = (text) => {
18+
const a = 'ąàáäâćęèéëêìíïîłńòóöôśùúüûźżñçßÿœæŕśńṕẃǵǹḿǘẍźḧ·/_,:;'
19+
const b = 'aaaaaceeeeeiiiilnoooosuuuuzzncsyoarsnpwgnmuxzh------'
20+
const p = new RegExp(a.split('').join('|'), 'g')
21+
22+
return text.toString().toLowerCase()
23+
.replace(/\s+/g, '-')
24+
.replace(p, c => b.charAt(a.indexOf(c)))
25+
.replace(/&/g, '-and-')
26+
.replace(/[^\w-]+/g, '')
27+
.replace(/--+/g, '-')
28+
.replace(/^-+/, '')
29+
.replace(/-+$/, '')
30+
}
31+
1732
export default {
1833
data () {
1934
return {
@@ -62,7 +77,10 @@ export default {
6277
column: 'title',
6378
text: this.$t('fields.title'),
6479
name: 'title',
65-
multiedit: false
80+
multiedit: false,
81+
onChange: (value, fields) => {
82+
fields.find(field => field.name === 'slug').value = slugify(value)
83+
}
6684
},
6785
{
6886
type: 'input',

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-crud",
3-
"version": "0.14.0",
3+
"version": "0.14.1",
44
"description": "Vue CRUD",
55
"author": "Szczepan Masny <[email protected]>",
66
"license": "MIT",

src/utils/crud/components/ChildDetails.vue

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,11 @@ export default {
156156
this.$set(this, 'fields', result)
157157
},
158158
valueChanged (val, fieldColumn) {
159-
this.$set(this.fields[this.fields.findIndex(el => el.column === fieldColumn)], 'value', val)
159+
const field = this.fields[this.fields.findIndex(el => el.column === fieldColumn)]
160+
this.$set(field, 'value', val)
161+
if (field.onChange) {
162+
field.onChange(val, this.fields)
163+
}
160164
},
161165
fieldRules (field) {
162166
const rules = []

src/utils/crud/components/ItemDetails.vue

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,11 @@ export default {
171171
return refField ? refField.value : undefined
172172
},
173173
valueChanged (val, fieldColumn) {
174-
this.$set(this.fields[this.fields.findIndex(el => el.column === fieldColumn)], 'value', val)
174+
const field = this.fields[this.fields.findIndex(el => el.column === fieldColumn)]
175+
this.$set(field, 'value', val)
176+
if (field.onChange) {
177+
field.onChange(val, this.fields)
178+
}
175179
},
176180
close () {
177181
this.details.show = false

0 commit comments

Comments
 (0)