editModal.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. <template>
  2. <a-modal
  3. centered
  4. class="marketingDivisionSetEdit-modal"
  5. :footer="null"
  6. :maskClosable="false"
  7. :title="modalTit"
  8. v-model="isShow"
  9. @cancle="isShow=false"
  10. :width="800">
  11. <!-- 表单 -->
  12. <div>
  13. <a-spin :spinning="spinning" tip="Loading...">
  14. <a-form-model
  15. id="marketingDivisionSetEdit-form"
  16. ref="ruleForm"
  17. :model="form"
  18. :rules="rules"
  19. :label-col="formItemLayout.labelCol"
  20. :wrapper-col="formItemLayout.wrapperCol"
  21. >
  22. <a-form-model-item label="分区名称" prop="subareaName">
  23. <a-input
  24. id="marketingDivisionSetEdit-subareaName"
  25. :maxLength="30"
  26. v-model="form.subareaName"
  27. @change="filterEmpty"
  28. placeholder="请输入分区名称(最多30个字符)"
  29. allowClear />
  30. </a-form-model-item>
  31. <a-form-model-item label="省份/市">
  32. <div class="tree-node" id="treeNode">
  33. <div class="tree-box" v-for="item in addrProvinceList" :key="item.id">
  34. <div class="tree-parent">
  35. <span>
  36. <a-checkbox style="width: 100%;" :indeterminate="item.indeterminate" v-model="item.checked" @change="onChange(item,$event)">
  37. {{ item.name }}
  38. <!-- <i style="font-size: 12px;color: #999;font-style: normal;">(已选 {{ item.children | hasCheckChild }} 个市区)</i> -->
  39. </a-checkbox>
  40. </span>
  41. <span @click="expend(item)"><a-icon :title="item.isOpen==0?'展开':'收起'" :type="item.isOpen==0?'plus-square':'minus-square'" /></span>
  42. </div>
  43. <!-- 子级 -->
  44. <div v-show="item.isOpen==1" class="tree-child">
  45. <div v-for="child in item.children" :key="child.id">
  46. <a-checkbox v-model="child.checked" style="width: 100%;" @change="onChange(child,$event,item)">
  47. {{ child.name }}
  48. </a-checkbox>
  49. </div>
  50. </div>
  51. </div>
  52. </div>
  53. </a-form-model-item>
  54. </a-form-model>
  55. <div class="btn-cont">
  56. <a-button type="primary" id="marketingDivisionSetEdit-modal-save" @click="handleSave">保存</a-button>
  57. <a-button id="marketingDivisionSetEdit-modal-back" @click="isShow = false" style="margin-left: 15px;">返回列表</a-button>
  58. </div>
  59. </a-spin>
  60. </div>
  61. </a-modal>
  62. </template>
  63. <script>
  64. import { STable, Upload } from '@/components'
  65. import Editor from '@/components/WEeditor'
  66. import { getArea } from '@/api/data'
  67. import { subareaSaveSubareaArea, subareaFindBySn } from '@/api/subarea'
  68. export default {
  69. name: 'MarketingDivisionSetEditModal',
  70. components: { STable, Upload, Editor },
  71. props: {
  72. openModal: { // 弹框显示状态
  73. type: Boolean,
  74. default: false
  75. },
  76. itemId: {
  77. type: [Number, String],
  78. default: ''
  79. },
  80. nowData: {
  81. type: Object,
  82. default: null
  83. }
  84. },
  85. computed: {
  86. modalTit () {
  87. return (this.itemId ? '编辑' : '新增') + '分区'
  88. }
  89. },
  90. filters: {
  91. hasCheckChild: function (value) {
  92. return value.filter(item => item.checked).length
  93. }
  94. },
  95. data () {
  96. return {
  97. isShow: this.openModal, // 是否打开弹框
  98. spinning: false,
  99. formItemLayout: {
  100. labelCol: { span: 4 },
  101. wrapperCol: { span: 18 }
  102. },
  103. form: {
  104. subareaName: '' // 分区名称
  105. },
  106. rules: {
  107. subareaName: [
  108. { required: true, message: '请输入分区名称', trigger: 'blur' }
  109. ]
  110. },
  111. addrProvinceList: [], // 省下拉
  112. subareaAreaList: []
  113. }
  114. },
  115. methods: {
  116. filterEmpty () {
  117. let str = this.form.subareaName
  118. str = str.replace(/\ +/g, '')
  119. str = str.replace(/[ ]/g, '')
  120. str = str.replace(/[\r\n]/g, '')
  121. this.form.subareaName = str
  122. },
  123. onChange (item, e, parent) {
  124. // console.log(item, `checked = ${e.target.checked}`)
  125. item.checked = e.target.checked
  126. item.noSave = false
  127. // 父级
  128. if (item.type == 2) {
  129. item.children && item.children.map(d => {
  130. d.checked = item.checked
  131. })
  132. item.indeterminate = false
  133. }
  134. // 子级
  135. if (item.type == 3) {
  136. // 父级节点
  137. const hasCheckd = parent.children.filter(k => k.checked)
  138. // 是否全选
  139. parent.checked = hasCheckd.length == parent.children.length
  140. parent.children.map(d => d.noSave = parent.checked)
  141. // indeterminate为true,则为全选,false则为部分选择或者空
  142. parent.indeterminate = ((hasCheckd.length < parent.children.length) && (hasCheckd.length != 0))
  143. }
  144. this.addrProvinceList = JSON.parse(JSON.stringify(this.addrProvinceList))
  145. },
  146. // 展开收起
  147. expend (item) {
  148. if (item.children && item.children.length == 0) {
  149. item.isOpen = 1
  150. this.getArea('city', item, 1)
  151. } else {
  152. item.isOpen = !item.isOpen
  153. }
  154. },
  155. // 省/市/区
  156. getArea (leve, item, openType) {
  157. let params
  158. if (leve == 'province') {
  159. params = { type: '2' }
  160. } else {
  161. params = { parentId: item.areaSn, type: leve == 'city' ? '3' : '4' }
  162. }
  163. getArea(params).then(res => {
  164. if (res.status == 200) {
  165. if (leve == 'province') {
  166. this.addrProvinceList = res.data || []
  167. this.addrProvinceList.map(item => {
  168. item.noSave = true
  169. item.checked = false
  170. item.children = []
  171. item.indeterminate = false
  172. })
  173. // 编辑时获取已选数据
  174. if (this.itemId) {
  175. this.getChecked()
  176. }
  177. document.getElementById('treeNode').scrollTop = 0
  178. } else if (leve == 'city') {
  179. item.children = res.data || []
  180. item.children.map(d => {
  181. // 编辑时回显
  182. if (this.itemId && openType == 0) {
  183. d.checked = !!this.subareaAreaList.find(o => o.areaSn == d.areaSn)
  184. d.noSave = false
  185. } else {
  186. d.checked = item.checked
  187. d.noSave = true
  188. }
  189. })
  190. // 首次加载子级
  191. if (openType == 1) {
  192. this.addrProvinceList = JSON.parse(JSON.stringify(this.addrProvinceList))
  193. } else {
  194. item.isOpen = 1
  195. item.indeterminate = true
  196. }
  197. }
  198. } else {
  199. if (leve == 'province') {
  200. this.addrProvinceList = []
  201. } else if (leve == 'city') {
  202. item.children = []
  203. item.isOpen = 0
  204. }
  205. }
  206. })
  207. },
  208. // 过滤数据
  209. filterData (list) {
  210. list.map(item => {
  211. if (item.checked && !item.noSave) {
  212. this.subareaAreaList.push({
  213. areaSn: item.areaSn,
  214. areaName: item.name,
  215. areaType: item.type,
  216. areaParentSn: item.parentId
  217. })
  218. } else {
  219. if (item.children && item.children.length) {
  220. this.filterData(item.children)
  221. }
  222. }
  223. })
  224. },
  225. // 保存
  226. handleSave () {
  227. const _this = this
  228. _this.$refs.ruleForm.validate(valid => {
  229. if (valid) {
  230. const formData = JSON.parse(JSON.stringify(_this.form))
  231. this.subareaAreaList = []
  232. this.filterData(this.addrProvinceList)
  233. // console.log(this.subareaAreaList)
  234. // 编辑
  235. if (this.itemId) {
  236. formData.subareaSn = this.itemId
  237. }
  238. formData.subareaAreaList = this.subareaAreaList
  239. // console.log(formData, 'formData')
  240. _this.spinning = true
  241. subareaSaveSubareaArea(formData).then(res => {
  242. if (res.status == 200) {
  243. _this.$message.success(res.message)
  244. _this.$emit('ok')
  245. _this.isShow = false
  246. _this.spinning = false
  247. } else {
  248. _this.spinning = false
  249. }
  250. })
  251. } else {
  252. return false
  253. }
  254. })
  255. },
  256. // 编辑
  257. getChecked () {
  258. subareaFindBySn({ sn: this.itemId }).then(res => {
  259. // console.log(res.data)
  260. this.form.subareaName = res.data.subareaName
  261. this.subareaAreaList = res.data.subareaAreaList
  262. this.form.subareaSn = res.data.subareaSn
  263. this.form.id = res.data.id
  264. // 回显数据
  265. this.showChecked()
  266. })
  267. },
  268. showChecked () {
  269. // 省回显
  270. this.addrProvinceList.map(item => {
  271. item.checked = !!this.subareaAreaList.find(d => d.areaSn == item.areaSn)
  272. item.noSave = false
  273. item.indeterminate = false
  274. })
  275. // 市回显
  276. const hasCity = this.nowData.subareaAreaList.filter(item => item.citySubareaAreaList && item.citySubareaAreaList.length)
  277. // console.log(hasCity, 'hasCity.nowData')
  278. hasCity.map(item => {
  279. // 省
  280. const pre = this.addrProvinceList.find(k => k.areaSn == item.areaSn)
  281. // 获取子级
  282. this.getArea('city', pre, 0)
  283. })
  284. }
  285. },
  286. watch: {
  287. // 父页面传过来的弹框状态
  288. openModal (newValue, oldValue) {
  289. this.isShow = newValue
  290. },
  291. // 重定义的弹框状态
  292. isShow (newValue, oldValue) {
  293. if (!newValue) {
  294. this.$emit('close')
  295. this.form.subareaName = ''
  296. this.form.subareaSn = ''
  297. delete this.form.id
  298. } else {
  299. this.getArea('province')
  300. }
  301. }
  302. }
  303. }
  304. </script>
  305. <style lang="less">
  306. .marketingDivisionSetEdit-modal{
  307. .ant-modal-body {
  308. padding: 40px 40px 24px;
  309. }
  310. .btn-cont {
  311. text-align: center;
  312. margin: 35px 0 10px;
  313. }
  314. .tree-node{
  315. height: 300px;
  316. overflow: auto;
  317. .tree-box{
  318. .tree-parent{
  319. display: flex;
  320. align-items: center;
  321. border-bottom: 1px solid #eee;
  322. padding: 0 10px;
  323. > span{
  324. margin:0 5px;
  325. &:first-child{
  326. flex-grow: 1;
  327. }
  328. &:last-child{
  329. cursor: pointer;
  330. }
  331. }
  332. }
  333. .tree-child{
  334. padding: 10px 10px 10px 35px;
  335. display: flex;
  336. align-items: center;
  337. flex-wrap:wrap ;
  338. > div{
  339. margin:0 5px;
  340. }
  341. }
  342. }
  343. }
  344. }
  345. </style>