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

fnSort prop in table and background in tags when change modelValue #521

Closed
wants to merge 35 commits into from

Conversation

clopezpro
Copy link
Contributor

@clopezpro clopezpro commented Aug 10, 2023

fnSort is used when onSort is executed in the Table component, this function can give me the freedom to sort the data in my own way or ask the backend to change the order when there is a lot of data
in template

<UTable
        :fn-sort="fnSort"
        :loading="loading"
        :columns="columns"
        :rows="data"
      >
        <template #url-data="{ row }">
          <UAvatar :src="row.url" alt="Avatar" />
        </template>
 </UTable>

in script

async function fetchData() {
  try {
    loading.value = true
    const params = new URLSearchParams()
    if (sortData.value.column && sortData.value.direction)
    {
      params.append('_sort', sortData.value.column)
      params.append('_order', sortData.value.direction.toUpperCase())
    }
    const response = await fetch(`https://jsonplaceholder.typicode.com/photos?${params.toString()}`)
    data.value = await response.json()
    loading.value = false
  }
  catch (err) {
    loading.value = false
    console.log(err)
  }
}
watch(sortData.value, () => {
  fetchData()
})
function fnSort (sort: { column: string | null; direction?: 'asc' | 'desc' | undefined }) {
    sortData.value.column = sort.column
    sortData.value.direction = sort.direction
}

could be related to #390 #296

In tags when modelValue is changed, the background of selected is not assigned to the tag
this could be the solution

watch(() => props.modelValue, (value) => {
      selectedIndex.value = value
      calcMarkerSize(value)
})

clopezpro and others added 30 commits July 18, 2023 17:21
Emit event Sort for capture this event and filter data in backend
this could also work
Remove emit sort
Co-authored-by: Benjamin Canac <canacb1@gmail.com>
Class priority issues in some cases when ring already defined on dark mode for example (input).
Co-authored-by: Benjamin Canac <canacb1@gmail.com>
@stackblitz
Copy link

stackblitz bot commented Aug 10, 2023

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@vercel
Copy link

vercel bot commented Aug 10, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
ui ❌ Failed (Inspect) Aug 14, 2023 2:52pm

Comment on lines 111 to 116
type: Object as PropType<{ column: string, direction: 'asc' | 'desc' }>,
default: () => ({})
},
fnSort: {
type: [ Function]
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What you think about adding fnSort to main srot prop like this

    sort: {
      type: Object as PropType<{ column: string, direction: 'asc' | 'desc', sortBy?: Function }>,
      default: () => ({})
    },
if (props.sort?.sortBy) {
  sort.value.column = toSort.column
  await props.sort.sortBy(toSort)
}

sort.value = toSort

Copy link
Contributor Author

@clopezpro clopezpro Aug 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better

<template>
     <UTable
        :sort="{
          column: sortData.column,
          direction: sortData.direction,
          sortBy: fnSort
        }"
        :loading="loading"
        :columns="columns"
        :rows="data"
      >
        <template #url-data="{ row }">
          <UAvatar :src="row.url" alt="Avatar" />
        </template>
      </UTable>
</template>
<script setup lang="ts"> 
const data = ref<[]>([])
const loading = ref<boolean>(false)
const sortData = ref<{ column: string | null; direction: 'asc' | 'desc'}>({
  column: 'id',
  direction: 'asc'
})
const columns = [{
                   key: 'id',
                   label: 'ID',
                   sortable: true
                 },
                 {
                   key: 'title',
                   label: 'Tile',
                   sortable: true
                 }, {
                   key: 'url',
                   label: 'IMG'
                 }]

async function fetchPhotos () {
  try {
    loading.value = true
    const params = new URLSearchParams()
    if (sortData.value.column && sortData.value.direction)
    {
      params.append('_sort', sortData.value.column)
      params.append('_order', sortData.value.direction.toUpperCase())
    }
    const response = await fetch(`https://jsonplaceholder.typicode.com/photos?${params.toString()}`)
    data.value = await response.json()
    loading.value = false
  }
  catch (err) {
    loading.value = false
    console.log(err)
  }
}

async function fnSort (sort: { column: string | null; direction: 'asc' | 'desc'  }) {
  sortData.value.column = sort.column
  sortData.value.direction = sort.direction
  await fetchPhotos()
}
onMounted(() => {
  fetchPhotos()
})
</script>

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it looks more readable to avoid a lot of props in features

@benjamincanac
Copy link
Member

Closing in favor of #803

This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants