Clicking on the headers of ngTable does not trigger the sorting function

Currently, I am working on a table that needs sorting in my Angular project (v1.2.12). To facilitate this, I have integrated the ngTable module (v0.3.2) from ngTable.

The default sorting is based on the title column, but I also want to allow sorting by year. The issue I am facing is that when I click on a table heading to sort the column, the actual sorting is not reflected in the header, and the sorting parameter is unset.

Upon debugging, I noticed that params.sorting() returns: {title: undefined}. Subsequently, I am unable to click on any sortable headers as they become unresponsive.

I believe there might be something I am overlooking, although I can't pinpoint it at the moment.

Here is how my data looks:

[{
    "year": 2014,
    "title": "Golden title"
}, {
    "year": 2013,
    "title": "Golden title"
}, {
    "year": 2013,
    "title": "Another title"
}, {
    "year": 2014,
    "title": "Whetshoverwerd xsade  aas"
}, {
    "year": 2013,
    "title": "Another brilliant title"
}, {
"year": 2013,
    "title": "Wherever I may SOAP"
}]

The view component looks like this:

<table ng-table="tableParams" class="table">
    <tbody>
        <tr ng-repeat="document in $data">
            <td data-title="'Year'" sortable="'year'">{{document.year}}</td>
            <td data-title="'Title'" sortable="'title'"><a href="#">{{document.title}}</a></td>
        </tr>
    </tbody>
</table>

This view is encapsulated within a directive:

angular.module('appDirectives').directive('myModuleDirective', function () {
    // Directive logic
    return {
        restrict: 'E',
        templateUrl: 'path/to/view.html',
        replace: true,
        controller: function ($scope, $timeout, $filter, TitleList, ngTableParams) {

            $scope.tableParams = new ngTableParams({
                page: 1,
                count: 10,
                sorting: {
                    title: 'asc'
                }
            }, {
                total: 0,
                getData: function ($defer, params) {
                    TitleList.get({}, function (data) {
                        var orderedData = params.sorting() ?
                            $filter('orderBy')(data, params.orderBy()) :
                            data;

                        params.total(orderedData.length);
                        orderedData = orderedData.slice((params.page() - 1) * params.count(), params.page() * params.count());
                        $defer.resolve(orderedData);
                    });
                }
            });
        }
    };

Answer №1

I'm still trying to understand the problem, but it seems that the ngTable examples are based on version 0.3.1 while I am using version 0.3.2. Luckily, switching back to the older version resolved the issue for me.

Answer №2

This issue has popped up for me as well, it seems like there may be some glitches in version 0.3.2

To resolve the issue, I made a change to line 502 within the ng-table code by replacing it with the following line:

var sortingParams = (event && (event.ctrlKey || event.metaKey)) ? $scope.params.sorting() : {};

Although I'm not entirely certain if this is the definitive solution (as I am unsure why 'event' was undefined), it did manage to solve the problem for me. Therefore, please consider this as more of a temporary workaround rather than a permanent fix.

Answer №3

I encountered a similar issue related to response format.
The problem stemmed from returning an object instead of an array, like this:

Incorrect format:

{
"data": {
    "items": { 
        "0": {
            "post_id": "26",
            "post_date": "2015-06-24 00:00:00"

Correct Format:

{
"data": {
    "items": [
        {
            "id": "26",
            "create_date": "2015-02-19 14:15:44",

Although the table can work with an object, not all functions support it.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Caution: The React Hook useEffect is missing a dependency: 'postImage'. Make sure to include it or remove the dependency array before running npm run build

Every time I run the npm run build command in my project, a warning message is displayed stating that Warning: React Hook useEffect has a missing dependency: 'postImage'. Either include it or remove the dependency array. react-hooks/exhaustive- ...

Issue with the positioning of data labels in the top row on Highcharts Gantt chart not following the y offset properly

Currently, I am working on creating a milestone comparison chart where I want the data labels to be positioned above the milestone markers. These data labels consist of two rows of text. To achieve this, I have customized the chart.height property to incr ...

Facing difficulty updating a collection in MongoDB using Mongoose and JavaScript

This is the database schema I am working with let couponId = Schema({ RestaurantId: { type: String }, RestaurantName: { type: String }, RestaurantLocation: { type: String }, AssignedTo: Schema.Types.Mixed, CouponValue: { type: [Strin ...

Is it possible to deactivate the video feature within the Programmable Video API's P2P connection offered by Twilio?

Can we utilize the programmable video API from Twilio to initiate an audio call through a P2P connection? Our goal is to enable both audio and video calls utilizing the Programmable Video API with a P2P connection. Is this achievable? ...

The functionality of the ng-model directive in Angular JS is experiencing difficulties when applied to an array

My requirement I need two multiselect dropdowns They should allow selection of multiple values The second dropdown should populate based on the selected values from the first dropdown To achieve this, I am using AngularJS and Multiselect.Js library. S ...

Displaying values in form fields when a specific class is present

Whenever I input and submit the form fields correctly using #submit_btn, my values disappear. However, when they are not valid, this issue does not occur. I attempted to address this problem with jQuery: $('#submit_btn').click(function() { i ...

The script fails to load when utilizing jquery/ajax

After the page loads, I am attempting to dynamically add a script into a div using ajax/jquery. This particular script is sourced from a CPM network and is responsible for loading a banner. However, when I try to load this script through ajax post-page lo ...

Execute a PUT request within a Firebase Cloud Function database handler

I am working on syncing data between my server's database and Firebase realtime db. The first part, which involves syncing from my server to Firebase, is already complete. However, I am facing challenges with the second part - syncing data from Fireba ...

Verify whether the user is obstructing third-party domain

I've encountered a recurring issue where many of our support calls pertain to images not loading due to users blocking Amazon S3 or a similar third-party service. I rely on third-party services for hosting images, videos, and certain JavaScript elemen ...

New Npm release reintroducing dual string key:value combinations

When I run npm version, I get a strange key-value pair that I can't seem to remove. I'm not sure where it came from or how to get rid of it. 'livelocation-master': '1.0.0' A little background: I initially used react-native w ...

Two jQuery event handlers with similar functionality are displaying distinct behaviors

I've encountered an issue with two identical document fragment objects where separate event listeners are attached using jQuery, as demonstrated in this fiddle. Although the two event listeners should function the same way, only the first one behaves ...

Unable to show certain special characters when using ng-repeat

I am attempting to showcase an array of objects using ng-repeat. Below is the collection of objects $scope.test = [ { value1: "app\test\maintenance1", value2: "other value1" }, { value1: "app\test\m ...

Arranging items in ng-options through the use of orderBy

Here is the code I am struggling with: <select ng-model="i.br" ng-options="item.name as info[item.name] for item in e.br | orderBy:info[item.name]" class="combobox"> Unfortunately, the orderBy function is not sorting the options according to info[i ...

Adding new elements dynamically to an array in JavaScript while updating values in a ReactJS component

Hello, I am new to React js and facing an issue with a specific scenario. I have an array that is loaded from my database. For this example, let's take a look at the "fields" array: componentDidMount() { axios.get('http://localhost:200 ...

Exploring the contrast of utilizing the `new Date()` function compared to Firestore's `new

I am developing a new app and need to calculate the time elapsed between the start and end of a run. When the run starts, I record a timestamp using the new Date() function in Firebase. (I opted for this over firestore fieldValue to avoid conflicts with o ...

Maximizing Efficiency of Vendor Bundle in Angular 2 with Webpack

My MEAN application's client side is built in Angular2 with Webpack, but I'm facing slow loading times due to a large vendor modules JS file. Is there a way to optimize this situation? I want to separate the vendor's JS file. Below is my we ...

Plotting Graphs in Django using ChartJS

I am working on creating a graph using ChartJs to visualize my expenses. Below is a snippet of my view: @login_required def index(request): truncate_month = connection.ops.date_trunc_sql('month', 'date_reg') expense = Expense. ...

How can I select the div inside the second to last li element?

Within a particular list item, I have three divs. I am trying to target the second to last list item which contains a div with the class 'designnone'. Here's what I have so far: $('div[groupname=' + groupName + ']').find ...

What is the best way to obtain the output produced by a function when a button is clicked

When I click on a button, the desired action is to trigger a function that adds a new property inside an object within a large array of multiple objects. This function then eventually returns a new array. How can I access and utilize this new array? I am ...

Using Javascript to assign a hidden form value when the drop down selection changes - having trouble populating options from an array in Javascript

When using my JavaScript code to create 2 arrays for Product Category and Product selection, I encountered an issue. The user must first choose the type of Campaign they want to run before selecting the Product Category or Product. The 'Campaign' ...