list.uvue 3.6 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 6 7 8 9 10 11 12 13
      <view v-if="error!=null" class="error">{{error.errMsg}}</view>
      <list-view ref="listView" class="list-view" :scroll-y="true" @scrolltolower="loadMore()">
        <list-item class="list-item" v-for="(item, _) in data" @click="gotoDetailPage(item['_id'] as string)">
          <view class="list-item-fill">
            <text class="name">{{item['username']}}</text>
            <text class="mobile">{{item['mobile']}}</text>
          </view>
        </list-item>
      </list-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 32 33 34 35 36 37 38 39 40 41 42 43 44 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
      <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,
        $uniCloudElement: null as UniCloudDBElement | null
      }
    },
    onReady() {
      // TODO 后续通过 EventChannel 实现
      uni.$on(UNICLOUD_DB_CONTACTS_DELETE, this.onDataChange);
      this.$uniCloudElement = this.$refs['udb'] as UniCloudDBElement
      this.$uniCloudElement!.loadData()
    },
    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() {
      this.$uniCloudElement!.loadData({
        clear: true,
        success: (_ : UniCloudDBGetResult) => {
          uni.stopPullDownRefresh()
        }
      })
    },
    methods: {
      loadMore() {
        this.$uniCloudElement!.loadMore()
      },
      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 80 81 82 83 84 85 86 87 88 89 90 91 92 93
        this.$uniCloudElement!.loadData({
          clear: true
        })
      }
    }
  }
</script>

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

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

98 99 100 101
  .udb {
    flex: 1;
  }

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 128 129 130 131 132 133 134 135 136 137 138 139 140
  .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>