uni-data-checkbox.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  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. &.is-list {
  434. flex-direction: column;
  435. }
  436. .checklist-box {
  437. @include flex;
  438. flex-direction: row;
  439. align-items: center;
  440. position: relative;
  441. margin: 5px 0;
  442. margin-right: 25px;
  443. .hidden {
  444. position: absolute;
  445. // transform: scale(0);
  446. opacity: 0;
  447. }
  448. // 文字样式
  449. .checklist-content {
  450. @include flex;
  451. flex: 1;
  452. flex-direction: row;
  453. align-items: center;
  454. justify-content: space-between;
  455. .checklist-text {
  456. font-size: 14px;
  457. color: #333;
  458. margin-left: 5px;
  459. line-height: 14px;
  460. }
  461. // .list-content {
  462. // margin-left: 15px;
  463. // }
  464. .checkobx__list {
  465. border: 1px solid #fff;
  466. border-left: 0;
  467. border-top: 0;
  468. height: 12px;
  469. width: 6px;
  470. transform-origin: center;
  471. transform: rotate(45deg);
  472. opacity: 0;
  473. }
  474. }
  475. // 多选样式
  476. .checkbox__inner {
  477. /* #ifndef APP-NVUE */
  478. flex-shrink: 0;
  479. box-sizing: border-box;
  480. /* #endif */
  481. position: relative;
  482. width: 16px;
  483. height: 16px;
  484. border: 1px solid $border-color;
  485. border-radius: 2px;
  486. background-color: #fff;
  487. z-index: 1;
  488. .checkbox__inner-icon {
  489. position: absolute;
  490. /* #ifdef APP-NVUE */
  491. top: 2px;
  492. /* #endif */
  493. /* #ifndef APP-NVUE */
  494. top: 1px;
  495. /* #endif */
  496. left: 5px;
  497. height: 8px;
  498. width: 4px;
  499. border: 1px solid #fff;
  500. border-left: 0;
  501. border-top: 0;
  502. opacity: 0;
  503. transform-origin: center;
  504. transform: rotate(40deg);
  505. }
  506. }
  507. // 单选样式
  508. .radio__inner {
  509. @include flex;
  510. /* #ifndef APP-NVUE */
  511. flex-shrink: 0;
  512. box-sizing: border-box;
  513. /* #endif */
  514. justify-content: center;
  515. align-items: center;
  516. position: relative;
  517. width: 16px;
  518. height: 16px;
  519. border: 1px solid $border-color;
  520. border-radius: 16px;
  521. background-color: #fff;
  522. z-index: 1;
  523. .radio__inner-icon {
  524. width: 8px;
  525. height: 8px;
  526. border-radius: 10px;
  527. opacity: 0;
  528. }
  529. }
  530. // 默认样式
  531. &.is--default {
  532. // 禁用
  533. &.is-disable {
  534. /* #ifdef H5 */
  535. cursor: not-allowed;
  536. /* #endif */
  537. .checkbox__inner {
  538. background-color: #F2F6FC;
  539. border-color: $border-color;
  540. /* #ifdef H5 */
  541. cursor: not-allowed;
  542. /* #endif */
  543. }
  544. .radio__inner {
  545. background-color: #F2F6FC;
  546. border-color: $border-color;
  547. }
  548. .checklist-text {
  549. color: #999;
  550. }
  551. }
  552. // 选中
  553. &.is-checked {
  554. .checkbox__inner {
  555. border-color: $checked-color;
  556. background-color: $checked-color;
  557. .checkbox__inner-icon {
  558. opacity: 1;
  559. transform: rotate(45deg);
  560. }
  561. }
  562. .radio__inner {
  563. border-color: $checked-color;
  564. .radio__inner-icon {
  565. opacity: 1;
  566. background-color: $checked-color;
  567. }
  568. }
  569. .checklist-text {
  570. color: $checked-color;
  571. }
  572. // 选中禁用
  573. &.is-disable {
  574. .checkbox__inner {
  575. opacity: $disable;
  576. }
  577. .checklist-text {
  578. opacity: $disable;
  579. }
  580. }
  581. }
  582. }
  583. // 按钮样式
  584. &.is--button {
  585. margin-right: 10px;
  586. padding: 5px 15px;
  587. border: 1px $border-color solid;
  588. border-radius: 3px;
  589. transition: border-color 0.2s;
  590. // 禁用
  591. &.is-disable {
  592. /* #ifdef H5 */
  593. cursor: not-allowed;
  594. /* #endif */
  595. border: 1px #eee solid;
  596. opacity: $disable;
  597. .checkbox__inner {
  598. background-color: #F2F6FC;
  599. border-color: $border-color;
  600. /* #ifdef H5 */
  601. cursor: not-allowed;
  602. /* #endif */
  603. }
  604. .radio__inner {
  605. background-color: #F2F6FC;
  606. border-color: $border-color;
  607. /* #ifdef H5 */
  608. cursor: not-allowed;
  609. /* #endif */
  610. }
  611. .checklist-text {
  612. color: #999;
  613. }
  614. }
  615. &.is-checked {
  616. border-color: $checked-color;
  617. .checkbox__inner {
  618. border-color: $checked-color;
  619. background-color: $checked-color;
  620. .checkbox__inner-icon {
  621. opacity: 1;
  622. transform: rotate(45deg);
  623. }
  624. }
  625. .radio__inner {
  626. border-color: $checked-color;
  627. .radio__inner-icon {
  628. opacity: 1;
  629. background-color: $checked-color;
  630. }
  631. }
  632. .checklist-text {
  633. color: $checked-color;
  634. }
  635. // 选中禁用
  636. &.is-disable {
  637. opacity: $disable;
  638. }
  639. }
  640. }
  641. // 标签样式
  642. &.is--tag {
  643. margin-right: 10px;
  644. padding: 5px 10px;
  645. border: 1px $border-color solid;
  646. border-radius: 3px;
  647. background-color: #f5f5f5;
  648. .checklist-text {
  649. margin: 0;
  650. color: #333;
  651. }
  652. // 禁用
  653. &.is-disable {
  654. /* #ifdef H5 */
  655. cursor: not-allowed;
  656. /* #endif */
  657. opacity: $disable;
  658. }
  659. &.is-checked {
  660. background-color: $checked-color;
  661. border-color: $checked-color;
  662. .checklist-text {
  663. color: #fff;
  664. }
  665. }
  666. }
  667. &.is--list {
  668. /* #ifndef APP-NVUE */
  669. display: flex;
  670. /* #endif */
  671. padding: 10px 15px;
  672. padding-left: 0;
  673. margin: 0;
  674. &.is-list-border {
  675. border-top: 1px #eee solid;
  676. }
  677. // 禁用
  678. &.is-disable {
  679. /* #ifdef H5 */
  680. cursor: not-allowed;
  681. /* #endif */
  682. .checkbox__inner {
  683. background-color: #F2F6FC;
  684. border-color: $border-color;
  685. /* #ifdef H5 */
  686. cursor: not-allowed;
  687. /* #endif */
  688. }
  689. .checklist-text {
  690. color: #999;
  691. }
  692. }
  693. &.is-checked {
  694. .checkbox__inner {
  695. border-color: $checked-color;
  696. background-color: $checked-color;
  697. .checkbox__inner-icon {
  698. opacity: 1;
  699. transform: rotate(45deg);
  700. }
  701. }
  702. .checklist-text {
  703. color: $checked-color;
  704. }
  705. .checklist-content {
  706. .checkobx__list {
  707. opacity: 1;
  708. border-color: $checked-color;
  709. }
  710. }
  711. // 选中禁用
  712. &.is-disable {
  713. .checkbox__inner {
  714. opacity: $disable;
  715. }
  716. .checklist-text {
  717. opacity: $disable;
  718. }
  719. }
  720. }
  721. }
  722. }
  723. }
  724. }
  725. </style>