Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(VDataTableServer): add search prop #16965

Merged
merged 4 commits into from
Apr 18, 2023
Merged

Conversation

nekosaur
Copy link
Member

Description

search prop is necessary so that we can signal table when update:options event is needed, so that we dont get double events, or none at all

closes #16959

Markup:

<script lang="ts" setup>
  import { computed, ref, watch } from 'vue'

  const headers = [
    { title: 'id', key: 'id' },
    { title: 'title', key: 'title' },
  ]
  const q = ref('')
  const users = ref([])
  const totalUsers = ref(0)

  const isLoading = ref(false)
  const options = ref<any>({})
  const createUrl = opts => {
    const url = 'https://jsonplaceholder.typicode.com/albums'

    const params = new URLSearchParams()

    const { page, itemsPerPage, q } = opts

    const _start = (page - 1) * itemsPerPage
    q && params.append('q', String(q))
    params.append('_start', String(_start))
    params.append('_limit', String(itemsPerPage))

    return `${url}?${params.toString()}`
  }

  const fetchData = opts => {
    const url = createUrl(opts)
    console.info('Fetching data using URL: ', url)
    isLoading.value = true

    fetch(url)
      .then(res => {
        totalUsers.value = Number(res.headers.get('X-Total-Count'))
        return res.json()
      })
      .then(data => {
        console.log(data)

        users.value = data

        isLoading.value = false

        console.log('-------\n')
      })
  }

  watch(
    options,
    (val, oldVal) => {
      console.log('Options changed!')

      const _val = JSON.parse(JSON.stringify(val))
      const _oldVal = JSON.parse(JSON.stringify(oldVal))

      console.log('val :>> ', _val)
      console.log('oldVal :>> ', _oldVal)

      fetchData({ ...options.value, q: q.value })
    },
    { deep: true }
  )
</script>

<template>
  <v-app>
    <v-main>
      <v-text-field label="Search..." @change="q = $event.target.value" />
      <v-data-table-server
        :headers="headers"
        :items="users"
        :search="q"
        :loading="isLoading"
        :page="options.page"
        :items-length="totalUsers"
        :items-per-page="2"
        @update:options="options = $event"
      />
      Total: {{ totalUsers }}
    </v-main>
  </v-app>
</template>

@nekosaur nekosaur self-assigned this Mar 20, 2023
@nekosaur nekosaur added this to the v3.1.x milestone Mar 20, 2023
@nekosaur nekosaur added the C: VDataTable VDatatable label Mar 20, 2023
@johnleider johnleider added the T: bug Functionality that does not work as intended/expected label Mar 23, 2023
@DeepKumbhare85
Copy link

Hi,

Thanks so much for working on the fix. I checked the pull request locally and tested it. It's working perfectly fine 💯

Thanks so much for your efforts. Can we merge this into the main branch now? v3.1.13 just released and unfortunately, we didn't get this in that patch release, We are eagerly waiting for the fix.

Best regards.

johnleider
johnleider previously approved these changes Apr 6, 2023
@johnleider
Copy link
Member

Resolve conflicts and we'll merge this straight away.

nekosaur and others added 4 commits April 16, 2023 13:09
search prop is necessary so that we can signal table when update:options event
is needed, so that we dont get double events, or none at all
@johnleider johnleider merged commit 939f85a into master Apr 18, 2023
@johnleider johnleider deleted the fix/data-table-server-search branch April 18, 2023 15:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C: VDataTableServer T: bug Functionality that does not work as intended/expected
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug Report][3.1.10] DataTable Double API call
4 participants