uni-data-checkbox.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  1. <template>
  2. <view class="uni-data-checklist">
  3. <template v-if="!isLocal">
  4. <view class="uni-data-loading">
  5. <uni-load-more v-if="!mixinDatacomErrorMessage" status="loading" iconType="snow" :iconSize="18" :content-text="contentText"></uni-load-more>
  6. <text v-else>{{mixinDatacomErrorMessage}}</text>
  7. </view>
  8. </template>
  9. <template v-else>
  10. <checkbox-group v-if="multiple" class="checklist-group" :class="{'is-list':mode==='list' || wrap}" @change="chagne">
  11. <!-- :class="item.labelClass" -->
  12. <label class="checklist-box" :class="['is--'+mode,item.selected?'is-checked':'',(disabled || !!item.disabled)?'is-disable':'',index!==0&&mode==='list'?'is-list-border':'']"
  13. :style="item.styleBackgroud" v-for="(item,index) in dataList" :key="index">
  14. <checkbox class="hidden" hidden :disabled="disabled || !!item.disabled" :value="item.value+''" :checked="item.selected" />
  15. <!-- :style="item.styleIcon" -->
  16. <view v-if="(mode !=='tag' && mode !== 'list') || ( mode === 'list' && icon === 'left')" class="checkbox__inner" :style="item.styleIcon">
  17. <!-- :class="item.checkboxClass" -->
  18. <view class="checkbox__inner-icon"></view>
  19. </view>
  20. <view class="checklist-content" :class="{'list-content':mode === 'list' && icon ==='left'}">
  21. <!-- :class="item.textClass" -->
  22. <text class="checklist-text" :style="item.styleIconText">{{item.text}}</text>
  23. <!-- :class="item.listClass" -->
  24. <view v-if="mode === 'list' && icon === 'right'" class="checkobx__list" :style="item.styleBackgroud"></view>
  25. </view>
  26. </label>
  27. </checkbox-group>
  28. <radio-group v-else class="checklist-group" :class="{'is-list':mode==='list','is-wrap':wrap}" @change="chagne">
  29. <!-- -->
  30. <label class="checklist-box" :class="['is--'+mode,item.selected?'is-checked':'',(disabled || !!item.disabled)?'is-disable':'',index!==0&&mode==='list'?'is-list-border':'']"
  31. :style="item.styleBackgroud" v-for="(item,index) in dataList" :key="index">
  32. <radio class="hidden" hidden :disabled="disabled || item.disabled" :value="item.value+''" :checked="item.selected" />
  33. <!-- :class="item.checkboxBgClass" -->
  34. <view v-if="(mode !=='tag' && mode !== 'list') || ( mode === 'list' && icon === 'left')" class="radio__inner"
  35. :style="item.styleBackgroud">
  36. <!-- :class="item.checkboxClass" -->
  37. <view class="radio__inner-icon" :style="item.styleIcon"></view>
  38. </view>
  39. <view class="checklist-content" :class="{'list-content':mode === 'list' && icon ==='left'}">
  40. <!-- :class="item.textClass" -->
  41. <text class="checklist-text" :style="item.styleIconText">{{item.text}}</text>
  42. <!-- :class="item.listClass" -->
  43. <view v-if="mode === 'list' && icon === 'right'" :style="item.styleRightIcon" class="checkobx__list"></view>
  44. </view>
  45. </label>
  46. </radio-group>
  47. </template>
  48. </view>
  49. </template>
  50. <script>
  51. /**
  52. * DataCheckbox 数据选择器
  53. * @description 通过数据渲染 checkbox 和 radio
  54. * @tutorial https://ext.dcloud.net.cn/plugin?id=3456
  55. * @property {String} mode = [default| list | button | tag] 显示模式
  56. * @value default 默认横排模式
  57. * @value list 列表模式
  58. * @value button 按钮模式
  59. * @value tag 标签模式
  60. * @property {Boolean} multiple = [true|false] 是否多选
  61. * @property {Array|String|Number} value 默认值
  62. * @property {Array} localdata 本地数据 ,格式 [{text:'',value:''}]
  63. * @property {Number|String} min 最小选择个数 ,multiple为true时生效
  64. * @property {Number|String} max 最大选择个数 ,multiple为true时生效
  65. * @property {Boolean} wrap 是否换行显示
  66. * @property {String} icon = [left|right] list 列表模式下icon显示位置
  67. * @property {Boolean} selectedColor 选中颜色
  68. * @property {Boolean} emptyText 没有数据时显示的文字 ,本地数据无效
  69. * @property {Boolean} selectedTextColor 选中文本颜色,如不填写则自动显示
  70. * @value left 左侧显示
  71. * @value right 右侧显示
  72. * @event {Function} change 选中发生变化触发
  73. */
  74. export default {
  75. name: 'uniDataCheckbox',
  76. mixins: [uniCloud.mixinDatacom || {}],
  77. props: {
  78. mode: {
  79. type: String,
  80. default: 'default'
  81. },
  82. multiple: {
  83. type: Boolean,
  84. default: false
  85. },
  86. value: {
  87. type: [Array, String, Number],
  88. default () {
  89. return ''
  90. }
  91. },
  92. localdata: {
  93. type: Array,
  94. default () {
  95. return []
  96. }
  97. },
  98. min: {
  99. type: [Number, String],
  100. default: ''
  101. },
  102. max: {
  103. type: [Number, String],
  104. default: ''
  105. },
  106. wrap: {
  107. type: Boolean,
  108. default: false
  109. },
  110. icon: {
  111. type: String,
  112. default: 'left'
  113. },
  114. selectedColor: {
  115. type: String,
  116. default: '#007aff'
  117. },
  118. selectedTextColor: {
  119. type: String,
  120. default: '#333'
  121. },
  122. emptyText:{
  123. type: String,
  124. default: '暂无数据'
  125. },
  126. disabled:{
  127. type: Boolean,
  128. default: false
  129. }
  130. },
  131. watch: {
  132. localdata: {
  133. handler(newVal) {
  134. this.range = newVal
  135. this.dataList = this.getDataList(this.getSelectedValue(newVal))
  136. },
  137. deep: true
  138. },
  139. mixinDatacomResData(newVal) {
  140. this.range = newVal
  141. this.dataList = this.getDataList(this.getSelectedValue(newVal))
  142. },
  143. value(newVal) {
  144. this.dataList = this.getDataList(newVal)
  145. this.formItem && this.formItem.setValue(newVal)
  146. }
  147. },
  148. data() {
  149. return {
  150. dataList: [],
  151. range: [],
  152. contentText: {
  153. contentdown: '查看更多',
  154. contentrefresh: '加载中',
  155. contentnomore: '没有更多'
  156. },
  157. isLocal:true,
  158. styles: {
  159. selectedColor: '#007aff',
  160. selectedTextColor: '#333',
  161. }
  162. };
  163. },
  164. created() {
  165. this.form = this.getForm('uniForms')
  166. this.formItem = this.getForm('uniFormsItem')
  167. this.formItem && this.formItem.setValue(this.value)
  168. if (this.formItem) {
  169. if (this.formItem.name) {
  170. this.rename = this.formItem.name
  171. this.form.inputChildrens.push(this)
  172. }
  173. }
  174. if (this.localdata && this.localdata.length !== 0) {
  175. this.isLocal = true
  176. this.range = this.localdata
  177. this.dataList = this.getDataList(this.getSelectedValue(this.range))
  178. } else {
  179. if (this.collection) {
  180. this.isLocal = false
  181. this.loadData()
  182. }
  183. }
  184. },
  185. methods: {
  186. loadData() {
  187. this.mixinDatacomGet().then(res=>{
  188. this.mixinDatacomResData = res.result.data
  189. if(this.mixinDatacomResData.length === 0){
  190. this.isLocal = false
  191. this.mixinDatacomErrorMessage = this.emptyText
  192. }else{
  193. this.isLocal = true
  194. }
  195. }).catch(err=>{
  196. this.mixinDatacomErrorMessage = err.message
  197. })
  198. },
  199. /**
  200. * 获取父元素实例
  201. */
  202. getForm(name = 'uniForms') {
  203. let parent = this.$parent;
  204. let parentName = parent.$options.name;
  205. while (parentName !== name) {
  206. parent = parent.$parent;
  207. if (!parent) return false
  208. parentName = parent.$options.name;
  209. }
  210. return parent;
  211. },
  212. chagne(e) {
  213. const values = e.detail.value
  214. let detail = {
  215. value: [],
  216. data: []
  217. }
  218. if (this.multiple) {
  219. this.range.forEach(item => {
  220. if (values.includes(item.value + '')) {
  221. detail.value.push(item.value)
  222. detail.data.push(item)
  223. }
  224. })
  225. } else {
  226. const range = this.range.find(item => (item.value + '') === values)
  227. if (range) {
  228. detail = {
  229. value: range.value,
  230. data: range
  231. }
  232. }
  233. }
  234. this.formItem && this.formItem.setValue(detail.value)
  235. this.$emit('input', detail.value)
  236. this.$emit('change', {
  237. detail
  238. })
  239. if (this.multiple) {
  240. // 如果 v-model 没有绑定 ,则走内部逻辑
  241. // if (this.value.length === 0) {
  242. this.dataList = this.getDataList(detail.value, true)
  243. // }
  244. } else {
  245. this.dataList = this.getDataList(detail.value)
  246. }
  247. },
  248. /**
  249. * 获取渲染的新数组
  250. * @param {Object} value 选中内容
  251. */
  252. getDataList(value) {
  253. // 解除引用关系,破坏原引用关系,避免污染源数据
  254. let dataList = JSON.parse(JSON.stringify(this.range))
  255. let list = []
  256. if (this.multiple) {
  257. if (!Array.isArray(value)) {
  258. value = []
  259. }
  260. }
  261. dataList.forEach((item, index) => {
  262. item.disabled = item.disable || item.disabled || false
  263. if (this.multiple) {
  264. if (value.length > 0) {
  265. let have = value.find(val => val === item.value)
  266. item.selected = have !== undefined
  267. } else {
  268. item.selected = false
  269. }
  270. } else {
  271. item.selected = value === item.value
  272. }
  273. list.push(item)
  274. })
  275. return this.setRange(list)
  276. },
  277. /**
  278. * 处理最大最小值
  279. * @param {Object} list
  280. */
  281. setRange(list) {
  282. let selectList = list.filter(item => item.selected)
  283. let min = Number(this.min) || 0
  284. let max = Number(this.max) || ''
  285. list.forEach((item, index) => {
  286. if (this.multiple) {
  287. if (selectList.length <= min) {
  288. let have = selectList.find(val => val.value === item.value)
  289. if (have !== undefined) {
  290. item.disabled = true
  291. }
  292. }
  293. if (selectList.length >= max && max !== '') {
  294. let have = selectList.find(val => val.value === item.value)
  295. if (have === undefined) {
  296. item.disabled = true
  297. }
  298. }
  299. }
  300. this.setStyles(item, index)
  301. list[index] = item
  302. })
  303. return list
  304. },
  305. /**
  306. * 设置 class
  307. * @param {Object} item
  308. * @param {Object} index
  309. */
  310. setStyles(item, index) {
  311. // 设置自定义样式
  312. item.styleBackgroud = this.setStyleBackgroud(item)
  313. item.styleIcon = this.setStyleIcon(item)
  314. item.styleIconText = this.setStyleIconText(item)
  315. item.styleRightIcon = this.setStyleRightIcon(item)
  316. },
  317. /**
  318. * 获取选中值
  319. * @param {Object} range
  320. */
  321. getSelectedValue(range) {
  322. if (!this.multiple) return this.value
  323. let selectedArr = []
  324. range.forEach((item) => {
  325. if (item.selected) {
  326. selectedArr.push(item.value)
  327. }
  328. })
  329. return this.value.length > 0 ? this.value : selectedArr
  330. },
  331. /**
  332. * 设置背景样式
  333. */
  334. setStyleBackgroud(item) {
  335. let styles = {}
  336. // if (item.selected) {
  337. if (this.mode !== 'list') {
  338. styles['border-color'] = item.selected?this.selectedColor:'#DCDFE6'
  339. }
  340. if (this.mode === 'tag') {
  341. styles['background-color'] = item.selected? this.selectedColor:'#f5f5f5'
  342. }
  343. // }
  344. let classles = ''
  345. for (let i in styles) {
  346. classles += `${i}:${styles[i]};`
  347. }
  348. return classles
  349. },
  350. setStyleIcon(item) {
  351. let styles = {}
  352. let classles = ''
  353. // if (item.selected) {
  354. styles['background-color'] = item.selected?this.selectedColor:'#fff'
  355. styles['border-color'] = item.selected?this.selectedColor:'#DCDFE6'
  356. if(!item.selected && item.disabled){
  357. styles['background-color'] = '#F2F6FC'
  358. styles['border-color'] = item.selected?this.selectedColor:'#DCDFE6'
  359. }
  360. for (let i in styles) {
  361. classles += `${i}:${styles[i]};`
  362. }
  363. // }
  364. return classles
  365. },
  366. setStyleIconText(item) {
  367. let styles = {}
  368. let classles = ''
  369. // if (item.selected) {
  370. // if (this.selectedTextColor) {
  371. // styles.color = item.selected?this.selectedTextColor:'#999'
  372. // } else {
  373. if (this.mode === 'tag') {
  374. styles.color = item.selected?'#fff':'#333'
  375. } else {
  376. styles.color = item.selected?this.selectedColor:'#333'
  377. }
  378. if(!item.selected && item.disabled){
  379. styles.color = '#999'
  380. }
  381. // }
  382. for (let i in styles) {
  383. classles += `${i}:${styles[i]};`
  384. }
  385. // }
  386. return classles
  387. },
  388. setStyleRightIcon(item) {
  389. let styles = {}
  390. let classles = ''
  391. // if (item.selected) {
  392. if (this.mode === 'list') {
  393. styles['border-color'] = item.selected?this.styles.selectedColor:'#DCDFE6'
  394. }
  395. for (let i in styles) {
  396. classles += `${i}:${styles[i]};`
  397. }
  398. // }
  399. return classles
  400. }
  401. // setColor(){
  402. // return
  403. // }
  404. }
  405. }
  406. </script>
  407. <style lang="scss">
  408. $checked-color: #007aff;
  409. $border-color: #DCDFE6;
  410. $disable:0.4;
  411. @mixin flex {
  412. /* #ifndef APP-NVUE */
  413. display: flex;
  414. /* #endif */
  415. }
  416. .uni-data-loading {
  417. @include flex;
  418. flex-direction: row;
  419. justify-content: center;
  420. align-items: center;
  421. height: 36px;
  422. padding-left: 10px;
  423. color: #999;
  424. }
  425. .uni-data-checklist {
  426. position: relative;
  427. z-index: 0;
  428. // 多选样式
  429. .checklist-group {
  430. @include flex;
  431. flex-direction: row;
  432. flex-wrap: wrap;
  433. justify-content: center;
  434. &.is-list {
  435. flex-direction: column;
  436. }
  437. .checklist-box {
  438. @include flex;
  439. flex-direction: row;
  440. align-items: center;
  441. position: relative;
  442. margin: 5px 0;
  443. margin-right: 25px;
  444. width: 268rpx;
  445. .hidden {
  446. position: absolute;
  447. // transform: scale(0);
  448. opacity: 0;
  449. }
  450. // 文字样式
  451. .checklist-content {
  452. @include flex;
  453. flex: 1;
  454. flex-direction: row;
  455. align-items: center;
  456. justify-content: space-between;
  457. .checklist-text {
  458. font-size: 14px;
  459. color: #333;
  460. margin-left: 5px;
  461. line-height: 14px;
  462. width: 100%;
  463. text-align: center;
  464. }
  465. // .list-content {
  466. // margin-left: 15px;
  467. // }
  468. .checkobx__list {
  469. border: 1px solid #fff;
  470. border-left: 0;
  471. border-top: 0;
  472. height: 12px;
  473. width: 6px;
  474. transform-origin: center;
  475. transform: rotate(45deg);
  476. opacity: 0;
  477. }
  478. }
  479. // 多选样式
  480. .checkbox__inner {
  481. /* #ifndef APP-NVUE */
  482. flex-shrink: 0;
  483. box-sizing: border-box;
  484. /* #endif */
  485. position: relative;
  486. width: 16px;
  487. height: 16px;
  488. border: 1px solid $border-color;
  489. border-radius: 2px;
  490. background-color: #fff;
  491. z-index: 1;
  492. .checkbox__inner-icon {
  493. position: absolute;
  494. /* #ifdef APP-NVUE */
  495. top: 2px;
  496. /* #endif */
  497. /* #ifndef APP-NVUE */
  498. top: 1px;
  499. /* #endif */
  500. left: 5px;
  501. height: 8px;
  502. width: 4px;
  503. border: 1px solid #fff;
  504. border-left: 0;
  505. border-top: 0;
  506. opacity: 0;
  507. transform-origin: center;
  508. transform: rotate(40deg);
  509. }
  510. }
  511. // 单选样式
  512. .radio__inner {
  513. @include flex;
  514. /* #ifndef APP-NVUE */
  515. flex-shrink: 0;
  516. box-sizing: border-box;
  517. /* #endif */
  518. justify-content: center;
  519. align-items: center;
  520. position: relative;
  521. width: 16px;
  522. height: 16px;
  523. border: 1px solid $border-color;
  524. border-radius: 16px;
  525. background-color: #fff;
  526. z-index: 1;
  527. .radio__inner-icon {
  528. width: 8px;
  529. height: 8px;
  530. border-radius: 10px;
  531. opacity: 0;
  532. }
  533. }
  534. // 默认样式
  535. &.is--default {
  536. // 禁用
  537. &.is-disable {
  538. /* #ifdef H5 */
  539. cursor: not-allowed;
  540. /* #endif */
  541. .checkbox__inner {
  542. background-color: #F2F6FC;
  543. border-color: $border-color;
  544. /* #ifdef H5 */
  545. cursor: not-allowed;
  546. /* #endif */
  547. }
  548. .radio__inner {
  549. background-color: #F2F6FC;
  550. border-color: $border-color;
  551. }
  552. .checklist-text {
  553. color: #999;
  554. }
  555. }
  556. // 选中
  557. &.is-checked {
  558. .checkbox__inner {
  559. border-color: $checked-color;
  560. background-color: $checked-color;
  561. .checkbox__inner-icon {
  562. opacity: 1;
  563. transform: rotate(45deg);
  564. }
  565. }
  566. .radio__inner {
  567. border-color: $checked-color;
  568. .radio__inner-icon {
  569. opacity: 1;
  570. background-color: $checked-color;
  571. }
  572. }
  573. .checklist-text {
  574. color: $checked-color;
  575. }
  576. // 选中禁用
  577. &.is-disable {
  578. .checkbox__inner {
  579. opacity: $disable;
  580. }
  581. .checklist-text {
  582. opacity: $disable;
  583. }
  584. }
  585. }
  586. }
  587. // 按钮样式
  588. &.is--button {
  589. margin-right: 10px;
  590. padding: 5px 15px;
  591. border: 1px $border-color solid;
  592. border-radius: 3px;
  593. transition: border-color 0.2s;
  594. // 禁用
  595. &.is-disable {
  596. /* #ifdef H5 */
  597. cursor: not-allowed;
  598. /* #endif */
  599. border: 1px #eee solid;
  600. opacity: $disable;
  601. .checkbox__inner {
  602. background-color: #F2F6FC;
  603. border-color: $border-color;
  604. /* #ifdef H5 */
  605. cursor: not-allowed;
  606. /* #endif */
  607. }
  608. .radio__inner {
  609. background-color: #F2F6FC;
  610. border-color: $border-color;
  611. /* #ifdef H5 */
  612. cursor: not-allowed;
  613. /* #endif */
  614. }
  615. .checklist-text {
  616. color: #999;
  617. }
  618. }
  619. &.is-checked {
  620. border-color: $checked-color;
  621. .checkbox__inner {
  622. border-color: $checked-color;
  623. background-color: $checked-color;
  624. .checkbox__inner-icon {
  625. opacity: 1;
  626. transform: rotate(45deg);
  627. }
  628. }
  629. .radio__inner {
  630. border-color: $checked-color;
  631. .radio__inner-icon {
  632. opacity: 1;
  633. background-color: $checked-color;
  634. }
  635. }
  636. .checklist-text {
  637. color: $checked-color;
  638. }
  639. // 选中禁用
  640. &.is-disable {
  641. opacity: $disable;
  642. }
  643. }
  644. }
  645. // 标签样式
  646. &.is--tag {
  647. margin-right: 10px;
  648. padding: 5px 10px;
  649. border: 1px $border-color solid;
  650. border-radius: 3px;
  651. background-color: #f5f5f5;
  652. .checklist-text {
  653. margin: 0;
  654. color: #333;
  655. }
  656. // 禁用
  657. &.is-disable {
  658. /* #ifdef H5 */
  659. cursor: not-allowed;
  660. /* #endif */
  661. opacity: $disable;
  662. }
  663. &.is-checked {
  664. background-color: $checked-color;
  665. border-color: $checked-color;
  666. .checklist-text {
  667. color: #fff;
  668. }
  669. }
  670. }
  671. &.is--list {
  672. /* #ifndef APP-NVUE */
  673. display: flex;
  674. /* #endif */
  675. padding: 10px 15px;
  676. padding-left: 0;
  677. margin: 0;
  678. &.is-list-border {
  679. border-top: 1px #eee solid;
  680. }
  681. // 禁用
  682. &.is-disable {
  683. /* #ifdef H5 */
  684. cursor: not-allowed;
  685. /* #endif */
  686. .checkbox__inner {
  687. background-color: #F2F6FC;
  688. border-color: $border-color;
  689. /* #ifdef H5 */
  690. cursor: not-allowed;
  691. /* #endif */
  692. }
  693. .checklist-text {
  694. color: #999;
  695. }
  696. }
  697. &.is-checked {
  698. .checkbox__inner {
  699. border-color: $checked-color;
  700. background-color: $checked-color;
  701. .checkbox__inner-icon {
  702. opacity: 1;
  703. transform: rotate(45deg);
  704. }
  705. }
  706. .checklist-text {
  707. color: $checked-color;
  708. }
  709. .checklist-content {
  710. .checkobx__list {
  711. opacity: 1;
  712. border-color: $checked-color;
  713. }
  714. }
  715. // 选中禁用
  716. &.is-disable {
  717. .checkbox__inner {
  718. opacity: $disable;
  719. }
  720. .checklist-text {
  721. opacity: $disable;
  722. }
  723. }
  724. }
  725. }
  726. }
  727. }
  728. }
  729. </style>