|
1 | 1 | <template>
|
2 | 2 | <div class="page-select">
|
3 | 3 | <h1>Select 相关实例</h1>
|
| 4 | + <p>以下例子中的<code class="sd">options</code>对应的内容统一如下:</p> |
| 5 | + <p> |
| 6 | + <pre class="hljs"><code class="javascript">const options = [ |
| 7 | + { |
| 8 | + name: '北京', |
| 9 | + id: '00001', |
| 10 | + spell: 'beijing', |
| 11 | + simpleSpell: 'bj' |
| 12 | + }, |
| 13 | + { |
| 14 | + name: '上海', |
| 15 | + id: '00002', |
| 16 | + spell: 'shanghai', |
| 17 | + simpleSpell: 'sh' |
| 18 | + }, |
| 19 | + { |
| 20 | + name: '广东', |
| 21 | + id: '00003', |
| 22 | + spell: 'guangdong', |
| 23 | + simpleSpell: 'gd' |
| 24 | + }, |
| 25 | + { |
| 26 | + name: '深圳', |
| 27 | + id: '00004', |
| 28 | + spell: 'shenzhen', |
| 29 | + simpleSpell: 'sz' |
| 30 | + }, |
| 31 | + { |
| 32 | + name: '重庆', |
| 33 | + id: '00005', |
| 34 | + spell: 'chongqing', |
| 35 | + simpleSpell: 'cq' |
| 36 | + }, |
| 37 | + { |
| 38 | + name: '西安', |
| 39 | + id: '00006', |
| 40 | + spell: 'xian', |
| 41 | + simpleSpell: 'xa' |
| 42 | + } |
| 43 | +]</code></pre> |
| 44 | + </p> |
| 45 | + <a-divider /> |
| 46 | + <h2>设置默认值</h2> |
| 47 | + <a-row :gutter="16"> |
| 48 | + <a-col :span="12"> |
| 49 | + 设置默认值或者当前选中值为空字符串<code class="sd">''</code>的时候会影响<code class="sd">placeholder</code>的展示。 |
| 50 | + </a-col> |
| 51 | + <a-col :span="12"> |
| 52 | + <a-select |
| 53 | + :default-value="''" |
| 54 | + showSearch |
| 55 | + placeholder="请选择选项" |
| 56 | + style="width: 120px; margin-right: 16px"> |
| 57 | + <a-select-option v-for="item in options" :key="item.code" :value="item.id"> |
| 58 | + {{ item.name }} |
| 59 | + </a-select-option> |
| 60 | + </a-select> |
| 61 | + <a-select |
| 62 | + :value="''" |
| 63 | + showSearch |
| 64 | + placeholder="请选择选项" |
| 65 | + style="width: 120px"> |
| 66 | + <a-select-option v-for="item in options" :key="item.code" :value="item.id"> |
| 67 | + {{ item.name }} |
| 68 | + </a-select-option> |
| 69 | + </a-select> |
| 70 | + </a-col> |
| 71 | + </a-row> |
| 72 | + <a-row :gutter="16"> |
| 73 | + <a-col :span="12"> |
| 74 | + 设置默认值或者当前选中值为<code class="sd">null</code>的时候会影响<code class="sd">placeholder</code>的展示。 |
| 75 | + </a-col> |
| 76 | + <a-col :span="12"> |
| 77 | + <a-select |
| 78 | + :default-value="null" |
| 79 | + showSearch |
| 80 | + placeholder="请选择选项" |
| 81 | + style="width: 120px; margin-right: 16px"> |
| 82 | + <a-select-option v-for="item in options" :key="item.code" :value="item.id"> |
| 83 | + {{ item.name }} |
| 84 | + </a-select-option> |
| 85 | + </a-select> |
| 86 | + <a-select |
| 87 | + :value="null" |
| 88 | + showSearch |
| 89 | + placeholder="请选择选项" |
| 90 | + style="width: 120px"> |
| 91 | + <a-select-option v-for="item in options" :key="item.code" :value="item.id"> |
| 92 | + {{ item.name }} |
| 93 | + </a-select-option> |
| 94 | + </a-select> |
| 95 | + </a-col> |
| 96 | + </a-row> |
| 97 | + <a-row :gutter="16"> |
| 98 | + <a-col :span="12"> |
| 99 | + 设置默认值或者当前选中值为<code class="sd">undefined</code>的时候,<code class="sd">placeholder</code>正常展示。 |
| 100 | + </a-col> |
| 101 | + <a-col :span="12"> |
| 102 | + <a-select |
| 103 | + :default-value="undefined" |
| 104 | + showSearch |
| 105 | + placeholder="请选择选项" |
| 106 | + style="width: 120px; margin-right: 16px"> |
| 107 | + <a-select-option v-for="item in options" :key="item.code" :value="item.id"> |
| 108 | + {{ item.name }} |
| 109 | + </a-select-option> |
| 110 | + </a-select> |
| 111 | + <a-select |
| 112 | + :value="undefined" |
| 113 | + showSearch |
| 114 | + placeholder="请选择选项" |
| 115 | + style="width: 120px"> |
| 116 | + <a-select-option v-for="item in options" :key="item.code" :value="item.id"> |
| 117 | + {{ item.name }} |
| 118 | + </a-select-option> |
| 119 | + </a-select> |
| 120 | + </a-col> |
| 121 | + </a-row> |
| 122 | + <a-divider /> |
| 123 | + <h2>设置筛选</h2> |
| 124 | + <h3>方式一:</h3> |
| 125 | + <a-row :gutter="16"> |
| 126 | + <a-col :span="12"> |
| 127 | + 设置<code class="sd">optionFilterProp</code>为<code class="sd">children</code> |
| 128 | + </a-col> |
| 129 | + <a-col :span="12"> |
| 130 | + <a-select |
| 131 | + showSearch |
| 132 | + optionFilterProp="children" |
| 133 | + placeholder="请选择选项" |
| 134 | + style="width: 120px; margin-right: 16px"> |
| 135 | + <a-select-option v-for="item in options" :key="item.code" :value="item.id"> |
| 136 | + {{ item.name }} |
| 137 | + </a-select-option> |
| 138 | + </a-select> |
| 139 | + </a-col> |
| 140 | + </a-row> |
| 141 | + <h3>方式二:</h3> |
| 142 | + <a-row :gutter="16"> |
| 143 | + <a-col :span="12"> |
| 144 | + 将<code class="sd">optionFilterProp</code>设置为<code class="sd">label</code>和<code class="sd">a-select-option</code>的<code class="sd">:label="item.name"</code>自定义属性 |
| 145 | + </a-col> |
| 146 | + <a-col :span="12"> |
| 147 | + <a-select |
| 148 | + showSearch |
| 149 | + optionFilterProp="label" |
| 150 | + placeholder="请选择选项" |
| 151 | + style="width: 120px; margin-right: 16px"> |
| 152 | + <a-select-option v-for="item in options" :key="item.code" :value="item.id" :label="item.name"> |
| 153 | + {{ item.name }} |
| 154 | + </a-select-option> |
| 155 | + </a-select> |
| 156 | + </a-col> |
| 157 | + </a-row> |
| 158 | + <h3>方式三:</h3> |
| 159 | + <a-row :gutter="16"> |
| 160 | + <a-col :span="12"> |
| 161 | + 使用<code class="sd">filterOption</code>和<code class="sd">optionLabelProp</code>实现筛选、拼音筛选、简拼筛选 |
| 162 | + </a-col> |
| 163 | + <a-col :span="12"> |
| 164 | + <a-select |
| 165 | + showSearch |
| 166 | + :filterOption="filterOption" |
| 167 | + optionLabelProp="label" |
| 168 | + placeholder="请选择选项" |
| 169 | + style="width: 120px; margin-right: 16px"> |
| 170 | + <a-select-option v-for="item in options" :key="item.code" :value="item.id" :label="item.name" :spell="item.spell" :simpleSpell="item.simpleSpell"> |
| 171 | + {{ item.name }} |
| 172 | + </a-select-option> |
| 173 | + </a-select> |
| 174 | + </a-col> |
| 175 | + </a-row> |
4 | 176 | </div>
|
5 | 177 | </template>
|
6 | 178 |
|
7 | 179 | <script>
|
| 180 | +const options = [ |
| 181 | + { |
| 182 | + name: '北京', |
| 183 | + id: '00001', |
| 184 | + spell: 'beijing', |
| 185 | + simpleSpell: 'bj' |
| 186 | + }, |
| 187 | + { |
| 188 | + name: '上海', |
| 189 | + id: '00002', |
| 190 | + spell: 'shanghai', |
| 191 | + simpleSpell: 'sh' |
| 192 | + }, |
| 193 | + { |
| 194 | + name: '广东', |
| 195 | + id: '00003', |
| 196 | + spell: 'guangdong', |
| 197 | + simpleSpell: 'gd' |
| 198 | + }, |
| 199 | + { |
| 200 | + name: '深圳', |
| 201 | + id: '00004', |
| 202 | + spell: 'shenzhen', |
| 203 | + simpleSpell: 'sz' |
| 204 | + }, |
| 205 | + { |
| 206 | + name: '重庆', |
| 207 | + id: '00005', |
| 208 | + spell: 'chongqing', |
| 209 | + simpleSpell: 'cq' |
| 210 | + }, |
| 211 | + { |
| 212 | + name: '西安', |
| 213 | + id: '00006', |
| 214 | + spell: 'xian', |
| 215 | + simpleSpell: 'xa' |
| 216 | + } |
| 217 | +] |
| 218 | +
|
8 | 219 | export default {
|
9 |
| - name: 'pageSelect' |
| 220 | + name: 'pageSelect', |
| 221 | + data () { |
| 222 | + return { |
| 223 | + options |
| 224 | + } |
| 225 | + }, |
| 226 | + mounted () { |
| 227 | + document.querySelectorAll('pre code').forEach((block) => { |
| 228 | + this.$hljs.highlightBlock(block) |
| 229 | + }) |
| 230 | + }, |
| 231 | + methods: { |
| 232 | + filterOption (inputValue, option) { |
| 233 | + console.log(inputValue, option) |
| 234 | + let currentOption |
| 235 | + for (let index = 0, len = this.options.length; index < len; index++) { |
| 236 | + const element = this.options[index] |
| 237 | + if (element.id === option.componentOptions.propsData.value) { |
| 238 | + currentOption = element |
| 239 | + break |
| 240 | + } |
| 241 | + } |
| 242 | + return currentOption.name.includes(inputValue) || currentOption.spell.includes(inputValue) || currentOption.simpleSpell.includes(inputValue) |
| 243 | + } |
| 244 | + } |
10 | 245 | }
|
11 | 246 | </script>
|
12 |
| - |
13 |
| -<style lang="less" scoped> |
14 |
| -.page-select {} |
15 |
| -</style> |
|
0 commit comments