Browse Source

fix: 修复picker组件自定义数据默认值问题

songchenglin3 5 years ago
parent
commit
3a1198d8fa
2 changed files with 24 additions and 4 deletions
  1. 13 3
      src/packages/picker/demo.vue
  2. 11 1
      src/packages/picker/picker-slot.vue

+ 13 - 3
src/packages/picker/demo.vue

@@ -74,7 +74,7 @@
     <nut-picker
       :is-visible="isVisible2"
       title="请选择城市"
-      :default-value-data="defaultValueData"
+      :default-value-data="defaultValueData3"
       :list-data="custmerCityData"
       @close="switchPicker('isVisible2')"
       @confirm="setChooseValueCustmer"
@@ -188,9 +188,19 @@ export default {
           }
         ]
       ],
-      cityCustmer: null,
+      cityCustmer: '北京-海淀区',
       isVisible2: false,
-      defaultValueData1: null
+      defaultValueData1: null,
+      defaultValueData3: [
+        {
+          label: 1,
+          value: '北京'
+        },
+        {
+          label: 4,
+          value: '海淀区'
+        }
+      ]
     };
   },
   created() {

+ 11 - 1
src/packages/picker/picker-slot.vue

@@ -181,7 +181,17 @@ export default {
 
     modifyStatus(type, defaultValue) {
       defaultValue = defaultValue ? defaultValue : this.defaultValue;
-      let index = this.listData.indexOf(defaultValue);
+      let index = -1;
+      if (defaultValue && defaultValue.value) {
+        this.listData.some((item, idx) => {
+          if (item.value == defaultValue.value) {
+            index = idx;
+            return true;
+          }
+        });
+      } else {
+        index = this.listData.indexOf(defaultValue);
+      }
       this.currIndex = index === -1 ? 1 : index + 1;
       let move = index === -1 ? 0 : index * this.lineSpacing;
       type && this.setChooseValue(-move);