detail.uvue 3.1 KB
Newer Older
1 2 3
<template>
  <scroll-view class="page">
    <unicloud-db ref="udb" v-slot:default="{data, loading, error}" :collection="collection" :where="where"
4
      page-data="replace">
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
      <view v-if="error!=null" class="error">{{error.errMsg}}</view>
      <view v-if="loading" class="loading">正在加载...</view>
      <view v-if="data.length>0">
        <view class="form-item">
          <text class="form-item-label">姓名</text>
          <text class="form-item-input">{{data[0]['username']}}</text>
        </view>
        <view class="form-item">
          <text class="form-item-label">电话</text>
          <text class="form-item-input">{{data[0]['mobile']}}</text>
        </view>
        <view class="form-item">
          <text class="form-item-label">邮箱</text>
          <text class="form-item-input">{{data[0]['email']}}</text>
        </view>
        <view class="form-item">
          <text class="form-item-label">备注</text>
          <text class="form-item-input">{{data[0]['comment']}}</text>
        </view>
        <view class="form-item">
          <text class="form-item-label">性别</text>
26
          <text class="form-item-input">{{displayGender(data[0].getNumber('gender'))}}</text>
27
        </view>
A
Anne_LXM 已提交
28
        <button type="default" @click="gotoUpdatePage(data[0].getString('_id'))">编辑</button>
29 30 31 32 33 34 35 36 37 38 39
      </view>
    </unicloud-db>
  </scroll-view>
</template>

<script>
  import { COLLECTION_NAME, GenderType, GenderList, UNICLOUD_DB_CONTACTS_UPDATE } from './types.uts'

  export default {
    data() {
      return {
40
        collection: '',
41
        where: '',
42 43
        whereID: '',
        uniCloudElement: null as UniCloudDBElement | null
44 45
      }
    },
46
    onLoad(options) {
47
      this.collection = COLLECTION_NAME;
48 49
      this.whereID = options['id'] as string;
      this.where = `_id=='${this.whereID}'`;
50 51 52 53 54 55 56 57 58 59
    },
    onShow() {
      // TODO 后续通过 EventChannel 实现
      uni.$off(UNICLOUD_DB_CONTACTS_UPDATE, this.onDataChange);
    },
    onUnload() {
      // TODO 后续通过 EventChannel 实现
      uni.$off(UNICLOUD_DB_CONTACTS_UPDATE, this.onDataChange);
    },
    onReady() {
60
      this.uniCloudElement = this.$refs['udb'] as UniCloudDBElement
61 62
    },
    methods: {
63 64
      displayGender(value : number | null) : string {
        const str = value ?? -1
65 66 67 68
        return (GenderList as GenderType[]).find((item : GenderType) : boolean => {
          return item.value == str
        })!.text;
      },
69
      gotoUpdatePage(id : string | null) {
70 71 72 73 74 75
        // TODO 后续通过 EventChannel 实现
        uni.$on(UNICLOUD_DB_CONTACTS_UPDATE, this.onDataChange);
        uni.navigateTo({
          url: './edit?id=' + id
        })
      },
76
      onDataChange(_ : string) {
77
        this.uniCloudElement!.loadData()
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
      }
    }
  }
</script>

<style>
  .page {
    padding: 15px;
  }

  .loading {
    align-items: center;
  }

  .form-item {
    flex-direction: row;
    margin-bottom: 15px;
    align-items: center;
    padding: 8px 0;
  }

  .form-item-label {
    width: 45px;
    margin-right: 10px;
  }

  .form-item-input {
    flex: 1;
    font-size: 14px;
    color: #666;
  }
</style>