chooseTypeModal.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <template>
  2. <a-modal
  3. centered
  4. class="chooseType-modal"
  5. :footer="null"
  6. :maskClosable="false"
  7. title="选择产品分类"
  8. v-model="isShow"
  9. @cancel="isShow=false"
  10. width="50%">
  11. <div class="chooseType-con">
  12. <div>
  13. <a-checkbox :checked="checkAll" :disabled="!linkageStatus&&chooseList.length>0 " @change="onCheckAllChange">全选</a-checkbox>
  14. </div>
  15. <a-divider style="margin:10px 0;" />
  16. <a-tree
  17. style="height: 400px;overflow-y: scroll;"
  18. checkable
  19. @check="onCheck"
  20. :checkStrictly="linkageStatus"
  21. v-model="checkedKeys"
  22. :treeData="productTypeList"
  23. :replaceFields="replaceFields"
  24. />
  25. <!-- 按钮 -->
  26. <div class="btn-con">
  27. <a-button
  28. id="chooseType-cancel"
  29. class="button-cancel"
  30. @click="isShow=false"
  31. style="padding: 0 30px;">取消</a-button>
  32. <a-button
  33. type="primary"
  34. id="chooseType-submit"
  35. @click="handleSave"
  36. style="padding: 0 30px;margin-left: 15px;">保存</a-button>
  37. </div>
  38. </div>
  39. </a-modal>
  40. </template>
  41. <script>
  42. import { productTypeQuery, productTypeQueryAll } from '@/api/productType'
  43. export default {
  44. name: 'ChooseTypeModal',
  45. props: {
  46. openModal: { // 弹框显示状态
  47. type: Boolean,
  48. default: false
  49. },
  50. chooseData: {
  51. type: Array,
  52. default: () => {
  53. return []
  54. }
  55. },
  56. type: { // 类型,促销规则promo
  57. type: String,
  58. default: ''
  59. },
  60. linkageStatus: { // 父子是否联动
  61. type: Boolean,
  62. default: true
  63. }
  64. },
  65. data () {
  66. return {
  67. isShow: this.openModal, // 是否打开弹框
  68. checkedKeys: [], // 选中项
  69. checkedRows: [], // 选中项 整条数据
  70. checkAll: false, // 全选
  71. indeterminate: true,
  72. replaceFields: {
  73. children: 'children',
  74. title: 'productTypeName',
  75. key: 'productTypeSn'
  76. },
  77. productTypeList: [],
  78. chooseList: []
  79. }
  80. },
  81. methods: {
  82. // 产品分类 列表
  83. getProductType () {
  84. if (this.type == 'promo') {
  85. productTypeQueryAll({}).then(res => {
  86. if (res.status == 200) {
  87. this.productTypeList = res.data
  88. } else {
  89. this.productTypeList = []
  90. }
  91. })
  92. } else {
  93. productTypeQuery({}).then(res => {
  94. if (res.status == 200) {
  95. if (!this.linkageStatus && this.chooseData.length > 0) {
  96. this.handleDisabled(res.data, this.chooseData)
  97. }
  98. if (!this.linkageStatus && this.chooseList.length > 0) {
  99. this.handleDisabled(res.data, this.chooseList)
  100. }
  101. this.productTypeList = res.data
  102. } else {
  103. this.productTypeList = []
  104. }
  105. })
  106. }
  107. },
  108. // 保存
  109. handleSave () {
  110. if (this.checkedKeys.checked && this.checkedKeys.checked.length < 1) {
  111. this.$message.warning('请在列表勾选后再进行操作!')
  112. return
  113. }
  114. this.$emit('ok', this.checkedRows)
  115. this.isShow = false
  116. },
  117. // 全选
  118. onCheckAllChange (e) {
  119. this.checkAll = e.target.checked
  120. if (e.target.checked) {
  121. this.checkedKeys = []
  122. this.checkedRows = []
  123. this.recursionFun(this.productTypeList)
  124. } else {
  125. this.checkedKeys = []
  126. this.checkedRows = []
  127. }
  128. },
  129. // 递归函数
  130. recursionFun (data) {
  131. if (data) {
  132. data.map((item, index) => {
  133. this.checkedKeys.push(item.productTypeSn)
  134. this.checkedRows.push(item)
  135. if (item.children && item.children.length > 0) {
  136. this.recursionFun(item.children)
  137. }
  138. })
  139. }
  140. },
  141. handleAllDisabled (list) {
  142. this.chooseList = list
  143. },
  144. // 禁用递归
  145. handleDisabled (data, selectData) {
  146. if (data) {
  147. data.map(item => {
  148. const posNum = selectData.findIndex(con => con.goodsSn == item.productTypeSn)
  149. if (posNum != -1) {
  150. item = Object.assign(item, selectData[posNum])
  151. }
  152. if (item.children && item.children.length > 0) {
  153. this.handleDisabled(item.children, selectData)
  154. }
  155. })
  156. }
  157. },
  158. onCheck (checkedKeys, info) {
  159. this.checkedRows = []
  160. info.checkedNodes.map((item, index) => {
  161. this.checkedRows.push(item.data.props)
  162. })
  163. }
  164. },
  165. watch: {
  166. // 父页面传过来的弹框状态
  167. openModal (newValue, oldValue) {
  168. this.isShow = newValue
  169. },
  170. // 重定义的弹框状态
  171. isShow (newValue, oldValue) {
  172. if (!newValue) {
  173. this.$emit('close')
  174. this.checkAll = false
  175. } else {
  176. this.getProductType()
  177. const _this = this
  178. this.checkedRows = this.chooseData
  179. this.checkedKeys = []
  180. this.chooseData.forEach(item => {
  181. _this.checkedKeys.push(item.goodsSn)
  182. })
  183. }
  184. }
  185. }
  186. }
  187. </script>
  188. <style lang="less" scoped>
  189. .chooseType-modal{
  190. .chooseType-con{
  191. .btn-con{
  192. text-align: center;
  193. margin: 30px 0 20px;
  194. .button-cancel{
  195. font-size: 12px;
  196. }
  197. }
  198. }
  199. }
  200. </style>