31
36

More than 1 year has passed since last update.

【Vuetify】v-autocompleteのsearch-inputをデバウンスして0.5秒後に入力された値のみでデータを取得

Last updated at Posted at 2023-01-22

個人の備忘録です。

環境

  • vue: 2.7.x(Composition API / script setup)
  • vuetify: 2.6.x

実現したいこと

困った点

1.gif

変更前
<script setup lang="ts">
省略

interface State {
  value: string;
  isSearching: boolean;
  users: Response[];
}

const state: State = reactive({
  value: '',
  isSearching: false,
  users: [],
});

const onSearchInput = async (searchName: string) => {
  state.isSearching = true;
  state.users = await repository.getUsers(searchName);
  state.isSearching = false;
};
</script>

<template>
  <v-autocomplete
    v-model="state.value"
    :items="state.users"
    // ここが keydown 毎に発火してしまう
    @update:search-input="onSearchInput"
    :loading="state.isSearching"
  >
  </v-autocomplete>
</template>

解決コード

  • JavaScritp の clearTimeout で古いタイムアウトを解除する

2.gif

変更後
<script setup lang="ts">
省略

interface State {
  value: string;
  isSearching: boolean;
  users: Response[];
  timerId?: ReturnType<typeof setTimeout>;
}

const state: State = reactive({
  value: '',
  isSearching: false,
  users: [],
  timerId: undefined,
});

const onSearchInput = (searchName: string) => {
  state.isSearching = true;

  // 古いタイムアウトは解除する
  clearTimeout(state.timerId);

  // 0.5秒後に発火する
  state.timerId = setTimeout(async () => {
    state.users = await repository.getUsers(searchName);
    state.isSearching = false;
  }, 500);
};
</script>

<template>
  <v-autocomplete
    v-model="state.value"
    :items="state.users"
    @update:search-input="onSearchInput"
    :loading="state.isSearching"
  >
  </v-autocomplete>
</template>

参照

31
36
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
31
36