tree.vue 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. <template>
  2. <view style="background-color: #f5f5f5;height: 100vh;overflow: hidden;">
  3. <search v-if="searchIf" ref="sea" @confirm="confirmSearch" />
  4. <view class="all">
  5. <view class="sheng">
  6. <view class="title">
  7. <scroll-view scroll-x @scroll="scroll" style="width: 100%;white-space: nowrap;" :scroll-left="scrollLeft" >
  8. <view v-for="(item,index) in parent" style="display:inline-block" :key="index">
  9. <view style='display:inline-block' v-if="index==0" @click="backTree(item,-1)" >
  10. <text v-if="index==parent.length-1&&!isre" class="none">全部</text>
  11. <text v-else class="active">全部</text>
  12. </view>
  13. <view style='display:inline-block' v-if="index==0&&isre" @click="backTree(item,-2)" :class="[index==parent.length-1&&isre]?'none':'active'">
  14. <text style='display:inline-block;margin: 0 6px;color: #D0D4DB;font-size: 14px;' class="iconfont icon-z043"></text>
  15. 搜索结果
  16. </view>
  17. <view style='display:inline-block' @click="backTree(item,index)" v-if="index!=0">
  18. <text style='display:inline-block;margin: 0 6px;color: #D0D4DB;font-size: 14px;' v-if="index!=0" class="iconfont icon-z043"></text>
  19. <text style='display:inline-block' v-if="index==parent.length-1" class="none">
  20. {{item[props.label]}}
  21. </text>
  22. <text style='display:inline-block' v-else class="active">
  23. {{item[props.label]}}
  24. </text>
  25. </view>
  26. </view>
  27. </scroll-view>
  28. </view>
  29. <view class="common" v-for="(item, index) in tree" @click="toChildren(item)" :key="index">
  30. <label class="content">
  31. <view class="checkbox" v-if="isCheck&&props.multiple" @click.stop="checkboxChange(item,index)">
  32. <text style="color: #0095F2;font-size: 20px;" v-if="item.checked" class="iconfont icon-xuanzhong txt colors"></text>
  33. <text style="font-size: 20px;color: #b8b8b8;" v-else class="iconfont icon-weixuanzhong txt"></text>
  34. </view>
  35. <view class="checkbox" v-if="isCheck&&!props.multiple&&props.nodes?item.user:true" @click.stop="checkbox(item,index)">
  36. <text style="font-size: 20px;color: #0ec7ff;" v-if="item.checked" class="txt iconfont icon-selected">
  37. </text>
  38. <text style="font-size: 20px;color: #b8b8b8;" v-else class="txt iconfont icon-weixuanzhong1">
  39. </text>
  40. </view>
  41. <view class="person" v-if="item.user" >
  42. {{item[props.label].substring(item[props.label].length-2)}}
  43. </view>
  44. <view class="word">{{item[props.label]}}</view>
  45. <view class="right">
  46. <text v-if="!item.user&&item.children.length>0" class="iconfont icon-z043"></text>
  47. </view>
  48. </label>
  49. </view>
  50. </view>
  51. </view>
  52. </view>
  53. </template>
  54. <script>
  55. import search from './components/search/index.vue'
  56. export default {
  57. props: {
  58. trees: {
  59. type: Array,
  60. default: () => []
  61. },
  62. isCheck: {
  63. type: Boolean,
  64. default: () => false
  65. },
  66. checkList: {
  67. type: Array,
  68. default: () => []
  69. },
  70. searchIf:{
  71. type:Boolean,
  72. default:()=>true
  73. },
  74. props: {
  75. type: Object,
  76. default: () => {
  77. return {
  78. label: 'name',
  79. children: 'children',
  80. multiple:true,
  81. checkStrictly:false,
  82. nodes:true
  83. }
  84. }
  85. }
  86. },
  87. data() {
  88. return {
  89. isre: false,
  90. tree: this.trees,
  91. allData: this.trees,
  92. parent: [1],
  93. searchResult: [],
  94. party_current: 100000,
  95. newCheckList:this.checkList,
  96. scrollLeft:999,
  97. old: {
  98. scrollTop: 0
  99. }
  100. }
  101. },
  102. components: {
  103. search
  104. },
  105. methods: {
  106. //多选
  107. checkboxChange: function(item, index) {
  108. var that = this;
  109. let status = !that.tree[index].checked,temp=Object.assign({},item)
  110. if(item.checked){//反选
  111. if(this.props.checkStrictly){
  112. if(item.user){
  113. that.$set(that.tree[index], 'checked', false)
  114. this.delUser(item.id)
  115. }
  116. else{
  117. that.$set(that.tree[index], 'checked', false)
  118. for(var index=0,n=this.newCheckList.length;index<n;index++){
  119. let temp=this.newCheckList[index];
  120. if(temp.id==item.id){
  121. this.newCheckList.splice(index,1)
  122. break
  123. }
  124. }
  125. this.delChild(item)
  126. }
  127. }else{
  128. that.$set(that.tree[index], 'checked', false)
  129. for(var index=0,n=this.newCheckList.length;index<n;index++){
  130. let temp=this.newCheckList[index];
  131. if(temp.id==item.id){
  132. this.newCheckList.splice(index,1)
  133. break
  134. }
  135. }
  136. }
  137. }else{//选中
  138. that.newCheckList.push(item)
  139. that.$set(that.tree[index], 'checked', true)
  140. if(this.props.checkStrictly){
  141. this.chooseChild(item)
  142. }
  143. }
  144. that.$emit('sendValue', that.newCheckList)
  145. },
  146. delUser(id){
  147. let that=this;
  148. for (var i = 0, len = that.newCheckList.length; i < len; i++) {
  149. if (that.newCheckList[i].id === id) {
  150. that.newCheckList.splice(i, 1)
  151. console.log('删user')
  152. return
  153. }
  154. }
  155. },
  156. chooseChild(arr){
  157. let that=this;
  158. if(!arr.user){
  159. for(var i=0,len=arr.children.length;i<len;i++){
  160. let item=arr.children[i];
  161. item.checked=true
  162. that.newCheckList.push(item)
  163. if(!item.user){
  164. this.chooseChild(item)
  165. }
  166. }
  167. }
  168. that.newCheckList=Array.from(new Set(that.newCheckList))
  169. },
  170. delChild(arr){
  171. console.log(this.newCheckList)
  172. if(!arr.user){
  173. for(var i=0,len=arr.children.length;i<len;i++){
  174. let item=arr.children[i];
  175. item.checked=false
  176. for(var index=0,n=this.newCheckList.length;index<n;index++){
  177. let temp=this.newCheckList[index];
  178. if(temp.id==item.id){
  179. this.newCheckList.splice(index,1)
  180. break
  181. }
  182. }
  183. if(!item.user){
  184. this.delChild(item)
  185. }
  186. }
  187. }
  188. },
  189. //单选
  190. checkbox: function(item, index) {
  191. var that = this;
  192. let status = !that.tree[index].checked
  193. that.$set(that.tree[index], 'checked', status)
  194. if (that.newCheckList.length <= 0) {
  195. that.newCheckList=[that.tree[index]]
  196. }
  197. else if(that.newCheckList.length==1){
  198. this.tree.forEach(item=>{
  199. if(item.id!=this.tree[index].id){
  200. item.checked=false
  201. }
  202. })
  203. that.newCheckList=[]
  204. if(that.tree[index].checked){
  205. that.newCheckList.push(that.tree[index])
  206. }
  207. }
  208. that.$emit('sendValue', that.newCheckList)
  209. },
  210. scroll: function(e) {
  211. console.log(e)
  212. this.old.scrollTop = e.detail.scrollTop
  213. },
  214. //到下一级
  215. toChildren(item) {
  216. var that = this;
  217. let children = that.props.children
  218. if (!item.user && item[children].length > 0) {
  219. that.tree = item[children]
  220. that.checkIf()
  221. if (that.parent[0].id == item.id) {
  222. } else {
  223. that.parent.push(item)
  224. }
  225. }
  226. },
  227. // 校验哪些选中了。单选
  228. checkIf(){
  229. let that=this;
  230. console.log(this.newCheckList,'new')
  231. for (var i = 0, len = that.tree.length; i < len; i++) {
  232. for (var j = 0, lens = that.newCheckList.length; j < lens; j++) {
  233. if (that.newCheckList[j].id === that.tree[i].id) {
  234. that.$set(that.tree[i], 'checked', true)
  235. break
  236. }
  237. else{
  238. that.$set(that.tree[i], 'checked', false)
  239. }
  240. }
  241. }
  242. },
  243. //搜索
  244. confirmSearch(val) {
  245. this.searchResult = []
  246. this.search(this.allData, val)
  247. this.isre = true
  248. this.parent.splice(1, 6666)
  249. uni.showLoading({
  250. title: '正在查找'
  251. })
  252. setTimeout(() => {
  253. this.tree = this.searchResult
  254. uni.hideLoading()
  255. }, 300)
  256. },
  257. search(data, keyword) {
  258. var that = this
  259. let children = that.props.children
  260. for (var i = 0, len = data.length; i < len; i++) {
  261. if (data[i].name.indexOf(keyword) >= 0) {
  262. that.searchResult.push(data[i])
  263. }
  264. if (!data[i].user && data[i][children].length > 0) {
  265. that.search(data[i][children], keyword)
  266. }
  267. }
  268. },
  269. //返回其它层
  270. backTree(item, index) {
  271. let that=this;
  272. if (index == -1) {
  273. this.tree = this.allData
  274. this.parent.splice(1, 6666)
  275. this.isre = false
  276. this.$refs.sea.clears()
  277. } else if (index == -2) {
  278. this.tree = this.searchResult
  279. this.parent.splice(1, 6666)
  280. } else {
  281. var t = this
  282. if (this.parent.length - index > 2) {
  283. this.parent.forEach((item, i) => {
  284. if (i > index) {
  285. this.parent.splice(i, 6666)
  286. }
  287. })
  288. } else if (index == this.parent.length - 1) {
  289. console.log('不操作')
  290. } else {
  291. this.parent.splice(this.parent.length - 1, 1)
  292. }
  293. this.tree = item[this.props.children]
  294. }
  295. if(that.props.multiple)return
  296. that.checkIf()
  297. }
  298. }
  299. }
  300. </script>
  301. <style lang="scss" scoped>
  302. .flex_between_center {
  303. display: flex;
  304. justify-content: space-between;
  305. align-items: center;
  306. }
  307. .checkbox {
  308. position: relative;
  309. height: 18px;
  310. margin-left: 5px;
  311. margin-right: 0px;
  312. width: 18px;
  313. .color{
  314. color: #00aaff;
  315. background-color: #00aaff;
  316. }
  317. .txt {
  318. line-height: 18px;
  319. width: 100%;
  320. height: 100%;
  321. display: flex;
  322. }
  323. }
  324. .checkBorder {
  325. border: 1px solid #ecdee4;
  326. }
  327. .all {
  328. padding-bottom: 68px;
  329. background-color: #f5f5f5;
  330. .sheng {
  331. margin-bottom: 10px;
  332. .title {
  333. height: 45px;
  334. padding: 0 16px;
  335. line-height: 45px;
  336. font-size: 15px;
  337. color: #606064;
  338. }
  339. }
  340. }
  341. .common {
  342. background-color: #fff;
  343. .content {
  344. display: flex;
  345. align-items: center;
  346. padding-left: 5px;
  347. border-bottom: 1px solid #fafafa;
  348. height: 50px;
  349. width: 100%;
  350. line-height: 50px;
  351. position: relative;
  352. font-size: 16px;
  353. .right{
  354. position: absolute;
  355. right: 15px;
  356. color: #babdc3;
  357. font-size: 16px;
  358. }
  359. .word {
  360. margin-left: 6px;
  361. }
  362. }
  363. }
  364. .person {
  365. height: 32px;
  366. width: 32px;
  367. border-radius: 50%;
  368. border: 1px solid #ff9f44;
  369. background-color: #fff9f4;
  370. margin-left: 0px;
  371. color: #f57a00;
  372. line-height: 32px;
  373. font-size: 11px;
  374. text-align: center;
  375. margin-left: 10px;
  376. }
  377. .active {
  378. color: #4297ED!important;
  379. }
  380. .none {
  381. color: #666666;
  382. }
  383. @import url("./css/icon.css");
  384. //@import url("https://at.alicdn.com/t/font_2009600_07r9regf6vmw.css");
  385. </style>