vue js多项动态选择人口问题

时间:2018-08-16 20:47:53

标签: javascript select dynamic vue.js multi-select

你好,vue js大师的

当前,我正在创建一些具有“高级”设置的向导,并且需要一些帮助来获得正确的结果。我尝试了几种不同的方法,但都没有成功。

我要执行的操作如下:

我正在构建一个锻炼创建向导,可以在其中添加部分,并且可以在每个部分内定义一个或多个运动,其中每个运动本身都应进行某种测量,最后一个应根据所选运动而改变,并且可以有一个或多个选项可供选择。

数据由axios(远程)获取,并保存到数据数组中,以进行类似这样的可用移动:

[
    '' => [
        1 => [
            'name' => 'pull ups',
            'measure' => [
                0 => 'none',
                1 => 'distance',
            ],
        ],
        2 => [
            'name' => 'push ups',
            'measure' => [
                0 => 'none',
                1 => 'weight',
            ],
        ],
        ...
    ],
    ...
]

然后将其存储在我的vue js实例的this.movements = data.movements;数据数组中。

这是我的vue js代码:

<script>
new Vue({
    el: '#workout-wrapper',
    data() {
        return {
            dynamicOptions: [],

            name: null,
            sections: [{
                name: null,
                rounds: null,
                minutes: null,
                measure: null,
                movements: [{
                    reps: null,
                    movement: null,
                    measure: null,
                    remarks: null
                }]
            }],
            movements: [],
            ...
        }
    },
    methods: {
        ...
        onMovementChange(group, movement) {
            // TODO: this one combined with the computed options still isn't working correctly

            if (group == '') {
                this.options = { section: 0, movement: 0, options: this.movements[""][movement].measure };
            } else {
                this.options = { section: 0, movement: 0, options: this.movements[group][movement].measure };
            }
        },
        ...
    },
    computed: {
        options: {
            get(event, data) {
                // TODO: now we should only return the options for section and movement indexes

                return this.dynamicOptions;
            },
            set(data) {
                this.dynamicOptions[data.section] = [];
                this.dynamicOptions[data.section][data.movement] = data.options;
                // console.log(this.dynamicOptions);
            }
        }
    },
    created() {
        axios.get('/workouts/create/data').then(response => {
            let data = response.data;
            this.movements = data.movements;
            ...
        }).catch(error => {
            console.error(error);
        });
    }
});
</script>

这里是模板:

<div class="row m-t-20" v-for="(section, index) in sections">
<div class="col-md-12">
    <div class="card card-default no-border">
        <div class="card-header separator">
            <div class="row">
                <div class="col-md-12">
                    <div class="form-group">
                        <label>Section name</label>
                        <div class="input-group m-b-15">
                            <input type="text" name="sections[0][name]" placeholder="e.g. Warming-Up" class="form-control" v-model="section.name">
                            <div class="input-group-btn">
                                <button type="button" class="btn btn-danger" data-title="Remove" data-tooltip @click="removeSection(index)">
                                    <i class="fa fa-times"></i>
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-md-4">
                    <div class="form-group">
                        <label>Rounds</label>
                        <input type="number" name="sections[0][rounds]" class="form-control" placeholder="Optional" min="0" v-model="section.rounds">
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="form-group">
                        <label for="">
                            Minutes
                        </label>
                        <input type="number" name="sections[0][minutes]" class="form-control" placeholder="Optional" min="0" v-model="section.minutes">
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="form-group">
                        <label>Measure</label>
                        <select name="sections[0][measure]" class="form-control" v-model="section.measure"> {{-- data-init-plugin="select2" --}}
                            <option :value="key" v-for="(measure, key) in measurements">@{{ measure }}</option>
                        </select>
                    </div>
                </div>
            </div>
        </div>
        <div class="card-block m-t-25">
            <table class="table table-striped">
                <thead>
                <tr>
                    <th width="10%">
                        Reps
                    </th>
                    <th width="35%">
                        Movement
                    </th>
                    <th width="20%">
                        Measure
                    </th>
                    <th width="35%">
                        Remarks
                    </th>
                    <th></th>
                </tr>
                </thead>
                <tbody>
                <tr v-for="(movement, movementIndex) in section.movements">
                    <td>
                        <input type="number" name="sections[0][movements][0][reps]" class="form-control" min="0" v-model="movement.reps">
                    </td>
                    <td>
                        <select name="sections[0][movements][0][movement]" class="form-control" v-model="movement.movement" @change="onMovementChange('', movement.movement)">
                            <optgroup :label="group" v-for="(options, group) in movements">
                                <option :value="key" v-for="(option, key) in options">@{{ option.name }}</option>
                            </optgroup>
                        </select>
                    </td>
                    <td>
                        <select name="sections[0][movements][0][measure]" class="form-control" v-model="movement.measure" :disabled="!movement.movement">
                            <option :value="key" v-for="(measure, key) in options">@{{ measure }}</option>
                        </select>
                    </td>
                    <td>
                        <textarea name="sections[0][movements][0][remark]" rows="1" class="form-control" v-model="movement.remarks"></textarea>
                    </td>
                    <td>
                        <button type="button" class="btn btn-link text-danger" data-title="Remove" data-tooltip @click="removeMovement(index, movementIndex)">
                            <i class="fa fa-trash"></i>
                        </button>
                    </td>
                </tr>
                <tr>
                    <td colspan="5" class="text-center">
                        <button type="button" class="btn btn-cons btn-complete" data-title="Add movement" data-tooltip @click="addMovement(index)">
                            <i class="fa fa-plus"></i>
                        </button>
                    </td>
                </tr>
                </tbody>
            </table>
        </div>
    </div>
</div>
</div>

使用此当前设置,它会在第二个选择框中添加数据,但是它总是在变化,因此,在添加第二个移动行时,两个选择框都会根据第二行中的所选移动来更改选项,等等。它应该仅根据当前行和偏离航向的部分进行更改。

也许有些人可以帮助我解决这个问题...如果需要更多信息,请告诉我:-)

0 个答案:

没有答案