checkbox-size.vue 2.63 KB
<template>
    <Checkbox-group :value="values" @on-change="selectSize">
        <Checkbox v-for="size in sizes" key="size" :label="size.id" :disabled="size.disabled">
            <span>{{size.sizeName}}</span>
        </Checkbox>
    </Checkbox-group>
</template>

<script>
import _ from 'lodash';
import api from 'product-create/api';

export default {
    name: 'CheckboxSize',
    props: {
        sortId: {
            type: Number
        },
        selectSizes: {
            type: Array,
            default() {
                return [];
            }
        }
    },
    computed: {
        values() {
            return _.map(this.selectSizes, size => {
                return size.id;
            });
        }
    },
    created() {
        this.render();
    },
    data() {
        return {
            sizes: []
        };
    },
    methods: {
        render() {
            api.getSize(this.sortId).then((result) => {
                this.initSizes(result.data);
            });
        },
        initSizes(sizes) {
            this.sizes = sizes.map(size => {
                let selectColor = _.find(this.selectSizes, c => c.id === size.id);

                if (selectColor) {
                    size.disabled = selectColor.disabled;
                    size.selected = true;
                } else {
                    size.disabled = false;
                    size.selected = false;
                }
                return size;
            });
        },
        selectSize(values) {
            let newSize = this.sizes.filter(size => _.some(values, val => val === size.id));
//
//            let oldSize = this.sizes.filter(size => size.selected);
//
//            this.sizes.forEach(size => {
//                size.selected = false;
//            });
//
//            newSize.forEach(size => {
//                size.selected = true;
//            });
//
//            let addSize = _.differenceWith(newSize, oldSize, function(left, right) {
//                return left.id === right.id;
//            });
//
//            let removeSize = _.differenceWith(oldSize, newSize, function(left, right) {
//                return left.id === right.id;
//            });
            console.log(values);
//            console.log( 'newsize', newSize);
//            console.log( 'oldsize', oldSize);
//            console.log( 'addsize', addSize);
//            console.log( 'remove', removeSize);

            this.$emit('on-change', newSize);
//            this.$emit('on-add', addSize);
//            this.$emit('on-remove', removeSize);
        }
    },
    watch: {
        selectSizes() {
            this.initSizes(this.sizes);
        }
    }
};

</script>