question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Table] How should look like sort provider function?

See original GitHub issue

Hello,

My function looks like:

sortProvider(ctx) {
        this.isBusy = true;
        const promise = fetch(`http://localhost:3000/photos?_sort=${ctx.sortBy}&_order=${ctx.sortDesc ? 'asc' : 'desc'}`);

        return promise
          .then((data) => {
            this.totalPhotos = parseInt(data.headers.get('X-Total-Count'), 10);
            return data.json();
          })
          .then((data) => {
            this.isBusy = false;
            return data;
          });
      },

The function works perfect I get sorted data, but table does not reflect to data change.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:14 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
tmorehousecommented, Jul 18, 2017

The sort-changed is just a notification event that the sorting has changed/cleared. It doesn’t receive the sort data or callback or promise. sort-changed is only needed when you are returning an array (or setting an array) via the :items prop.

The provider function assigned to :items will be called when the sorting changes (as long as no-provider-sorting is not set.

The ctx object provided to the :items provider function includes the sortBy and sortDesc.

So do this:

<b-table :items="myProvider"></b-table>
myProvider(ctx) {
  this.isBusy = true;
  let url = `http://localhost:3000/photos?_page=${ctx.currentPage}&_limit=${ctx.perPage}`;
  if (ctx.sortBy) {
    url = `${url}&_sort=${ctx.sortBy}&_order=${ctx.sortDesc ? 'desc' : 'asc'}`;
  }
  return fetch(url)
    .then((data) => {
      this.totalPhotos = parseInt(data.headers.get('X-Total-Count'), 10);
      this.isBusy = false;
      return data.json();
    });
}
1reaction
tmorehousecommented, Jul 18, 2017

Example:

Per Page:
<b-form-select :options="[5, 10, 15]"
               v-model="perPage"
></b-form-select>
<br>
<b-pagination :total-rows="totalPhotos"
              :per-page="perPage"
              v-model="currentPage"
></b-pagination>
<br>
<b-table :items="myProvider"
         :fields="fields"
         :current-page="currentPage"
         :per-page="perPage"
         :busy="isBusy"
></b-table>

App definition:

{
  data: {
    totalPhotos: 0,
    perPage: 5,
    currentPage: 1,
    isBusy: false,
    fields: {
      // Your fields definition
    }
  },
  methods: {
    myProvider(ctx) {
      this.isBusy = true;
      let url = `http://localhost:3000/photos?_page=${ctx.currentPage}&_limit=${ctx.perPage}`;
      if (ctx.sortBy) {
        url = `${url}&_sort=${ctx.sortBy}&_order=${ctx.sortDesc ? 'desc' : 'asc'}`;
      }
      return fetch(url)
        .then((data) => {
          this.totalPhotos = parseInt(data.headers.get('X-Total-Count'), 10);
          this.isBusy = false;
          return data.json();
        });
    }
  }
}

In this example, anytime you click on a pagination page, or sort by clicking a header, then myProvider will be called with the context ctx object set with the appropriate values.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Sort data in a table - Microsoft Support
Sort the table. Select a cell within the data. Select Home > Sort & Filter. Sort&Filter. Or, select Data > Sort. DATA-Sort&Filter. Select...
Read more >
Video: Sort data in a range or table - Microsoft Support
Select a range of data, such as A1:L5 (multiple rows and columns), or C1:C80 (a single column). The range can include titles (headers)...
Read more >
Creating a React sortable table - LogRocket Blog
In this case, we will use the sort() function. However, depending on the item's data type, we can sort elements using different methods....
Read more >
Implementing Custom Sorting in Columns in JavaScript
This tutorial will describe how to give your users the ability to sort table columns using the MatTable's built-in sorting mechanism. Demo ...
Read more >
Creating Sortable Tables With React - Smashing Magazine
Making your tables sortable in React might sound like a daunting task, ... We need to do this because the Array.prototype.sort function ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found