list.uvue 3.7 KB
Newer Older
1 2
<template>
  <view class="page">
3 4
    <unicloud-db ref="udb" class="udb" v-slot:default="{data, pagination, loading, hasMore, error}"
      :collection="collection" :page-size="15" :getcount="true" loadtime="manual">
5
      <view v-if="error!=null" class="error">{{error.errMsg}}</view>
雪洛's avatar
雪洛 已提交
6 7
      <scroll-view ref="listView" class="list-view" @scrolltolower="loadMore()">
        <view class="list-item" v-for="(item, _) in data" @click="gotoDetailPage(item['_id'] as string)">
8 9 10 11
          <view class="list-item-fill">
            <text class="name">{{item['username']}}</text>
            <text class="mobile">{{item['mobile']}}</text>
          </view>
雪洛's avatar
雪洛 已提交
12 13
        </view>
      </scroll-view>
14
      <uni-loading v-if="loading" class="loading" color="#999" text="正在加载..."></uni-loading>
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
      <view v-if="data.length>0" class="pagination">
        <text class="pagination-item">{{data.length}} / {{pagination.count}}</text>
      </view>
    </unicloud-db>
    <view class="btn-plus" @click="gotoAddPage">
      <text class="btn-plus-text">+</text>
    </view>
  </view>
</template>

<script>
  import { COLLECTION_NAME, UNICLOUD_DB_CONTACTS_ADD, UNICLOUD_DB_CONTACTS_UPDATE, UNICLOUD_DB_CONTACTS_DELETE } from './types.uts'

  export default {
    data() {
      return {
        collection: COLLECTION_NAME,
32
        uniCloudElement: null as UniCloudDBElement | null
33 34 35 36 37
      }
    },
    onReady() {
      // TODO 后续通过 EventChannel 实现
      uni.$on(UNICLOUD_DB_CONTACTS_DELETE, this.onDataChange);
38 39
      this.uniCloudElement = this.$refs['udb'] as UniCloudDBElement
      this.uniCloudElement!.loadData()
40 41 42 43 44 45 46 47 48 49 50 51 52
    },
    onUnload() {
      // TODO 后续通过 EventChannel 实现
      uni.$off(UNICLOUD_DB_CONTACTS_ADD, this.onDataChange);
      uni.$off(UNICLOUD_DB_CONTACTS_UPDATE, this.onDataChange);
      uni.$off(UNICLOUD_DB_CONTACTS_DELETE, this.onDataChange);
    },
    onShow() {
      // TODO 后续通过 EventChannel 实现
      uni.$off(UNICLOUD_DB_CONTACTS_ADD, this.onDataChange);
      uni.$off(UNICLOUD_DB_CONTACTS_UPDATE, this.onDataChange);
    },
    onPullDownRefresh() {
53
      this.uniCloudElement!.loadData({
54 55 56 57 58 59 60 61
        clear: true,
        success: (_ : UniCloudDBGetResult) => {
          uni.stopPullDownRefresh()
        }
      })
    },
    methods: {
      loadMore() {
62
        this.uniCloudElement!.loadMore()
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
      },
      gotoAddPage() {
        // TODO 后续通过 EventChannel 实现
        uni.$on(UNICLOUD_DB_CONTACTS_ADD, this.onDataChange);
        uni.navigateTo({
          url: './add'
        })
      },
      gotoDetailPage(id : string) {
        // TODO 后续通过 EventChannel 实现
        uni.$on(UNICLOUD_DB_CONTACTS_UPDATE, this.onDataChange);
        uni.navigateTo({
          url: './detail?id=' + id
        })
      },
78
      onDataChange(_ : string) {
79
        this.uniCloudElement!.loadData({
80 81 82 83 84 85 86 87 88 89 90 91 92 93
          clear: true
        })
      }
    }
  }
</script>

<style>
  .page {
    flex: 1;
    flex-direction: column;
  }

  .loading {
94 95
    margin: 20px;
    align-self: center;
96 97
  }

98 99
  .udb {
    flex: 1;
100 101 102 103 104
    /* TODO */
    /* #ifdef WEB */
    display: flex;
    /* #endif */
    flex-direction: column;
105 106
  }

107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
  .list-view {
    flex: 1;
    flex-direction: column;
  }

  .list-item {
    flex-direction: row;
    padding: 10px;
    background-color: #fff;
    margin-bottom: 1px;
  }

  .mobile {
    margin-top: 5px;
  }

  .btn-plus {
    position: absolute;
    width: 64px;
    height: 64px;
    right: 20px;
    bottom: 20px;
    align-items: center;
    justify-content: center;
    background-color: #1e90ff;
    border-radius: 64px;
  }

  .btn-plus-text {
    font-size: 30px;
    color: #fff;
  }

  .pagination {
    align-items: center;
    background-color: #f8f8f8;
    padding: 15px;
  }
</style>