linkage-picker.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <template>
  2. <view class="w-picker-view">
  3. <picker-view class="d-picker-view" :indicator-style="itemHeight" :value="pickVal" @change="handlerChange">
  4. <picker-view-column v-for="(group,gIndex) in range" :key="gIndex">
  5. <view class="w-picker-item" v-for="(item,index) in group" :key="index">{{item[nodeKey]}}</view>
  6. </picker-view-column>
  7. </picker-view>
  8. </view>
  9. </template>
  10. <script>
  11. let _this=null;
  12. export default {
  13. data() {
  14. return {
  15. pickVal:[],
  16. range:[],
  17. checkObj:{}
  18. };
  19. },
  20. props:{
  21. itemHeight:{
  22. type:String,
  23. default:"44px"
  24. },
  25. value:{
  26. type:[Array,String],
  27. default:""
  28. },
  29. defaultType:{
  30. type:String,
  31. default:"label"
  32. },
  33. options:{
  34. type:Array,
  35. default(){
  36. return []
  37. }
  38. },
  39. defaultProps:{
  40. type:Object,
  41. default(){
  42. return{
  43. lable:"label",
  44. value:"value",
  45. children:"children"
  46. }
  47. }
  48. },
  49. level:{
  50. //多级联动层级,表示几级联动
  51. type:[Number,String],
  52. default:2
  53. }
  54. },
  55. computed:{
  56. nodeKey(){
  57. return _this.defaultProps.label;
  58. },
  59. nodeVal(){
  60. return _this.defaultProps.value;
  61. },
  62. nodeChild(){
  63. return _this.defaultProps.children;
  64. }
  65. },
  66. watch:{
  67. value(val){
  68. if(_this.options.length!=0){
  69. this.initData();
  70. }
  71. },
  72. options(val){
  73. this.initData();
  74. }
  75. },
  76. created() {
  77. _this=this;
  78. if(_this.options.length!=0){
  79. _this.initData();
  80. }
  81. },
  82. methods:{
  83. getData(){
  84. //用来处理初始化数据
  85. let options=_this.options;
  86. let col1={},col2={},col3={},col4={};
  87. let arr1=options,arr2=[],arr3=[],arr4=[];
  88. let col1Index=0,col2Index=0,col3Index=0,col4Index=0;
  89. let a1="",a2="",a3="",a4="";
  90. let dVal=[],obj={};
  91. let value=this.value;
  92. let data=[];
  93. a1=value[0];
  94. a2=value[1];
  95. if(_this.level>2){
  96. a3=value[2];
  97. }
  98. if(_this.level>3){
  99. a4=value[3];
  100. };
  101. /*第1列*/
  102. col1Index=arr1.findIndex((v)=>{
  103. return v[_this.defaultType]==a1
  104. });
  105. col1Index=value?(col1Index!=-1?col1Index:0):0;
  106. col1=arr1[col1Index];
  107. /*第2列*/
  108. arr2=arr1[col1Index][_this.nodeChild];
  109. col2Index=arr2.findIndex((v)=>{
  110. return v[_this.defaultType]==a2
  111. });
  112. col2Index=value?(col2Index!=-1?col2Index:0):0;
  113. col2=arr2[col2Index];
  114. // /*第3列*/
  115. // if(_this.level>2){
  116. // arr3=arr2[col2Index][_this.nodeChild];
  117. // col3Index=arr3.findIndex((v)=>{
  118. // return v[_this.defaultType]==a3;
  119. // });
  120. // col3Index=value?(col3Index!=-1?col3Index:0):0;
  121. // col3=arr3[col3Index];
  122. // };
  123. switch(_this.level*1){
  124. case 2:
  125. dVal=[col1Index,col2Index];
  126. obj={
  127. col1,
  128. col2
  129. }
  130. data=[arr1,arr2];
  131. break;
  132. case 3:
  133. dVal=[col1Index,col2Index,col3Index];
  134. obj={
  135. col1,
  136. col2,
  137. col3
  138. }
  139. data=[arr1,arr2,arr3];
  140. break;
  141. case 4:
  142. dVal=[col1Index,col2Index,col3Index,col4Index];
  143. obj={
  144. col1,
  145. col2,
  146. col3,
  147. col4
  148. }
  149. data=[arr1,arr2,arr3,arr4];
  150. break
  151. }
  152. return {
  153. data,
  154. dVal,
  155. obj
  156. }
  157. },
  158. initData(){
  159. let dataData=_this.getData();
  160. let data=dataData.data;
  161. let arr1=data[0];
  162. let arr2=data[1];
  163. let arr3=data[2]||[];
  164. let arr4=data[3]||[];
  165. let obj=dataData.obj;
  166. let col1=obj.col1,col2=obj.col2,col3=obj.col3||{},col4=obj.col4||{};
  167. let result="",value=[];
  168. let range=[];
  169. switch(_this.level){
  170. case 2:
  171. value=[col1[_this.nodeVal],col2[_this.nodeVal]];
  172. result=`${col1[_this.nodeKey]+col2[_this.nodeKey]}`;
  173. range=[arr1,arr2];
  174. break;
  175. case 3:
  176. value=[col1[_this.nodeVal],col2[_this.nodeVal],col3[_this.nodeVal]];
  177. result=`${col1[_this.nodeKey]+col2[_this.nodeKey]+col3[_this.nodeKey]}`;
  178. range=[arr1,arr2,arr3];
  179. break;
  180. case 4:
  181. value=[col1[_this.nodeVal],col2[_this.nodeVal],col3[_this.nodeVal],col4[_this.nodeVal]];
  182. result=`${col1[_this.nodeKey]+col2[_this.nodeKey]+col3[_this.nodeKey]+col4[_this.nodeKey]}`;
  183. range=[arr1,arr2,arr3,arr4];
  184. break;
  185. }
  186. _this.range=range;
  187. _this.checkObj=obj;
  188. _this.$nextTick(()=>{
  189. _this.pickVal=dataData.dVal;
  190. });
  191. _this.$emit("change",{
  192. result:result,
  193. value:value,
  194. obj:obj
  195. })
  196. },
  197. handlerChange(e){
  198. let arr=[...e.detail.value];
  199. let col1Index=arr[0],col2Index=arr[1],col3Index=arr[2]||0,col4Index=arr[3]||0;
  200. let arr1=[],arr2=[],arr3=[],arr4=[];
  201. let col1,col2,col3,col4,obj={};
  202. let result="",value=[];
  203. arr1=_this.options;
  204. arr2=(arr1[col1Index]&&arr1[col1Index][_this.nodeChild])||arr1[arr1.length-1][_this.nodeChild]||[];
  205. col1=arr1[col1Index]||arr1[arr1.length-1]||{};
  206. col2=arr2[col2Index]||arr2[arr2.length-1]||{};
  207. if(_this.level>2){
  208. arr3=(arr2[col2Index]&&arr2[col2Index][_this.nodeChild])||arr2[arr2.length-1][_this.nodeChild];
  209. col3=arr3[col3Index]||arr3[arr3.length-1]||{};
  210. }
  211. switch(_this.level){
  212. case 2:
  213. obj={
  214. col1,
  215. col2
  216. }
  217. _this.range=[arr1,arr2];
  218. result=`${(col1[_this.nodeKey]||'')+(col2[_this.nodeKey]||'')}`;
  219. value=[col1[_this.nodeVal]||'',col2[_this.nodeVal]||''];
  220. break;
  221. case 3:
  222. obj={
  223. col1,
  224. col2,
  225. col3
  226. }
  227. _this.range=[arr1,arr2,arr3];
  228. result=`${(col1[_this.nodeKey]||'')+(col2[_this.nodeKey]||'')+(col3[_this.nodeKey]||'')}`;
  229. value=[col1[_this.nodeVal]||'',col2[_this.nodeVal]||'',col3[_this.nodeVal]||''];
  230. break;
  231. case 4:
  232. obj={
  233. col1,
  234. col2,
  235. col3,
  236. col4
  237. }
  238. _this.range=[arr1,arr2,arr3,arr4];
  239. result=`${(col1[_this.nodeKey]||'')+(col2[_this.nodeKey]||'')+(col3[_this.nodeKey]||'')}`;
  240. value=[col1[_this.nodeVal]||'',col2[_this.nodeVal]||'',col3[_this.nodeVal]||''];
  241. break;
  242. }
  243. _this.checkObj=obj;
  244. _this.pickVal=arr;
  245. _this.$emit("change",{
  246. result:result,
  247. value:value,
  248. obj:obj
  249. })
  250. }
  251. }
  252. }
  253. </script>
  254. <style lang="scss">
  255. @import "./w-picker.css";
  256. </style>