Skip to content

Commit 0aa9df0

Browse files
author
Pablo Henrique
authored
Merge pull request vuematerial#749 from vuematerial/issue#742
fix on issue vuematerial#742 - v-model reflects changes on mdSelect
2 parents 0734305 + c6b687d commit 0aa9df0

File tree

3 files changed

+67
-12
lines changed

3 files changed

+67
-12
lines changed

docs/src/pages/components/Select.vue

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,24 @@
303303
<div class="multiple" slot="demo">
304304
<div class="field-group">
305305
<md-input-container>
306-
<label for="users">Users</label>
306+
<label for="users">Simple multiselect</label>
307+
<md-select name="option" id="option" multiple v-model="items">
308+
<md-option v-for="option in options"
309+
:key="option"
310+
:value="option">
311+
{{ option.name }}
312+
</md-option>
313+
</md-select>
314+
</md-input-container>
315+
</div>
316+
317+
<div>Selected letters: {{ items }}</div>
318+
<br/>
319+
<br/>
320+
321+
<div class="field-group">
322+
<md-input-container>
323+
<label for="users">Multiselect with subheaders</label>
307324
<md-select name="users" id="users" multiple v-model="users">
308325
<md-subheader>Managers</md-subheader>
309326
<md-option value="jim_halpert">Jim Halpert</md-option>
@@ -325,12 +342,27 @@
325342
</md-select>
326343
</md-input-container>
327344
</div>
328-
345+
329346
<div>Selected users: {{ users }}</div>
347+
330348
</div>
331349

332350
<div slot="code">
333351
<code-block lang="xml">
352+
&lt;md-input-container&gt;
353+
&lt;label for=&quot;users=&quot;&gt;Simple multiselect&lt;/label&gt;
354+
&lt;md-select name=&quot;option=&quot; id=&quot;option=&quot; multiple v-model=&quot;items=&quot;&gt;
355+
&lt;md-option v-for=&quot;option in options=&quot;
356+
:key=&quot;option=&quot;
357+
:value=&quot;option=&quot;&gt;
358+
{ { option.name } }
359+
&lt;/md-option&gt;
360+
&lt;md-select&gt;
361+
&lt;/md-input-container&gt;
362+
363+
&lt;div&gt;Selected letters: {{ items }}&lt;/div&gt;
364+
365+
334366
&lt;md-input-container&gt;
335367
&lt;label for=&quot;users&quot;&gt;Users&lt;/label&gt;
336368
&lt;md-select name=&quot;users&quot; id=&quot;users&quot; multiple v-model=&quot;users&quot;&gt;
@@ -361,10 +393,15 @@
361393
export default {
362394
data: () => ({
363395
food: '',
364-
users: [
365-
'jim_halpert',
366-
'michael_scott'
367-
]
396+
users: [],
397+
options: [
398+
{ id: 1, name: 'a' },
399+
{ id: 2, name: 'b' },
400+
{ id: 3, name: 'c' },
401+
{ id: 4, name: 'd' },
402+
{ id: 5, name: 'e' }
403+
],
404+
items: []
368405
})
369406
};
370407
</code-block>
@@ -400,10 +437,15 @@
400437
country: '',
401438
font: '',
402439
food: '',
403-
users: [
404-
'jim_halpert',
405-
'michael_scott'
406-
]
440+
users: [],
441+
options: [
442+
{ id: 1, name: 'a' },
443+
{ id: 2, name: 'b' },
444+
{ id: 3, name: 'c' },
445+
{ id: 4, name: 'd' },
446+
{ id: 5, name: 'e' }
447+
],
448+
items: []
407449
}),
408450
methods: {
409451
setPulpFiction() {

src/components/mdSelect/mdOption.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
2222
export default {
2323
props: {
24-
value: [String, Boolean, Number]
24+
value: [String, Boolean, Number, Object]
2525
},
2626
data: () => ({
2727
parentSelect: {},

src/components/mdSelect/mdSelect.vue

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
watch: {
6161
value(value) {
6262
this.setTextAndValue(value);
63+
this.selectOptions(value);
6364
},
6465
disabled() {
6566
this.setParentDisabled();
@@ -81,6 +82,16 @@
8182
setParentPlaceholder() {
8283
this.parentContainer.hasPlaceholder = !!this.placeholder;
8384
},
85+
selectOptions(modelValue) {
86+
const optionsArray = Object.keys(this.options).map((el) => this.options[el]);
87+
88+
if (optionsArray && optionsArray.length) {
89+
optionsArray.filter((el) => modelValue.indexOf(el.value) !== -1)
90+
.forEach((el) => {
91+
el.check = true;
92+
});
93+
}
94+
},
8495
getSingleValue(value) {
8596
let output = {};
8697
@@ -124,7 +135,9 @@
124135
return {};
125136
},
126137
setTextAndValue(modelValue) {
127-
const output = this.multiple ? this.getMultipleValue(modelValue) : this.getSingleValue(modelValue);
138+
const output = this.multiple ?
139+
this.getMultipleValue(modelValue) :
140+
this.getSingleValue(modelValue);
128141
129142
this.selectedValue = output.value;
130143
this.selectedText = output.text;

0 commit comments

Comments
 (0)