action-sheet.uvue 4.6 KB
Newer Older
Y
yurj26 已提交
1
<template>
W
微调  
wanganxp 已提交
2 3 4 5 6 7 8 9 10
  <view>
    <page-head :title="title"></page-head>
    <view class="uni-list">
      <radio-group @change="radioChange">
        <radio 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': ''" :value="item.value" :checked="index === current">
          {{item.name}}
        </radio>
      </radio-group>
Y
yurj26 已提交
11
    </view>
W
微调  
wanganxp 已提交
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
    <view class="uni-list">
      <view class="uni-list-cell uni-list-cell-pd">
        <view class="uni-list-cell-db">自定义itemColor</view>
        <switch :checked="itemColorCustom" @change="itemColorChange" />
      </view>
      <view class="uni-list-cell uni-list-cell-pd">
        <view class="uni-list-cell-db">超长文本和空文本item</view>
        <switch :checked="itemContentLarge" @change="itemContentLargeChange" />
      </view>
      <view class="uni-list-cell uni-list-cell-pd">
        <view class="uni-list-cell-db">超过6个item</view>
        <switch :checked="itemNumLargeSelect" @change="itemNumLargeChange" />
      </view>
    </view>
    <view class="uni-padding-wrap">
      <view class="uni-btn-v">
        <button class="uni-btn-v" type="default" @tap="actionSheetTap">弹出action sheet</button>
      </view>
    </view>
  </view>
Y
yurj26 已提交
32
</template>
Y
yurj26 已提交
33
<script lang="uts">
W
微调  
wanganxp 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47
  type ItemType = {
    value : string,
    name : string,
  }
  export default {
    data() {
      return {
        title: 'action-sheet',
        itemColorCustom: false,
        itemContentLarge: false,
        itemNumLargeSelect: false,
        items: [{
          value: '标题',
          name: '有标题'
Y
yurj26 已提交
48
        },
W
微调  
wanganxp 已提交
49 50 51
        {
          value: '',
          name: '无标题'
Y
yurj26 已提交
52
        },
W
微调  
wanganxp 已提交
53 54 55 56 57 58 59
        {
          value: '超长标题测试内容,测试超过显示最大范围之后的样式-超长标题测试内容,测试超过显示最大范围之后的样式',
          name: '超长标题'
        }
        ] as ItemType[],
        current: 0,
      }
杜庆泉's avatar
杜庆泉 已提交
60 61 62 63 64 65
    },
    onLoad(){
      uni.showActionSheet({
        title: "onLoad 调用示例,请手动取消",
        itemList:['item1', 'item2'],
      })
W
微调  
wanganxp 已提交
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
    },
    methods: {
      radioChange(e : RadioGroupChangeEvent) {
        for (let i = 0; i < this.items.length; i++) {
          if (this.items[i].value === e.detail.value) {
            this.current = i;
            break;
          }
        }
      },
      itemContentLargeChange: function (e : SwitchChangeEvent) {
        this.itemContentLarge = e.detail.value
      },
      itemColorChange: function (e : SwitchChangeEvent) {
        this.itemColorCustom = e.detail.value
      },
      itemNumLargeChange: function (e : SwitchChangeEvent) {
        this.itemNumLargeSelect = e.detail.value
      },
      actionSheetTap() {

        let itemInfo = ['item1', 'item2', 'item3', 'item4']

        if (this.itemContentLarge) {
          itemInfo = ['两个黄鹂鸣翠柳,一行白鹭上青天。窗含西岭千秋雪,门泊东吴万里船', '水光潋滟晴方好,山色空蒙雨亦奇。 欲把西湖比西子,淡妆浓抹总相宜', '']
杜庆泉's avatar
杜庆泉 已提交
91 92
        }

杜庆泉's avatar
杜庆泉 已提交
93
        if (this.itemNumLargeSelect) {
W
微调  
wanganxp 已提交
94
          // 大量选项测试,不能超过6个元素 https://uniapp.dcloud.net.cn/api/ui/prompt.html#showactionsheet
杜庆泉's avatar
杜庆泉 已提交
95
          itemInfo = []
W
微调  
wanganxp 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
          for (var i = 1; i <= 10; i++) {
            itemInfo.push('两个黄鹂鸣翠柳,一行白鹭上青天');
          }
        }

        const that = this
        if (this.itemColorCustom) {
          uni.showActionSheet({
            title: this.items[this.current].value,
            itemList: itemInfo,
            itemColor: "#ff00ff",
            success: (e) => {
              console.log(e.tapIndex);
              uni.showToast({
                title: "点击了第" + e.tapIndex + "个选项",
                icon: "none"
              })
            },
            fail: (e) => {
              console.log(e);
            }
          })
        } else {
          uni.showActionSheet({
            title: this.items[this.current].value,
            itemList: itemInfo,
            success: (e) => {
              console.log(e.tapIndex);
              uni.showToast({
                title: "点击了第" + e.tapIndex + "个选项",
                icon: "none"
              })
Y
yurj26 已提交
128
            },
W
微调  
wanganxp 已提交
129 130 131 132 133 134
            fail: (e) => {
              console.log(e);
              uni.showToast({
                title: e.errMsg,
                icon: "none"
              })
Y
yurj26 已提交
135
            }
W
微调  
wanganxp 已提交
136
          })
Y
yurj26 已提交
137
        }
W
微调  
wanganxp 已提交
138
      },
Y
yurj26 已提交
139
    }
W
微调  
wanganxp 已提交
140 141
  }
</script>