unicloud-db.uvue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <template>
  2. <view class="content">
  3. <unicloud-db ref="udb" v-slot:default="{data, pagination, loading, error}" :collection="collection" :getcount="true"
  4. loadtime="manual">
  5. <list-view v-if="data.length>0" ref="listView" class="list" :scroll-y="true" @scrolltolower="loadMore()">
  6. <list-item class="list-item" v-for="(item, _) in data">
  7. <view class="list-item-fill">
  8. <text>{{item}}</text>
  9. </view>
  10. <view>
  11. <text class="list-item-remove" @click="remove(item.getString('_id')!)">❌</text>
  12. </view>
  13. </list-item>
  14. </list-view>
  15. <text class="loading" v-if="loading">Loading...</text>
  16. <view v-if="error!=null">{{error.errMsg}}</view>
  17. <view class="pagination" v-if="data.length>0">
  18. <text class="pagination-item">{{data.length}} / {{pagination.count}}</text>
  19. </view>
  20. </unicloud-db>
  21. <view class="btn-group">
  22. <button class="btn" @click="add()">Add</button>
  23. <button class="btn" @click="get()">Get</button>
  24. </view>
  25. </view>
  26. </template>
  27. <script>
  28. const db = uniCloud.databaseForJQL()
  29. export default {
  30. data() {
  31. return {
  32. collection: 'unicloud-db-test',
  33. collectionList: [
  34. db.collection('book').where('name == "水浒传"').getTemp(),
  35. ] as UTSJSONObject[],
  36. uniCloudElement: null as UniCloudDBElement | null,
  37. isTesting: false,
  38. addResult: {},
  39. updateResult: {},
  40. removeResult: {}
  41. }
  42. },
  43. onReady() {
  44. this.uniCloudElement = this.$refs['udb'] as UniCloudDBElement
  45. this.get();
  46. },
  47. onPullDownRefresh() {
  48. this.uniCloudElement!.loadData({
  49. clear: true,
  50. success: (_ : UniCloudDBGetResult) => {
  51. uni.stopPullDownRefresh()
  52. }
  53. })
  54. },
  55. methods: {
  56. loadMore() {
  57. this.uniCloudElement!.loadMore()
  58. },
  59. get() {
  60. this.uniCloudElement!.loadData({
  61. clear: true
  62. })
  63. },
  64. add() {
  65. const value = {
  66. title: "title-" + Date.now(),
  67. comment: "comment" + Date.now()
  68. };
  69. this.uniCloudElement!.add(value, {
  70. showToast: false,
  71. success: (res : UniCloudDBAddResult) => {
  72. this.addResult = {
  73. id: res.id
  74. };
  75. this.get();
  76. },
  77. fail: (err : any | null) => {
  78. this.showError(err)
  79. }
  80. })
  81. },
  82. update(id : string) {
  83. const value = {
  84. title: "title-" + Date.now(),
  85. comment: "comment" + Date.now()
  86. };
  87. this.uniCloudElement!.update(id, value, {
  88. showToast: false,
  89. needLoading: true,
  90. needConfirm: false,
  91. loadingTitle: "正在更新...",
  92. success: (res : UniCloudDBUpdateResult) => {
  93. this.updateResult = {
  94. updated: res.updated
  95. }
  96. },
  97. fail: (err : any | null) => {
  98. this.showError(err)
  99. }
  100. })
  101. },
  102. remove(id : string) {
  103. this.uniCloudElement!.remove(id, {
  104. showToast: false,
  105. needConfirm: false,
  106. needLoading: false,
  107. success: (res : UniCloudDBRemoveResult) => {
  108. this.removeResult = {
  109. deleted: res.deleted
  110. }
  111. },
  112. fail: (err : any | null) => {
  113. this.showError(err)
  114. }
  115. })
  116. },
  117. onQueryLoad(data : Array<UTSJSONObject>, ended : boolean, pagination : UTSJSONObject) {
  118. console.log(data, ended, pagination);
  119. },
  120. showError(err : any | null) {
  121. const error = err as UniCloudError
  122. uni.showModal({
  123. content: error.errMsg,
  124. showCancel: false
  125. })
  126. }
  127. }
  128. }
  129. </script>
  130. <style>
  131. .content {
  132. flex: 1;
  133. flex-direction: column;
  134. }
  135. .list {
  136. flex: 1;
  137. flex-direction: column;
  138. }
  139. .list-item {
  140. flex-direction: row;
  141. padding: 10px;
  142. }
  143. .list-item-fill {
  144. flex: 1;
  145. }
  146. .list-item-remove {
  147. padding: 10px;
  148. }
  149. .loading {
  150. padding: 10px;
  151. text-align: center;
  152. }
  153. .pagination {
  154. flex-direction: row;
  155. background-color: #f2f2f2;
  156. }
  157. .pagination-item {
  158. margin: auto;
  159. padding: 5px 10px;
  160. }
  161. .btn-group {
  162. flex-direction: row;
  163. }
  164. .btn {
  165. flex: 1;
  166. margin: 10px;
  167. }
  168. </style>