tui-sel-dict.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <template>
  2. <picker :disabled="disabled" class="abow-sel-dict" @change="selChange" :start="start" :end="end" :value="idx"
  3. :range="listData" range-key="label" :mode="modetype">
  4. <slot>
  5. <block v-if="modetype=='selector'">
  6. <view class="content" :class="{ placeholder: !selData }">
  7. <text>{{selData&&selData.label?selData.label:placeholder}}</text>
  8. </view>
  9. </block>
  10. <block v-else>
  11. <view class="content" :class="{ placeholder: !selData }">
  12. <text>{{ selData ? selData : placeholder }}</text>
  13. </view>
  14. </block>
  15. </slot>
  16. </picker>
  17. </template>
  18. <script>
  19. export default {
  20. data() {
  21. return {
  22. idx: 0,
  23. listData: "",
  24. selData: null
  25. }
  26. },
  27. props: {
  28. disabled: {
  29. type: Boolean,
  30. default: false
  31. },
  32. modetype: {
  33. type: String,
  34. default: "selector" // selector:单选: multiSelector:联级选择(别用); time:时间; date:日期;
  35. },
  36. start: {
  37. type: String,
  38. default: ""
  39. },
  40. end: {
  41. type: String,
  42. default: ""
  43. },
  44. // 占位符
  45. placeholder: {
  46. type: String,
  47. default: "无"
  48. },
  49. apiUrl: {
  50. type: String,
  51. default: ""
  52. },
  53. isRecords: {
  54. type: String,
  55. default: ""
  56. },
  57. postData: {
  58. type: Object,
  59. default: null
  60. },
  61. keyLabel: {
  62. type: String,
  63. default: "name"
  64. },
  65. keyId: {
  66. type: String,
  67. default: "id"
  68. },
  69. daType: {
  70. type: [String],
  71. default: "sex"
  72. },
  73. list: {
  74. type: [Array],
  75. default: () => {
  76. return []
  77. }
  78. },
  79. setfirst: {
  80. type: Boolean,
  81. default: false
  82. },
  83. value: {
  84. type: [Number, String],
  85. default: ""
  86. },
  87. isSelAll: {
  88. type: Boolean,
  89. default: false
  90. }
  91. },
  92. watch: {
  93. daType(val) {
  94. this.initData()
  95. },
  96. value(val) {
  97. this.init()
  98. },
  99. list(val) {
  100. this.initData()
  101. }
  102. },
  103. created() {
  104. this.initData(true)
  105. },
  106. methods: {
  107. initData(type) {
  108. if (this.modetype != 'selector') {
  109. return false
  110. }
  111. if (this.apiUrl) {
  112. this.loadData(type)
  113. } else if (this.daType == 'sex') {
  114. this.listData = [{
  115. label: "女",
  116. id: 0
  117. }, {
  118. label: "男",
  119. id: 1
  120. }]
  121. this.init(type)
  122. } else if (this.list && this.list.length > 0) {
  123. this.listData = this.list.map(item => {
  124. this.$set(item, 'label', item[this.keyLabel])
  125. this.$set(item, 'id', item[this.keyId])
  126. return item
  127. })
  128. this.init(type)
  129. }
  130. },
  131. init(type) {
  132. if (this.modetype == 'date') {
  133. this.selData = this.formatDate(this.value, "yyyy-MM-dd")
  134. return false
  135. }
  136. if (this.modetype != 'selector') {
  137. return false
  138. }
  139. if (!this.listData || this.listData.length <= 0) {
  140. this.idx = 0
  141. this.selData = null
  142. return false
  143. }
  144. if (!this.value && this.listData.length > 0 && this.setfirst) {
  145. this.idx = 0
  146. this.selData = this.listData[0]
  147. this.$emit("input", this.listData)
  148. this.$emit("change", this.selData)
  149. return false
  150. }
  151. this.listData.forEach((item, index) => {
  152. if (String(this.value) == String(item.id)) {
  153. this.idx = index
  154. this.selData = item
  155. this.$emit("change", this.selData, type)
  156. }
  157. })
  158. },
  159. selChange(e) {
  160. if (this.modetype == 'selector') {
  161. if (!this.listData || this.listData.length <= 0) {
  162. return false
  163. }
  164. this.idx = parseInt(e.detail.value)
  165. this.selData = this.listData[this.idx]
  166. this.$emit("input", this.selData.id)
  167. this.$emit("change", this.selData)
  168. } else if (this.modetype == 'date') {
  169. this.listData = e.detail.value
  170. var t = new Date(this.listData).getTime()
  171. this.$emit("input", t)
  172. this.$emit("change", t)
  173. } else {
  174. this.listData = e.detail.value
  175. this.$emit("input", this.listData)
  176. this.$emit("change", this.listData)
  177. }
  178. },
  179. loadData(type) {
  180. this.$tui.request(this.apiUrl, this.postData).then(res => {
  181. if (res.code == 0) {
  182. if (this.isRecords) {
  183. this.listData = res.items[this.isRecords].map((item, index) => {
  184. return {
  185. label: item[this.keyLabel],
  186. id: item[this.keyId]
  187. }
  188. });
  189. } else {
  190. this.listData = res.items.map((item, index) => {
  191. return {
  192. label: item[this.keyLabel],
  193. id: item[this.keyId]
  194. }
  195. });
  196. }
  197. console.log(this.isSelAll,'全部全部全部全部11')
  198. if (this.isSelAll) {
  199. console.log('全部全部全部全部22')
  200. this.listData.unshift({
  201. label:'全部',
  202. id: ''
  203. })
  204. }
  205. this.init(type)
  206. }
  207. }).catch(res => {})
  208. },
  209. formatDate(datestr, _temp = "yyyy-MM-dd hh:mm:ss") {
  210. var _date
  211. if (datestr) {
  212. if (!isNaN(datestr)) {
  213. datestr = new Date(parseInt(datestr))
  214. }
  215. var reg = new RegExp('-', "g");
  216. datestr = String(datestr).replace(reg, '/');
  217. _date = new Date(datestr);
  218. } else {
  219. return ""
  220. }
  221. var o = {
  222. "M+": _date.getMonth() + 1, //月份
  223. "d+": _date.getDate(), //日
  224. "h+": _date.getHours(), //小时
  225. "m+": _date.getMinutes(), //分
  226. "s+": _date.getSeconds(), //秒
  227. "q+": Math.floor((_date.getMonth() + 3) / 3), //季度
  228. "S": _date.getMilliseconds() //毫秒
  229. };
  230. if (/(y+)/.test(_temp)) {
  231. _temp = _temp.replace(RegExp.$1, (_date.getFullYear() + "").substr(4 - RegExp.$1.length));
  232. }
  233. for (var k in o) {
  234. if (new RegExp("(" + k + ")").test(_temp)) {
  235. _temp = _temp.replace(
  236. RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  237. }
  238. }
  239. return _temp;
  240. }
  241. }
  242. }
  243. </script>
  244. <style lang="scss" scoped>
  245. .abow-sel-dict {
  246. flex: 1;
  247. }
  248. .content {
  249. height: 100%;
  250. width: 100%;
  251. text-align: right;
  252. }
  253. .placeholder {
  254. height: 100%;
  255. width: 100%;
  256. color: #999999;
  257. }
  258. </style>