naiveui | select下拉选择自定义选项渲染

naiveui,select,下拉,选择,自定义,选项,渲染 · 浏览次数 : 39

小编点评

```vue <n-select v-model:value=\"selectValue\" placeholder=\"请选择数据\" :options=\"sourceOption\" clearable filterable :render-label=\"renderReportsLabel\" :render-option=\"renderReportsOption\" /> ```

正文

select-demo1.gif

      <n-select
          v-model:value="selectValue"
          placeholder="请选择数据"
          :options="sourceOption"
          clearable
          filterable
          :render-label="renderReportsLabel"
          :render-option="renderReportsOption"
      />
   import { h, VNode , ref} from 'vue'
   import { SelectRenderLabel, NTag, NTooltip, SelectOption } from 'naive-ui'
   
   
   const selectValue = ref(null)
   
   const sourceOption = ref(
    Array.from({ length: 10 }, (j, i) => {
      return {
        label: `数据${i + 1}`,
        value: i + 1,
        code: `code${i + 1}`,
      }
    })
  )

   
  const renderReportsLabel: SelectRenderLabel = (option: any) =>
    h(
      'div',
      {
        class: 'flex items-center ',
      },
      {
        default: () => [
          h(
            NTag,
            { type: 'primary', size: 'tiny', bordered: false },
            { default: () => option.code.toUpperCase() }
          ),
          h('div', { class: 'truncate w-full ml-2' }, { default: () => option.label }),
        ],
      }
    )

  const renderReportsOption = ({ node, option }: { node: VNode; option: SelectOption }) =>
    h(NTooltip, null, {
      trigger: () => node,
      default: () => option.label,
    })

与naiveui | select下拉选择自定义选项渲染相似的内容: