uni-clientdb-component.md 7.2 KB
Newer Older
d-u-a's avatar
d-u-a 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 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 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 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 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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
# uni-clientdb

uni-clientdb是一个数据库查询组件

HBuilderX 2.9.5+ 支持

**平台差异说明**

|App|H5|微信小程序|支付宝小程序|百度小程序|字节跳动小程序|
|:-:|:-:|:-:|:-:|:-:|:-:|
|√|√|√|√|√|√|√|√|√|


#### 属性

|属性|类型|描述|
|:-|:-|:-|
|v-slot:default||查询状态及结果|
|collection|string|表名|
|orderby|string|排序|
|field|string|查询字段,多个字段用 `,` 分割|
|where|string|查询条件,[详情](https://uniapp.dcloud.net.cn/uniCloud/uni-clientDB?id=jsquery)|
|getone|Boolean|指定查询结果是否返回数组第一条数据,默认位 false,应用场景:详情页|
|action|string|调用数据库查询前后执行的操作,[详情](https://uniapp.dcloud.net.cn/uniCloud/uni-clientDB?id=%e4%ba%91%e7%ab%af%e9%83%a8%e5%88%86)|
|manual|Boolean|是否手动加载数据,默认位 false,自动加载数据|
|@load|EventHandle|成功回调|
|@error|EventHandle|失败回调|

**提示:目前仅支持单表查询**


#### v-slot:default

```
<uni-clientdb v-slot:default="{data, pagination, loading, error, options}"></uni-clientdb>
```


|属性|类型|描述|
|:-|:-|:-|
|data|Array&#124;Object|查询结果,默认值为`Array`, 当 `getone` 指定为 `true` 时,值为数组中第一条数据,类型为 `Object`|
|pagination|Object|分页属性|
|loading|Boolean|查询状态|
|error|Object|查询错误|
|options|Object|插槽不能访问外面的数据,通过此参数传递, 不支持传递函数|

**提示:如果不指定分页模式, `data` 为多次查询的集合**


#### orderby

格式为 `字段名` 空格 `asc`(升序)/`desc`(降序)`,多个字段用 `,` 分割,优先级为字段顺序

示例代码
```
<uni-clientdb orderby="createTime desc"></uni-clientdb>
```



#### pagination属性

|属性|类型|描述|
|:-|:-|:-|
|current|Number|当前页|
|size|Number|每页多少条数据|
|total|Boolean|分页模式,默认 `false`,需要分页模式时指定为 `true`|


#### 事件

```
@load
handleLoad(data, ended, pagination) {
  // `data` 当前查询结果
  // `ended` 是否有更多数据
  // `pagination` 分页信息
}
```

```
@error
handleError(e) {
  // {errorMessage}
}
```


#### 方法

```
// loadMore
// 使用ref方式访问组件方法加载更过数据,每加载成功一次,当前页 +1
this.$refs.dataQuery.loadMore()
```


#### 示例代码


**需要在 uniCloud 控制台增加 Schema 权限配置**,详情 [https://uniapp.dcloud.net.cn/uniCloud/schema](https://uniapp.dcloud.net.cn/uniCloud/schema)


滚动模式

```

<template>
  <view class="content">
    <uni-clientdb ref="dataQuery" v-slot:default="{data, pagination, loading, error, options}"
      :options="options"
      :collection="collection"
      orderby="createTime desc"
      field="name,subType,createTime"
      :getone="false"
      :action="action"
      :where="where"
      :pagination="pagination" @load="onqueryload" @error="onqueryerror">
      <view>{{pagination}}</view>
      <view v-if="error" class="error">{{error.errMsg}}</view>
      <view v-else class="list">
        <view v-for="(item, index) in data" :key="index" class="list-item">
          {{ item.createTime }}
          <!-- 使用日期格式化组件,详情见插件 https://ext.dcloud.net.cn/search?q=date-format -->
          <!-- <uni-date-format :date="item.createTime" /> -->
        </view>
      </view>
      <view class="loading" v-if="loading">加载中...</view>
    </uni-clientdb>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        options: {}, // 插槽不能访问外面的数据,通过此参数传递, 不支持传递函数
        collection: 'unicloud-test',
        pagination: {
          current: 1,
          size: 10,
          total: true
        },
        action: '',
        where: {} // 类型为对象或字符串
      }
    },
    onReady() {
      // 当 uni-clientdb manual 属性设为为 true 时,手动加载数据
      // this.$refs.dataQuery.loadData()
    },
    onPullDownRefresh() {
      this.$refs.dataQuery.loadData({
        clear: true
      }, () => {
        uni.stopPullDownRefresh()
      })
    },
    onReachBottom() {
      this.$refs.dataQuery.loadMore()
    },
    methods: {
      onqueryload(data, ended) {
        // 格式化数据
        data.forEach((item) => {
          item.createTime = new Date(item.createTime).toLocaleString()
        })

        if (ended) {
          // 没有更多数据了
        }
      },
      onqueryerror(e) {
        // 加载数据失败
      }
    }
  }
</script>

<style>
  .content {
    display: flex;
    flex-direction: column;
    background-color: #f0f0f0;
  }

  .list-item {
    background-color: #fff;
    margin-bottom: 1px;
    padding: 30px 15px;
  }

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

  .error {
    color: #DD524D;
  }
</style>

```


分页模式

```
<template>
  <view class="content">
    <uni-clientdb ref="dataQuery" v-slot:default="{data, pagination, loading, error, options}"
      :options="options"
      :collection="collection"
      orderby="createTime desc"
      field="name,subType,createTime"
      :getone="false"
      :action="action"
      :pagination="pagination" @load="onqueryload" @error="onqueryerror">
      <view>{{pagination}}</view>
      <view v-if="error" class="error">{{error.errMsg}}</view>
      <view v-else class="list">
        <view v-for="(item, index) in data" :key="index" class="list-item">
          {{ item.createTime }}
        </view>
      </view>
      <view class="loading" v-if="loading">加载中...</view>
      <!-- 分页组件 -->
      <uni-pagination show-icon :page-size="pagination.size" v-model="options.current" total="pagination.total" @change="onpagination" />
    </uni-clientdb>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        options: {}, // 插槽不能访问外面的数据,通过此参数传递, 不支持传递函数
        collection: 'unicloud-test',
        pagination: {
          current: 1,
          size: 10,
          total: true
        },
        action: '',
        where: {} // 类型为对象或字符串
      }
    },
    onPullDownRefresh() {
      this.$refs.dataQuery.loadData({
        clear: true
      }, () => {
        uni.stopPullDownRefresh()
      })
    },
    methods: {
      onqueryload(data, ended) {
        data.forEach((item) => {
          item.createTime = new Date(item.createTime).toLocaleString()
        })

        if (ended) {
          // 没有更多数据了
        }
      },
      onqueryerror(e) {
        // 加载数据失败
      },
      onpagination(e) {
        this.$refs.dataQuery.loadData({
          current: e.current
        })
      }
    }
  }
</script>

<style>
  .content {
    display: flex;
    flex-direction: column;
    background-color: #f0f0f0;
  }

  .list-item {
    background-color: #fff;
    margin-bottom: 1px;
    padding: 30px 15px;
  }

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

  .error {
    color: #DD524D;
  }
</style>

```


**调试小技巧**

- H5平台,开发模式下浏览器控制台输入 `unidev.clientDB.data`,可查看组件内部数据,多个组件通过索引查看 `unidev.clientDB.data[0]`