PostCategorySelector.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <template>
  2. <div class="PostCategorySelector row q-col-gutter-md">
  3. <div class="col-md-4 col-12">
  4. <div>
  5. دسته بندی اصلی
  6. </div>
  7. <q-select v-model="mainCategory"
  8. :options="mainCategoryOptions"
  9. :loading="postCategories.laoding" />
  10. </div>
  11. <div class="col-md-4 col-12">
  12. <div>
  13. دسته بندی جزیی
  14. </div>
  15. <q-select v-model="subCategory"
  16. :options="subCategoryOptions"
  17. :loading="postCategories.laoding" />
  18. </div>
  19. <div class="col-md-4 col-12">
  20. <div>
  21. بخش
  22. </div>
  23. <q-select v-model="bakhshCategory"
  24. :options="bakhshCategoryOptions"
  25. :loading="postCategories.laoding" />
  26. </div>
  27. </div>
  28. </template>
  29. <script>
  30. import { APIGateway } from 'src/api/APIGateway.js'
  31. import { PostCategoryList } from 'src/models/PostCategory.js'
  32. export default {
  33. name: 'PostCategorySelector',
  34. props: {
  35. value: {
  36. default: () => [],
  37. type: [Array, String, Number, Boolean, Object]
  38. },
  39. disable: {
  40. default: false,
  41. type: Boolean
  42. }
  43. },
  44. emits: ['update:value'],
  45. data () {
  46. return {
  47. mainCategory: null,
  48. subCategory: null,
  49. bakhshCategory: null,
  50. mainCategoryOptions: [],
  51. subCategoryOptions: [],
  52. bakhshCategoryOptions: [],
  53. postCategories: new PostCategoryList()
  54. }
  55. },
  56. watch: {
  57. value () {
  58. if (this.value?.id) {
  59. this.getCategories()
  60. } else if (this.value) {
  61. this.inputData = this.value
  62. }
  63. },
  64. mainCategory (newValue) {
  65. this.subCategory = null
  66. this.subCategoryOptions = []
  67. if (!newValue.item.children) {
  68. this.$emit('update:value', newValue.value)
  69. return
  70. }
  71. this.subCategoryOptions = newValue.item.children.map(item => {
  72. return {
  73. item,
  74. label: item.title,
  75. value: item.id
  76. }
  77. })
  78. },
  79. subCategory (newValue) {
  80. this.bakhshCategory = null
  81. this.bakhshCategoryOptions = []
  82. if (!newValue?.item?.children) {
  83. this.$emit('update:value', null)
  84. return
  85. }
  86. this.bakhshCategoryOptions = newValue.item.children.map(item => {
  87. return {
  88. item,
  89. label: item.title,
  90. value: item.id
  91. }
  92. })
  93. },
  94. bakhshCategory (newValue) {
  95. const categoryId = newValue ? newValue.value : null
  96. this.$emit('update:value', categoryId)
  97. }
  98. },
  99. mounted() {
  100. this.getCategories()
  101. },
  102. methods: {
  103. setMainCategory (category) {
  104. if (!category?.parent?.parent?.id) {
  105. return
  106. }
  107. this.mainCategory = this.mainCategoryOptions.find(item => item.value === category.parent.parent.id)
  108. },
  109. setSubCategory (category) {
  110. if (!category?.parent?.id) {
  111. return
  112. }
  113. this.subCategory = this.subCategoryOptions.find(item => item.value === category.parent.id)
  114. },
  115. setBakhshCategory (category) {
  116. if (!category?.id) {
  117. return
  118. }
  119. this.bakhshCategory = this.bakhshCategoryOptions.find(item => item.value === category.id)
  120. },
  121. getCategories () {
  122. this.postCategories.laoding = true
  123. const args = { parent__isnull: 'true', per_page: 99999 }
  124. const apiGateway = APIGateway.postCategory.index(args)
  125. apiGateway
  126. .then(postCategories => {
  127. this.postCategories = new PostCategoryList(postCategories.list)
  128. this.postCategories.laoding = false
  129. this.mainCategoryOptions = this.postCategories.list.map(item => {
  130. return {
  131. item,
  132. label: item.title,
  133. value: item.id
  134. }
  135. })
  136. if (this.value?.parent?.parent?.id && this.value?.id) {
  137. const category = this.value
  138. this.$nextTick(() => {
  139. this.setMainCategory(category)
  140. this.$nextTick(() => {
  141. this.setSubCategory(category)
  142. this.$nextTick(() => {
  143. this.setBakhshCategory(category)
  144. this.$emit('update:value', category.id)
  145. })
  146. })
  147. })
  148. }
  149. })
  150. .catch(() => {
  151. this.postCategories.laoding = false
  152. })
  153. }
  154. }
  155. }
  156. </script>
  157. <style lang="scss" scoped>
  158. .PostCategorySelector {
  159. }
  160. </style>