unicloud-db.uvue 4.3 KB
Newer Older
1 2
<template>
  <view class="content">
A
Anne_LXM 已提交
3
    <unicloud-db ref="udb" v-slot:default="{data, pagination, loading, error}"
4 5 6 7 8 9 10 11 12 13 14 15
      :collection="collection" :getcount="true" loadtime="manual">
      <list-view v-if="data.length>0" ref="listView" class="list" :scroll-y="true" @scrolltolower="loadMore()">
        <list-item class="list-item" v-for="(item, _) in data">
          <view class="list-item-fill">
            <text>{{item}}</text>
          </view>
          <view>
            <text class="list-item-remove" @click="remove(item.getString('_id')!)">❌</text>
          </view>
        </list-item>
      </list-view>
      <text class="loading" v-if="loading">Loading...</text>
A
Anne_LXM 已提交
16
      <view v-if="error!=null">{{error.errMsg}}</view>
17 18 19 20 21
      <view class="pagination" v-if="data.length>0">
        <text class="pagination-item">{{data.length}} / {{pagination.count}}</text>
      </view>
    </unicloud-db>
    <view class="btn-group">
A
Anne_LXM 已提交
22 23
      <button class="btn" @click="add()">Add</button>
      <button class="btn" @click="get()">Get</button>
24 25 26 27 28 29 30 31 32 33 34 35 36 37
    </view>
  </view>
</template>

<script>
  const db = uniCloud.databaseForJQL()

  export default {
    data() {
      return {
        collection: 'unicloud-db-test',
        collectionList: [
          db.collection('book').where('name == "水浒传"').getTemp(),
        ] as UTSJSONObject[],
38 39 40 41 42
        uniCloudElement: null as UniCloudDBElement | null,
        isTesting: false,
        addResult: {},
        updateResult: {},
        removeResult: {}
43 44 45
      }
    },
    onReady() {
46
      this.uniCloudElement = this.$refs['udb'] as UniCloudDBElement
47 48 49
      this.get();
    },
    onPullDownRefresh() {
50
      this.uniCloudElement!.loadData({
51 52 53 54 55 56 57 58
        clear: true,
        success: (_ : UniCloudDBGetResult) => {
          uni.stopPullDownRefresh()
        }
      })
    },
    methods: {
      loadMore() {
59
        this.uniCloudElement!.loadMore()
60 61
      },
      get() {
62
        this.uniCloudElement!.loadData({
63 64 65 66 67 68 69 70
          clear: true
        })
      },
      add() {
        const value = {
          title: "title-" + Date.now(),
          comment: "comment" + Date.now()
        };
71
        this.uniCloudElement!.add(value, {
72 73
          showToast: false,
          success: (res : UniCloudDBAddResult) => {
74
            this.addResult = {
75 76 77 78 79 80 81 82 83 84 85 86 87 88
              id: res.id
            };
            this.get();
          },
          fail: (err : any | null) => {
            this.showError(err)
          }
        })
      },
      update(id : string) {
        const value = {
          title: "title-" + Date.now(),
          comment: "comment" + Date.now()
        };
89
        this.uniCloudElement!.update(id, value, {
90 91 92 93 94
          showToast: false,
          needLoading: true,
          needConfirm: false,
          loadingTitle: "正在更新...",
          success: (res : UniCloudDBUpdateResult) => {
95
            this.updateResult = {
96 97 98 99 100 101 102 103 104
              updated: res.updated
            }
          },
          fail: (err : any | null) => {
            this.showError(err)
          }
        })
      },
      remove(id : string) {
105
        this.uniCloudElement!.remove(id, {
106 107 108 109
          showToast: false,
          needConfirm: false,
          needLoading: false,
          success: (res : UniCloudDBRemoveResult) => {
110
            this.removeResult = {
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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
              deleted: res.deleted
            }
          },
          fail: (err : any | null) => {
            this.showError(err)
          }
        })
      },
      onQueryLoad(data : Array<UTSJSONObject>, ended : boolean, pagination : UTSJSONObject) {
        console.log(data, ended, pagination);
      },
      showError(err : any | null) {
        const error = err as UniCloudError
        uni.showModal({
          content: error.errMsg,
          showCancel: false
        })
      }
    }
  }
</script>

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

  .list {
    flex: 1;
    flex-direction: column;
  }

  .list-item {
    flex-direction: row;
    padding: 10px;
  }

  .list-item-fill {
    flex: 1;
  }

  .list-item-remove {
    padding: 10px;
  }

  .loading {
    padding: 10px;
    text-align: center;
  }

  .pagination {
    flex-direction: row;
    background-color: #f2f2f2;
  }

  .pagination-item {
    margin: auto;
    padding: 5px 10px;
  }

  .btn-group {
    flex-direction: row;
  }

  .btn {
    flex: 1;
    margin: 10px;
  }
A
Anne_LXM 已提交
180
</style>