How to trigger v-data-table-server reload based on external trigger #19160
Replies: 5 comments 3 replies
-
I had the same problem, and I came up with the following hack, this is not a solution to the problem by any means and I'm not proud of it... The process goes like this:
import debounce from 'lodash/debounce'
// method to modify the VuetifyTable's search options
const modifySearch = search_options => {
let modified_options = JSON.parse(JSON.stringify(search_options))
let date_settings = null
if(date_from.value || date_to.value) {
let date_relation = 'between'
if(!date_from.value) {
date_relation = '<='
}
if(!date_to.value) {
date_relation = '>='
}
date_settings = {
min: date_from.value,
max: date_to.value,
relation: date_relation,
}
}
// add date settings to the search options
if(date_settings) {
modified_options['date'] = date_settings
}
return modified_options
}
// debounced method triggered by VuetifyTable's update:options event
const debouncedSearch = debounce(options => doSearch(options), 1000, { leading: false, trailing: true })
// http request to gather search results
const doSearch = options => {
let all_search_settings = modifySearch(options)
$axios.post('/search/endpoint', all_search_settings).then(processSearchResponse)
}
const date_token = computed(() => `${date_from.value}_${date_to.value}`)
// concatanated date_from and date_to as a string
watch(() => date_token.value, () => {
// Trigger a change in search_string (will remove it leater)
search_string.value += ' omgthisislame'
nextTick(() => {
search_string.value = search_string.value.replaceAll(' omgthisislame', '')
})
}) Please let me know if it's unclear, I'm using this solution at the moment, and itWorksOnMyMachine ™ :D But it would be lovely to have a way to trigger this more easily with a prop or something. |
Beta Was this translation helpful? Give feedback.
-
@ilyen85 Nice, thats a creative workaround, thanks a lot! I managed to make it almost work, however, I struggle with the debounce. From where do you import this function? |
Beta Was this translation helpful? Give feedback.
-
Hello guys I think I can trigger the table so easy. The table triggers when, @update:options, any options changes, so I only allow changes manually with the help of :search prop and a triggerFetch function. First I have a ref bind to the :search prop
Then I have a function like the following, which triggers the loadItems() on @update:options
So I can use triggerFetch in my watcher or wherever.
and in the loadItems I can get the actual login value for the api call like Maybe you can watch the model of your input and call something like triggerFetch? I used this approach with input before also and worked fine. If I understood your problem, I hope it helps |
Beta Was this translation helpful? Give feedback.
-
Hi, I've come across a similar situation and found a workaround that might help you. The idea is to leverage the Here's how you can do it:
Here's a code snippet that illustrates this approach: <template>
<v-data-table-server
:search="searchJson"
@update:options="refresh"
/>
</template>
<script>
export default {
data () {
return {
searchObject: {
query: 'foo',
fromDateFilter: Date(),
toDateFilter: Date(),
anyExtraFilter: 'bar'
}
}
},
computed: {
searchJson () {
return JSON.stringify(this.searchObject)
}
},
methods: {
refresh (options) {
const {
page,
itemsPerPage,
sortBy,
groupBy,
search
} = options
const {
query,
fromDateFilter,
toDateFilter,
anyExtraFilter
} = JSON.parse(search)
// perform api call using all required parameters
}
}
}
</script> In this code, Moreover, you can even bind |
Beta Was this translation helpful? Give feedback.
-
In my project I have a different approach: <template>
<v-data-table-server :v-model:options="options" @update:options="load" />
</template>
<script setup>
const options = ref({
page: 1,
itemsPerPage: 10
})
async function load({ page, itemsPerPage, sortBy }) {
// api call
}
async function refresh() {
await load(options.value)
}
</script> |
Beta Was this translation helpful? Give feedback.
-
Hi there,
I have implemented a server side table which reloads nicely upon change of pagination, sorting or search via the
@update:options=reload
event. However, I would like to trigger the reload also based on an external variable. In particular I have an input field that allows the user to specify a time window to filter for entries that were generated within this time window.I was thinking to implement a watcher on this variable which triggers the
reload
function however, I have no access to the requried paramter such aspage
,itemsPerPage
sortBy
orsearch
. Has anyone done this before?Any help would be highly appreciated
Beta Was this translation helpful? Give feedback.
All reactions