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

DataTable: Custom Column Component #644

Closed
sisisin opened this issue Nov 15, 2018 · 4 comments
Closed

DataTable: Custom Column Component #644

sisisin opened this issue Nov 15, 2018 · 4 comments

Comments

@sisisin
Copy link

sisisin commented Nov 15, 2018

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

Current behavior
Now, DataTable Component can't receive react function component

ok

<DataTable>
  <Column />
</DataTable>

ng

const MyColumn = () => (<Column />)
<DataTable>
  <MyColumn />
</DataTable>

Expected behavior

DataTable component can receive react function component

  • React version:
    16.5.2

  • PrimeReact version:
    2.0.0-beta.9

  • Language: [all | TypeScript X.X | ES6/7 | ES5]
    all

@mertsincan mertsincan added Status: Discussion Issue or pull request needs to be discussed by Core Team Type: Enhancement Issue contains an enhancement related to a specific component. Additional functionality has been add labels Feb 9, 2021
@mertsincan mertsincan added this to the 6.1.0 milestone Feb 9, 2021
@nrayburn-tech
Copy link

I am also looking to be able to do this. My use case is adding the “sortable” and “filter” props by default.

@Liam-OShea
Copy link

I am also looking for this functionality. I am applying different filter types depending on the column data. I would like to create a CustomColumn component to handle the state and handlers for each filter.

@mertsincan
Copy link
Member

mertsincan commented Mar 4, 2021

Hi,

DataTable component expects Column tags in its children. After finding them, it creates itself according to their properties. When using a functional component, React doesn't tell us what type of component it is returning. Therefore, we cannot access the information of the returned element. For these reasons, DataTable cannot be created using functional components.

// our codes
let columns = React.Children.toArray(this.props.children);
...
for (let i = 0; i < columns.length; i++) {
    console.log('Field: ' + coumns[i].props.field); // you can access all info about column
    ...
}

You can examine the example below to make dynamic columns;
https://primefaces.org/primereact/showcase/#/datatable/dynamiccolumns

Also, please see this demo;

const DataTableDynamicDemo = () => {
    const [products, setProducts] = useState([]);
    const columns = [
        {field: 'code', header: 'Code', filter: true, sortable: true},
        {field: 'name', header: 'Name'},
        {field: 'category', header: 'Category', filter: true},
        {field: 'quantity', header: 'Quantity', sortable: true}
    ];

    const productService = new ProductService();

    useEffect(() => {
        productService.getProductsSmall().then(data => setProducts(data));
    }, []); // eslint-disable-line react-hooks/exhaustive-deps

    const dynamicColumns = columns.map((col,i) => {
        return <Column key={col.field} {...col} />;
    });

    return (
        <div>
            <div className="card">
                <DataTable value={products}>
                    {dynamicColumns}
                </DataTable>
            </div>
        </div>
    );
}

dynamic_columns

Best Regards,

@mertsincan mertsincan removed this from the 6.1.0 milestone Mar 4, 2021
@mertsincan mertsincan removed Status: Discussion Issue or pull request needs to be discussed by Core Team Type: Enhancement Issue contains an enhancement related to a specific component. Additional functionality has been add labels Mar 4, 2021
@Liam-OShea
Copy link

@mertsincan Is there a way to create dynamic columns with more advanced filters than the default search filter enabled by setting filter=true in the column? For example create a dropdown toggle filter (like the representatives filter in the docs) in a dynamically created column. I don't understand how to deal with the filter state (selectedOptions) and filterElement in this case.

@melloware melloware changed the title Enable DataTable Component to receive react function component DataTable: Custom Column Component Jun 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants