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.

Cannot get sorting to work when source of table data is Ajax

See original GitHub issue

Thanks @pstephan1187 for this great plugin. I am however unable to get sorting to work, or even the sorting icons to appear when the source of the table data is an API loaded via Ajax.

Below is my code setup…


main.js code…

import 'bootstrap/dist/css/bootstrap.min.css'; import 'bootstrap-vue/dist/bootstrap-vue.css';

import Vue from 'vue'; import DatatableFactory from 'vuejs-datatable'; import BootstrapVue from 'bootstrap-vue';

import App from './App'; import router from './router';

DatatableFactory.useDefaultType(false) .registerTableType('datatable', (table_type) => { table_type.mergeSettings({ table: { class: 'table hover table-scroll', // table sorting: { classes: { canSort: ['sort'], // 'btn', 'btn-link', 'text-muted', sortNone: ['fa', 'fa-sort'], sortAsc: ['fa', 'fa-sort-down'], sortDesc: ['fa', 'fa-sort-up'], } } }, pager: { classes: { pager: 'pagination text-center', // selected: 'current', li: 'page-item', a: 'page-link', } } }); });

Vue.use(BootstrapVue); Vue.use(DatatableFactory); Vue.config.productionTip = false;

new Vue({ el: '#app', router, template: '<App/>', components: { App }, });


User.vue code

<template> <div> <h5>User List</h5>

<div class="col-md-12"> <div id="table" class="col-xs-12 table-responsive">

<datatable :columns="user.columns" :data="getData">

<template slot-scope="{ row }"> <tr> <td>{{row.first_name}}</td> <td>{{row.last_name}}</td> <td>{{row.national_id}}</td> <td>{{row.dob}}</td> <td>{{row.activated}}</td>

</tr> </template>

</datatable>

</div> </div>

<div class="col-md-12"> <div class="col-xs-12 form-inline"> <datatable-pager v-model="user.page" type="abbreviated" :per-page="user.perPage"></datatable-pager> </div> </div>

</div> </template>

<script> import axios from 'axios';

export default { data() { return { user: { columns: [ { label: 'First Name', field: '', sortable: true }, { label: 'Last Name', field: '', sortable: true }, { label: 'National ID', field: '', sortable: true }, { label: 'Date of Birth', field: '', sortable: true }, { label: 'Activated', field: '', sortable: true }, ], rows: [], page: 1, perPage: 2, }, }; }, methods: { getData: (params, setRowData) => { axios.get('/api/user', { params: params } ).then( function(res) { setRowData( res.data.data, res.data.data.length, ); }.bind(this)); } } }; </script>

image

I was expecting my CSS sorting classes to at least be applied on the table’s headers…

image

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:11

github_iconTop GitHub Comments

1reaction
SerGioPicconticommented, Jan 24, 2019

You’re welcome))

Problem is that you have not defined field value in columns array:

columns: [
{ label: 'First Name', field: '**user.name**', sortable: true },
{ label: 'Last Name', field: '**user.last_name**', sortable: true },
{ label: 'National ID', field: '**user.id**', sortable: true },
{ label: 'Date of Birth', field: '**user.date**', sortable: true },
{ label: 'Activated', field: '**user.activated**', sortable: true },
]

Hahaha, Works like a charm! What a shame I didn’t think of that … Thank you so much for your help @SerGioPicconti !

image

image

1reaction
sobierocommented, Jan 24, 2019

Problem is that you have not defined field value in columns array:

columns: [
{ label: 'First Name', field: '**user.name**', sortable: true },
{ label: 'Last Name', field: '**user.last_name**', sortable: true },
{ label: 'National ID', field: '**user.id**', sortable: true },
{ label: 'Date of Birth', field: '**user.date**', sortable: true },
{ label: 'Activated', field: '**user.activated**', sortable: true },
]

Hahaha, Works like a charm! What a shame I didn’t think of that, what a Shame… Thank you so much for your help @SerGioPicconti !

image

image

Read more comments on GitHub >

github_iconTop Results From Across the Web

AJAX source displays data but will not sort - DataTables
I can get an ajax source to display, but none the sorts and filters are not working. I have tried in 1.9.4 and...
Read more >
Jquery DataTable sorting not working when using ajax to ...
Based on my tests, your issue isn't reproducible. It works fine on my side. JS Fiddle: OP's test code. Nonetheless, your source code...
Read more >
Search and sorting functionality not working #114 - GitHub
I am integrating ajax-datatables-rails gem into my application, and can not get it to work with search and sort, and pagination.
Read more >
sorttable: Make all your tables sortable - Kryogenix
Sorttable works out the type of your columns in order to work out how to sort them (numbers sort differently than letters, for...
Read more >
Table Options - Bootstrap Table
Should implement the same API as jQuery ajax method. ... These buttons can be sorted with the table option buttonsOrder, ... Attribute: data-custom-sort....
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