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

Selecting single record #112

Closed
shahns opened this issue Oct 31, 2022 · 11 comments
Closed

Selecting single record #112

shahns opened this issue Oct 31, 2022 · 11 comments
Labels
question Further information is requested

Comments

@shahns
Copy link

shahns commented Oct 31, 2022

Is your feature request related to a problem? Please describe.
Currently, the way of selecting records allows for multiple record selection. Is there out-of-the-box way to restrict selection to single record only?

@icflorescu icflorescu added the question Further information is requested label Oct 31, 2022
@icflorescu
Copy link
Owner

Not at the moment.
Do you think we should implement this as a new feature?
If so, can you please elaborate on how you'd like it to work?
For instance, in case of single-record selection, should we render a Checkbox or a Radio?

@ShahriarKh
Copy link

ShahriarKh commented Nov 1, 2022

How about this?

const [selectedRecords, setSelectedRecords] = useState([]);

<DataTable
    selectedRecords={selectedRecords}
    onSelectedRecordsChange={setSelectedRecords}
    isRecordSelectable={(record) => record == selectedRecords[0] || selectedRecords.length < 1}
/>

Or for any n, you can simply do:

const n = 12;

<DataTable 
    {/* ... */}
    isRecordSelectable={(record) => selectedRecords.includes(record) || selectedRecords.length < n} 
/>

@shahns
Copy link
Author

shahns commented Nov 1, 2022

Not at the moment. Do you think we should implement this as a new feature? If so, can you please elaborate on how you'd like it to work? For instance, in case of single-record selection, should we render a Checkbox or a Radio?

A property allowMultipleSelect can be added which makes sure only current selected record is added to selectedRecords state.

I would still go ahead with checkbox and removing checkbox from the header field.

@shahns
Copy link
Author

shahns commented Nov 2, 2022

    isRecordSelectable={(record) => record == selectedRecords[0] || selectedRecords.length < 1}

@ShahriarKh thanks for the suggestion. But, unfortunately this doesn't serve the purpose.

I tried this. Still I could select multiple items by clicking header checkbox.

image

Also, this requires me to uncheck the selected record every time before checking the new record.

For now, I use this workaround:

const [selectedRecords, setSelectedRecords] = useState<DataProps[]>([]);
onSelectedRecordsChange={(records) => setSelectedRecords([records[records.length - 1]])}

@otobot1
Copy link
Contributor

otobot1 commented Nov 16, 2022

I think if you are only able to select one row then a radio would be more clear. But I think making that configurable (with radio as default) would be ideal.

@shahns
Copy link
Author

shahns commented Dec 19, 2022

Hi, does community feel this feature is important and is it planned sometime in near future? If not, I don't mind closing this issue.

@russocorp
Copy link

Single record selection clicking on line is really cool.
With this, i can get ID from clicked line and use for edit item and other things too.

@icflorescu
Copy link
Owner

@russocorp You can easily get the record data of a clicked row with onRowClick: handling-row-clicks

@icflorescu
Copy link
Owner

In the end, since this is really an edge case, I'd suggest using a custom column with a render function showing a component.

@icflorescu icflorescu closed this as not planned Won't fix, can't repro, duplicate, stale Feb 1, 2023
@russocorp
Copy link

@russocorp You can easily get the record data of a clicked row with onRowClick: handling-row-clicks

I know that, but after click, line dont have a diferent background or something like that.
https://www.ag-grid.com/react-data-grid/row-selection/ have this, for example.

@icflorescu
Copy link
Owner

@russocorp Since you'll have to manage the selected row in your state anyway, you could easily apply your own particular style to it: https://icflorescu.github.io/mantine-datatable/examples/additional-styling#row-styling

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

5 participants