【问题标题】:Vuejs select won't select right optionsVuejs select不会选择正确的选项
【发布时间】:2020-07-14 15:23:05
【问题描述】:

当我在编辑页面时,我希望默认选择我保存的产品标签。

目前它只选择其中的 1 个并重复我有多少标签(示例)

代码

HTML

<el-col :span="22">
    <el-select
        v-model="form.tags"
        multiple
        filterable
        style="width:100%;"
        allow-create
        default-first-option
        placeholder="Choose tags for your product">
        <el-option
            v-for="item in tagss"
            :key="item.id"
            :label="item.name"
            :value="item.name">
        </el-option>
    </el-select>
</el-col>

<el-form-item label="Categories">
    <el-col :span="24">
        <el-cascader
            v-model="form.categories"
            style="width: 100%;"
            :options="cats"
            :props="{
                multiple: true,
                checkStrictly: true,
                value: 'id',
                label: 'name',
            }"
            clearable
            filterable>
        </el-cascader>
    </el-col>
</el-form-item>

Script

data() {
    return {
        cats: [],
        tagss: [],
        form: {
            tags: [],
            categories: [],
            _method: 'PUT',
        },
    }
},
mounted () {
    this.fetchProduct()
    this.getData()
},
methods: {
    getData () {
        axios.get('/api/admin/products', {
            headers: {
            Authorization: 'Bearer ' + localStorage.getItem('access_token')
            }
        })
        .then(response => {
            this.brands = response.data.brands;
            this.cats = response.data.categories;
            this.tagss = response.data.tags;
        })
        .catch(function (error) {
            console.log('error', error);
        });
    },
    fetchProduct() {
        axios
            .get('/api/admin/products/'+this.$route.params.id, {
                headers: {
                    Authorization: 'Bearer ' + localStorage.getItem('access_token')
                }
            })
            .then(response => {
                this.form.tags = response.data.data.tags
                this.form.categories = response.data.data.categories
                console.log('all tags from backend: ', this.tagss) // sample data below
                console.log('this product tags from backend: ', response.data.data.tags) // sample data below
            })
            .catch(function (error) {
                console.log('error', error);
            });
    }
}

这是上面代码中我的console.log 的示例结果

注意:我在这个问题中提到了我的标签,但也分享了我的类别代码,因为你可以看到我的类别有同样的问题。

知道可能是什么问题吗?

【问题讨论】:

  • 我这辈子从来没有用过这个组件,但是我很好奇,我去查看他们的文档。我没有找到任何与您一样使用的组件示例,而是将该选项作为道具传递:vue-select.org/guide/values.html#tagging
  • @Raffobaffo docs
  • 是的,这里它使用了不同的组件el-cascader你正在使用el-select
  • 糟糕的是我正在做别的事情:D
  • 大声笑,发生了!很好,我在你发给我链接后注意到了!

标签: typescript vue.js vuejs2


【解决方案1】:

已解决

Final code

this.form.tags = response.data.data.tags.map(row => row.name);
this.form.categories = response.data.data.categories.map(row => row.id);

Result

【讨论】:

    猜你喜欢
    • 2021-07-17
    • 1970-01-01
    • 2015-09-09
    • 1970-01-01
    • 2011-04-02
    • 2015-09-02
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    相关资源
    最近更新 更多