checkbox.uvue 3.9 KB
Newer Older
Y
init  
yurj26 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
<template>
    <view>
        <page-head :title="title"></page-head>
        <view class="uni-padding-wrap uni-common-mt">
            <view class="uni-title uni-common-mt">
                <text class="uni-title-text">
                    默认样式
                </text>
            </view>
            <view>
                <checkbox-group class="uni-flex uni-row">
                    <checkbox value="cb1" :checked="true" style="margin-right: 30rpx;">选中</checkbox>
                    <checkbox value="cb">未选中</checkbox>
                </checkbox-group>
            </view>
            <view class="uni-title uni-common-mt">
                <text class="uni-title-text">
                    不同颜色和尺寸的checkbox
                </text>
            </view>
            <view>
                <checkbox-group class="uni-flex uni-row">
                    <checkbox value="cb1" :checked="true" color="#FFCC33"
                        style="transform:scale(0.7); margin-right: 30rpx;">选中
                    </checkbox>
                    <checkbox value="cb" color="#FFCC33" style="transform:scale(0.7)">未选中</checkbox>
                </checkbox-group>
            </view>
        </view>

        <view class="uni-padding-wrap">
            <view class="uni-title uni-common-mt">
                <text class="uni-title-text">
                    推荐展示样式
                </text>
                <text class="uni-subtitle-text">
                    使用 uni-list 布局
                </text>
            </view>
        </view>
        <view class="uni-list">
            <checkbox-group @change="checkboxChange">
Y
yurj26 已提交
43 44
                <view class="uni-list-cell uni-list-cell-pd" v-for="(item, index) in items" :key="item.value"
                    :class="index < items.length - 1 ? 'uni-list-cell-line': ''">
Y
init  
yurj26 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
                    <view>
                        <checkbox :value="item.value" :checked="item.checked">{{item.name}}</checkbox>
                    </view>
                </view>
            </checkbox-group>
        </view>
    </view>
</template>
<script lang="ts">
    type ItemType = {
        value : string,
        name : string,
        checked : boolean
    }
    export default {
        data() {
            return {
                title: 'checkbox 复选框',
                items: [{
                    value: 'USA',
                    name: '美国',
                    checked: false
                },
                {
                    value: 'CHN',
                    name: '中国',
                    checked: true
                },
                {
                    value: 'BRA',
                    name: '巴西',
                    checked: false
                },
                {
                    value: 'JPN',
                    name: '日本',
                    checked: false
                },
                {
                    value: 'ENG',
                    name: '英国',
                    checked: false
                },
                {
                    value: 'FRA',
                    name: '法国',
                    checked: false
                }
                ] as ItemType[]
            }
        },
        methods: {
Y
yurj26 已提交
97
            checkboxChange: function (value : string[]) {
Y
init  
yurj26 已提交
98 99
                // TODO 后续更改参数
                var items = this.items,
Y
yurj26 已提交
100
                    values = value;
Y
init  
yurj26 已提交
101
                for (var i = 0, lenI = items.length; i < lenI; ++i) {
Y
yurj26 已提交
102 103
                    const item = items[i]
                    if (values.indexOf(item.value) >= 0) {
Y
init  
yurj26 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
                        item.checked = true
                    } else {
                        item.checked = false
                    }
                }
            }
        }
    }
</script>

<style>
    .uni-list-cell {
        justify-content: flex-start
    }
</style>