fa-selects.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <template>
  2. <view class="selects">
  3. <view class="" style="min-height: 70rpx;padding-top: 20rpx;" @click="show = true;">
  4. <rich-text :class="!lists_lable ? 'richColor' : ''" :nodes="nodes(lists_lable || '请选择' + title)"></rich-text>
  5. </view>
  6. <u-popup :show="show" mode="bottom" height="600" @close="close">
  7. <view class="u-flex u-flex-column">
  8. <view class="fa-column u-flex-1 u-flex fa-scroll">
  9. <scroll-view scroll-y="true" :style="[{ height: scrollHg + 'px', width: '100vw' }]">
  10. <!-- 多选-->
  11. <view v-if="checkeType == 'selects'">
  12. <checkbox-group placement="column" @change="checkSelct">
  13. <u-cell-group>
  14. <u-cell :arrow="false" v-for="(item, index) in list" :key="index" @click.self="selectCell(index)">
  15. <view slot="title"><rich-text :nodes="nodes(item[showField])"></rich-text></view>
  16. <checkbox
  17. slot="right-icon"
  18. shape="square"
  19. :class="item.disabled == true ? 'fa-disabled' : ''"
  20. :checked="item.checked"
  21. :color="theme.bgColor"
  22. :disabled="item.disabled == true"
  23. ></checkbox>
  24. </u-cell>
  25. </u-cell-group>
  26. </checkbox-group>
  27. </view>
  28. <!-- 单选 -->
  29. <view class="" v-else>
  30. <u-radio-group style="width: 100%;" v-model="radio_value" >
  31. <u-cell-group>
  32. <u-cell :arrow="false" v-for="(item, index) in list" :key="index" @click.self="selectCell(index)">
  33. <view slot="title"><rich-text :nodes="nodes(item[showField])"></rich-text></view>
  34. <u-radio slot="right-icon" :active-color="theme.bgColor" :name="item[keyField]" :disabled="item.disabled == true"></u-radio>
  35. </u-cell>
  36. </u-cell-group>
  37. </u-radio-group>
  38. </view>
  39. </scroll-view>
  40. </view>
  41. <view class="fa-column select-footer u-text-center" v-if="checkeType == 'selects'">
  42. <u-gap height="10" bg-color="#eaeaec"></u-gap>
  43. <view class="u-p-10 u-flex u-row-around">
  44. <view class="u-flex-1" v-if="checkeType == 'selects'" @click="clearAll"><text>清空</text></view>
  45. <!-- <view class="u-flex-1" @click="allSelect"> -->
  46. <!-- <text>全选</text> -->
  47. <!-- </view> -->
  48. <view class="u-flex-1" @click="confirm"><text>确定</text></view>
  49. </view>
  50. </view>
  51. <view class="fa-column select-footer u-text-center" v-if="checkeType != 'selects'">
  52. <u-gap height="5" bg-color="#eaeaec"></u-gap>
  53. <view class="u-p-10 u-flex u-row-around">
  54. <view class="u-flex-1" @click="close"><text>取消</text></view>
  55. </view>
  56. </view>
  57. </view>
  58. </u-popup>
  59. </view>
  60. </template>
  61. <script>
  62. import Emitter from '@/uni_modules/uview-ui/libs/util/emitter.js';
  63. export default {
  64. name: 'fa-selects',
  65. mixins: [Emitter],
  66. props: {
  67. value: {
  68. type: [String, Number],
  69. default: false
  70. },
  71. //数据源
  72. faList: {
  73. type: [Object, Array, String],
  74. default: ''
  75. },
  76. //选择类型
  77. checkeType: {
  78. type: String,
  79. default: 'select'
  80. },
  81. title: {
  82. type: String,
  83. default: ''
  84. },
  85. //显示的字段
  86. showField: {
  87. type: String,
  88. default: 'name'
  89. },
  90. //取值得字段
  91. keyField: {
  92. type: String,
  93. default: 'id'
  94. },
  95. //默认的值
  96. showValue: {
  97. type: [String, Number],
  98. default: ''
  99. }
  100. },
  101. computed: {
  102. nodes() {
  103. return title => {
  104. return [
  105. {
  106. name: 'div',
  107. children: [
  108. {
  109. type: 'text',
  110. text: title
  111. }
  112. ]
  113. }
  114. ];
  115. };
  116. }
  117. },
  118. watch: {
  119. faList: {
  120. immediate: true,
  121. handler(val) {
  122. if (this.$u.test.array(val)) {
  123. if (val.length > 0 && this.$u.test.object(val[0])) {
  124. this.list = JSON.parse(JSON.stringify(val));
  125. } else {
  126. this.list = [];
  127. for (let i in val) {
  128. this.list.push({
  129. name: val[i],
  130. id: i
  131. });
  132. }
  133. }
  134. } else if (this.$u.test.object(val)) {
  135. this.list = [];
  136. for (let i in val) {
  137. this.list.push({
  138. name: val[i],
  139. id: i
  140. });
  141. }
  142. }
  143. if (this.checkeType == 'selects') {
  144. this.list.forEach((item, index) => {
  145. this.$set(this.list[index], 'checked', false);
  146. });
  147. }
  148. // 单选的默认值
  149. if (this.showValue && this.checkeType == 'select') {
  150. this.list.forEach((item, index) => {
  151. if (item[this.keyField] == this.showValue) {
  152. this.radio_value = this.showValue;
  153. this.lists_lable = item[this.showField];
  154. }
  155. });
  156. }
  157. // 多选的默认值
  158. if (this.showValue && this.checkeType == 'selects') {
  159. let arr = this.showValue.split(',');
  160. let lables = [];
  161. this.list.forEach((item, index) => {
  162. arr.forEach(id => {
  163. if (item[this.keyField] == id) {
  164. this.$set(this.list[index], 'checked', !this.list[index].checked);
  165. lables.push(item[this.showField]);
  166. }
  167. });
  168. });
  169. this.lists_lable = lables.join(',');
  170. }
  171. }
  172. },
  173. //显示高度
  174. show(newValue, oldValue) {
  175. if (newValue) {
  176. this.show = true;
  177. this.$nextTick(() => {
  178. setTimeout(() => {
  179. uni.createSelectorQuery()
  180. .in(this)
  181. .select('.fa-scroll')
  182. .boundingClientRect(rect => {
  183. console.log(rect);
  184. if (rect) {
  185. this.scrollHg = rect.height;
  186. }
  187. })
  188. .exec();
  189. }, 100); //在百度直接获取不到,需要延时
  190. });
  191. } else {
  192. this.sendChange();
  193. }
  194. }
  195. },
  196. data() {
  197. return {
  198. show: false,
  199. radio_value: '',
  200. checkbox_value: '',
  201. scrollHg: 200,
  202. lists_lable: '',
  203. list: []
  204. };
  205. },
  206. methods: {
  207. close() {
  208. this.show = false;
  209. },
  210. checkSelct(e){
  211. console.log(e);
  212. console.log(this.list)
  213. for(var i=0;i<this.list.length;i++){
  214. if(this.list[i].id==e){
  215. this.selectCell(i);
  216. break;
  217. }
  218. }
  219. },
  220. selectCell(index) {
  221. console.log(index)
  222. if (this.list[index].disabled == true) {
  223. return;
  224. }
  225. if (this.checkeType == 'selects') {
  226. this.$set(this.list[index], 'checked', !this.list[index].checked);
  227. } else {
  228. //单选值确定
  229. this.radio_value = this.list[index][this.keyField] || '';
  230. this.lists_lable = this.list[index][this.showField] || '';
  231. this.$emit('input', this.radio_value);
  232. this.close();
  233. setTimeout(() => {
  234. this.dispatch('u-form-item', 'on-form-blur', this.radio_value);
  235. }, 50);
  236. }
  237. },
  238. //多选的确定
  239. confirm() {
  240. let lable = [];
  241. let ids = [];
  242. this.list.forEach(item => {
  243. if (item.checked) {
  244. lable.push(item.name);
  245. ids.push(item.id);
  246. }
  247. });
  248. this.lists_lable = lable.join(',');
  249. this.checkbox_value = ids.join(',');
  250. this.$emit('input', this.checkbox_value);
  251. setTimeout(() => {
  252. this.dispatch('u-form-item', 'on-form-blur', this.checkbox_value);
  253. }, 50);
  254. this.close();
  255. },
  256. //全选
  257. allSelect() {
  258. this.list.map(item => {
  259. item.checked = true;
  260. });
  261. },
  262. //清空
  263. clearAll() {
  264. this.list.map(item => {
  265. item.checked = false;
  266. });
  267. },
  268. //派发事件
  269. sendChange() {
  270. setTimeout(() => {
  271. if (this.checkeType == 'select') {
  272. this.dispatch('u-form-item', 'on-form-change', this.radio_value);
  273. } else {
  274. this.dispatch('u-form-item', 'on-form-change', this.checkbox_value);
  275. }
  276. }, 50);
  277. }
  278. }
  279. };
  280. </script>
  281. <style lang="scss" scoped>
  282. .selects {
  283. width: 100%;
  284. }
  285. .richColor {
  286. color: #909399;
  287. }
  288. .u-flex-column {
  289. flex-direction: column;
  290. height: 100%;
  291. .fa-column {
  292. width: 100%;
  293. }
  294. }
  295. </style>