choice-list-pc.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // var choiceListComponentTemplate = `
  2. // <div class="selector-content choice-list-content">
  3. // <div class="select-group-container">
  4. // <el-checkbox
  5. // v-for="(item, index) in selectList"
  6. // :key="index"
  7. // :name="item.value"
  8. // v-model="item.selected"
  9. // @change="changeState(item)"
  10. // >{{ item.label }}</el-checkbox>
  11. // </div>
  12. // </div>
  13. // `
  14. var choiceListComponentTemplate = "\n<div class=\"selector-content choice-list-content\">\n <div class=\"select-group-container\">\n <el-checkbox\n v-for=\"(item, index) in selectList\"\n :key=\"index\"\n :name=\"item.value\"\n v-model=\"item.selected\"\n @change=\"changeState(item)\"\n >{{ item.label }}</el-checkbox>\n </div>\n</div>\n";
  15. var choiceListComponent = {
  16. name: 'choice-list-pc',
  17. template: choiceListComponentTemplate,
  18. props: {
  19. multi: {
  20. type: Boolean,
  21. default: true
  22. },
  23. list: {
  24. type: Array,
  25. default: function () {
  26. return [
  27. // {
  28. // label: '',
  29. // value: ''
  30. // }
  31. ]
  32. }
  33. }
  34. },
  35. data: function () {
  36. return {
  37. selectList: [],
  38. selectedList: []
  39. }
  40. },
  41. created: function () {
  42. this.initData()
  43. },
  44. methods: {
  45. initData: function () {
  46. var arr = []
  47. this.list.forEach(function (item, index) {
  48. var i = JSON.parse(JSON.stringify(item))
  49. i.selected = false
  50. arr.push(i)
  51. })
  52. this.selectList = arr
  53. },
  54. setState: function (arr) {
  55. this.selectList.forEach(function (item) {
  56. if (arr.indexOf(item.label) !== -1 || arr.indexOf(item.value) !== -1) {
  57. item.selected = true
  58. } else {
  59. item.selected = false
  60. }
  61. })
  62. this.selectedList = this.getState()
  63. },
  64. getState: function () {
  65. var arr = []
  66. this.selectList.forEach(function (item) {
  67. if (item.selected) {
  68. arr.push(item.value)
  69. }
  70. })
  71. return arr
  72. },
  73. onChange: function () {
  74. this.selectedList = this.getState()
  75. this.$emit('change', this.selectedList)
  76. },
  77. changeState: function (item) {
  78. if (!this.multi) {
  79. this.selectList.forEach(function (c) {
  80. c.selected = false
  81. })
  82. item.selected = !item.selected
  83. }
  84. this.onChange()
  85. }
  86. }
  87. }