edit.uvue 5.0 KB
Newer Older
1
<template>
雪洛's avatar
雪洛 已提交
2
  <scroll-view class="scroll-view">
3
    <unicloud-db ref="udb" v-slot:default="{data, loading, error}" :collection="collection" :where="where"
4
      page-data="replace">
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
      <view v-if="error!=null" class="error">{{error.errMsg}}</view>
      <view v-if="loading" class="loading">正在加载...</view>
      <form v-if="data.length>0" @submit="onFormSubmit">
        <view class="form-item">
          <text class="form-item-label">姓名</text>
          <input class="form-item-input" placeholder="姓名" name="username" :value="data[0].getString('username')" />
        </view>
        <view class="form-item">
          <text class="form-item-label">电话</text>
          <input class="form-item-input" placeholder="电话" name="mobile" :value="data[0].getString('mobile')" />
        </view>
        <view class="form-item">
          <text class="form-item-label">邮箱</text>
          <input class="form-item-input" placeholder="邮箱地址" name="email" :value="data[0].getString('email')" />
        </view>
        <view class="form-item">
          <text class="form-item-label">备注</text>
          <textarea class="form-item-input" placeholder="备注" name="comment" :value="data[0].getString('comment')" />
        </view>
        <view class="form-item">
          <text class="form-item-label">性别</text>
          <radio-group class="radio-list" name="gender">
            <view class="radio-item" v-for="(item, _) in genderList" :key="item.value">
              <radio :value="item.value" :checked="item.value == data[0].getNumber('gender')" />
              <text>{{item.text}}</text>
            </view>
          </radio-group>
        </view>
        <view class="btn-group">
          <button class="btn-submit" type="primary" form-type="submit">保存</button>
35 36 37
          <button class="btn-delete" type="warn"
            @click="remove(data[0].getString('_id'), data[0].getString('username'))">删除联系人</button>
        </view>
38 39 40
      </form>
    </unicloud-db>
  </scroll-view>
41 42 43 44 45 46 47 48
</template>

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

  export default {
    data() {
      return {
49
        collection: '',
50 51
        where: '',
        genderList: GenderList as GenderType[],
52 53
        whereID: '',
        uniCloudElement: null as UniCloudDBElement | null
54 55
      }
    },
56
    onLoad(options) {
57
      this.collection = COLLECTION_NAME;
58 59
      this.whereID = options['id'] as string;
      this.where = `_id=='${this.whereID}'`;
60 61
    },
    onReady() {
62
      this.uniCloudElement = this.$refs['udb'] as UniCloudDBElement
63 64
    },
    methods: {
H
hdx 已提交
65
      onFormSubmit: function (e : UniFormSubmitEvent) {
66 67 68
        const formData = e.detail.value
        const genderString = formData['gender'] as string
        formData['gender'] = (genderString.length > 0) ? parseInt(genderString) : -1
69
        this.uniCloudElement!.update(this.whereID, formData, {
70 71 72 73 74 75
          showToast: false,
          needLoading: true,
          needConfirm: false,
          loadingTitle: "正在保存...",
          success: (_ : UniCloudDBUpdateResult) => {
            // TODO 后续通过 EventChannel 实现
76
            uni.$emit(UNICLOUD_DB_CONTACTS_UPDATE, this.whereID)
77 78 79 80 81 82 83 84 85 86 87 88 89 90
            setTimeout(() => {
              uni.navigateBack()
            }, 500)
          },
          fail: (err : any | null) => {
            const error = err as UniCloudError
            uni.showModal({
              content: error.errMsg,
              showCancel: false
            })
          }
        })
      },
      remove(id : string | null, name : string | null) {
91
        this.uniCloudElement!.remove(id!, {
92 93 94 95 96 97 98
          needConfirm: true,
          needLoading: true,
          loadingTitle: "正在删除...",
          confirmTitle: "确定删除?",
          confirmContent: name,
          success: (_ : UniCloudDBRemoveResult) => {
            // TODO 后续通过 EventChannel 实现
99
            uni.$emit(UNICLOUD_DB_CONTACTS_DELETE, this.whereID)
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
            setTimeout(() => {
              uni.navigateBack({
                delta: 2
              })
            }, 500)
          },
          fail: (err : any | null) => {
            const error = err as UniCloudError
            uni.showModal({
              content: error.errMsg,
              showCancel: false
            })
          }
        })
      }
    }
  }
</script>

<style>
  .page {
    flex: 1;
  }

  .scroll-view {
    padding: 15px;
    flex: 1;
  }

  .loading {
    align-items: center;
  }

  .form-item {
    flex-direction: row;
    margin-bottom: 15px;
    align-items: center;
  }

  .form-item-label {
    width: 45px;
    margin-right: 10px;
  }

  .form-item-input {
    flex: 1;
    font-size: 14px;
    color: #666;
    border: 1px #e5e5e5 solid;
    border-radius: 5px;
    padding: 8px;
  }

  .radio-list {
    flex-direction: row;
  }

  .radio-item {
    flex-direction: row;
    margin-right: 30px;
    align-items: center;
  }

163 164
  .btn-group {
    margin-top: 30px;
165 166 167
  }

  .btn-delete {
168
    margin-top: 15px;
169
  }
170
</style>