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

sortFunction doesn't work for DataTable #348

Closed
berdyshev opened this issue Mar 15, 2018 · 2 comments
Closed

sortFunction doesn't work for DataTable #348

berdyshev opened this issue Mar 15, 2018 · 2 comments
Assignees
Labels
Type: Bug Issue contains a defect related to a specific component.
Milestone

Comments

@berdyshev
Copy link
Contributor

I'm submitting a ... (check one with "x")

[x] bug report
[ ] feature request
[ ] support request => Please do not submit support request here, instead see http://forum.primefaces.org/viewforum.php?f=57

Current behavior
Documentation says that Column has sortFunction property, which can be used for custom sorting. But this callback is never called and I don't see even it in source code of the DataTable component.

Expected behavior
Ability to define custom sorting for the column

@berdyshev berdyshev changed the title sortFunction doesn't work for DataTable sortFunction doesn't work for DataTable Mar 15, 2018
@cagataycivici cagataycivici self-assigned this Mar 15, 2018
@cagataycivici cagataycivici added the Type: Bug Issue contains a defect related to a specific component. label Mar 15, 2018
@cagataycivici cagataycivici added this to the 1.5.1 milestone Mar 15, 2018
@cagataycivici
Copy link
Member

Will be fixed in next week's 1.5.1.

@mertsincan
Copy link
Member

Exp;

import React, { Component } from 'react';
import { DataTable } from '../../components/datatable/DataTable';
import { Column } from '../../components/column/Column';
import { CarService } from '../service/CarService';
import ObjectUtils from '../../components/utils/ObjectUtils';

export class DataTableSortDemo extends Component {

    constructor() {
        super();
        this.state = {
            cars: []
        };
        this.carservice = new CarService();
        this.onSortFunction = this.onSortFunction.bind(this);
    }

    componentDidMount() {
        this.carservice.getCarsSmall().then(data => this.setState({ cars: data }));
    }

    onSortFunction(event) {
        var value = [...this.state.cars];
        value.sort((data1, data2) => {
            let value1 = ObjectUtils.resolveFieldData(data1, event.field);
            let value2 = ObjectUtils.resolveFieldData(data2, event.field);
            let result = null;

            if (value1 == null && value2 != null)
                result = -1;
            else if (value1 != null && value2 == null)
                result = 1;
            else if (value1 == null && value2 == null)
                result = 0;
            else if (typeof value1 === 'string' && typeof value2 === 'string')
                result = value1.localeCompare(value2);
            else
                result = (value1 < value2) ? -1 : (value1 > value2) ? 1 : 0;

            return (event.order * result);
        });

        return value;
    }

    render() {
        return (
            <DataTable value={this.state.cars}>
                <Column field="vin" header="Vin" sortable={true} />
                <Column field="year" header="Year" sortable="custom" sortFunction={this.onSortFunction} />
                <Column field="brand" header="Brand" sortable={true} />
                <Column field="color" header="Color" sortable={true} />
            </DataTable>
        );
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug Issue contains a defect related to a specific component.
Projects
None yet
Development

No branches or pull requests

3 participants