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

Ordering Columns dynamically #393

Closed
apercas opened this issue Jan 28, 2019 · 38 comments
Closed

Ordering Columns dynamically #393

apercas opened this issue Jan 28, 2019 · 38 comments

Comments

@apercas
Copy link
Contributor

apercas commented Jan 28, 2019

Expected Behavior

I'll be pushing a PR for this. Is there any specific consideration I should take into account beforehand?

@chapmanjacobd
Copy link

what do you mean? multi-sort? or just changing which column is ordered by default?

@apercas
Copy link
Contributor Author

apercas commented Feb 4, 2019

Mostly the second one. So an user can reorder the columns before exporting a csv, for instance, dragging and dropping them where they wish.

@chapmanjacobd
Copy link

ohhh... changing the column order. That's cool. Are you going to use react-draggable ? You could probably combine the feature with the viewColumns boolean. Like dragging a column to the trash. Just an idea. I don't personally have a use case for changing the column order but I'm sure someone else will

@apercas
Copy link
Contributor Author

apercas commented Feb 5, 2019

I have not yet started with the dragging so if that's a good option I'll take it :)

@chapmanjacobd
Copy link

chapmanjacobd commented Feb 5, 2019 via email

@apercas
Copy link
Contributor Author

apercas commented Feb 6, 2019

I started implementing it with a handler in the columns popover.
screen shot 2019-02-06 at 12 15 19

Hope this approach makes sense. My idea is to reorder the data when the dragging stops: the user releases the mouse, we read the order in that instant and if it's different to the previous one, apply the column reordering.

@chapmanjacobd
Copy link

chapmanjacobd commented Feb 6, 2019 via email

@apercas
Copy link
Contributor Author

apercas commented Feb 6, 2019

I might switch for this other library: https://github.com/atlassian/react-beautiful-dnd

@apercas
Copy link
Contributor Author

apercas commented Feb 6, 2019

dnd

With react-beautiful-dnd

@apercas
Copy link
Contributor Author

apercas commented Feb 7, 2019

I have this working
dnd
apercas@f5bdeb1 Will push the PR soon.

@apercas
Copy link
Contributor Author

apercas commented Feb 7, 2019

I'm having some trouble updating the filters (since the index of the columns is different), any tip @jaksco ?

@chapmanjacobd
Copy link

chapmanjacobd commented Feb 7, 2019 via email

@apercas
Copy link
Contributor Author

apercas commented Feb 8, 2019

Ah, I'll check that PR and add @gregnb in the loop just in case, thanks :)

@gregnb
Copy link
Owner

gregnb commented Feb 9, 2019

@apercas This is pretty awesome. I would love to come up with a great name for the options to describe this feature. My first thought was 'draggableColumns' but that made me think for a second. What I think would truly be the killer feature that would make sense to most users is if we could also make the column headers draggable.

If they happen to select a table column and drag it we can put a highlight around the whole column at move it to a new place. What are your thoughts? Is this feasible?

@apercas
Copy link
Contributor Author

apercas commented Feb 13, 2019

Hi, sorry I've been out for some days. The first approach I took was actually dragging the headers but that interfered with the sorting sometimes, it was a little bit annoying trying to sort a column and move it (to eventually leave it in its current position) and I guess sorting a column is something a user does more often than changing the position of the column (since the main case for us is just the CSV export), thus adding it to the column list popover. I can always take a re-spin to that if still interested of course.

@chapmanjacobd
Copy link

I agree that discovery of the column moving feature may not be immediate to an end user, but I like the way that @apercas implemented this feature.

Additionally, I've never seen a table or spreadsheet software that uses the same area clicky-action to both sort and move columns.

just my 2 cents

@gregnb
Copy link
Owner

gregnb commented Feb 20, 2019

@apercas @jaksco Sorry for the late reply. I'm glad you both shared your thoughts on the header.

@apercas I would love for you to send in a PR. We also need the ability to turn this feature on/off so let's come up with a name that would be used in options and have it be a bool with default value of true. Maybe showColumnsDraggable ?

@apercas
Copy link
Contributor Author

apercas commented Feb 21, 2019

This is not a good week but I'll keep on working on this over the weekend.

@gabrielliwerant
Copy link
Collaborator

I agree that the killer feature would be to drag the column header. Though much better than not having the option at all, the problem with hiding the sort under the show columns button is that the feature is not discoverable. A user looking to sort columns might never think to look there, whereas dragging a column header is an immediately obvious and intuitive solution.

@gregnb
Copy link
Owner

gregnb commented Apr 14, 2019

hey @apercas any update on this work? maybe someone else can take up your effort? would you mind sharing it somewhere if that is ok?

@apercas
Copy link
Contributor Author

apercas commented May 6, 2019

Hello, sorry for the late response, it shouldn't be hard to finish it, please if anyone can take over it'd be great. Thank you. This is the branch: https://github.com/apercas/mui-datatables/tree/393_arrange_columns

@Oussidi1998
Copy link

Oussidi1998 commented Sep 2, 2019

Hi guys can someone tells me just how to add that feature in my project, I will appreciate that

@patorjk
Copy link
Collaborator

patorjk commented Sep 2, 2019

It's a pretty cool idea, though it doesn't look like it was finished:

https://codesandbox.io/s/github/apercas/mui-datatables/tree/393_arrange_columns

The 2 issues I see are:

  • Dragging a column screws up the filters.
  • The ViewColumns dialog shrinks a little when dragging (this isn't too bad of an issue though).

@Oussidi1998
Copy link

@patorjk Thanks man I am working with default package of Mui datatable which installed in my react porject via npm how can implement that ? and thanks

@patorjk
Copy link
Collaborator

patorjk commented Sep 3, 2019

You'd have to add that repo as a remote, fetch and checkout the 393_arrange_columns branch, and then rebase it. This post has the exact commands you'd want to use: https://stackoverflow.com/a/7244456/256460 (except you'd be rebasing a branch instead of master)

The problem is that it's unfinished, so you wouldn't be able to use this with filtering.

I'm debating taking a stab at continuing this idea, though I like the way React Grid does it's column dragging:

https://devexpress.github.io/devextreme-reactive/react/grid/docs/guides/column-reordering/

I'll have to think about it though.

@Oussidi1998
Copy link

@patorjk thanks buddy I appreciate that man big thanks

@apercas
Copy link
Contributor Author

apercas commented Sep 3, 2019

Hi, we ended up with our own table component with some extra features (as this one). It's not based on this repo but we're using it only in two only cases, 90% of the app still uses this. When that one is properly tested I'll share it here.

@Oussidi1998
Copy link

@apercas Ok boss we are waiting for that version , thanks a lot for that great work

@gabrielliwerant
Copy link
Collaborator

If anyone wants to take it on, I'd be happy to add a feature for direct column dragging to re-order columns. If you want to take it, it would be best to open a new issue with the approach outlined, and then I can assign you.

@patorjk
Copy link
Collaborator

patorjk commented Sep 3, 2019

@gabrielliwerant I may be interested, but don't assign anything to me just yet as I want to prototype some ideas I have (plus I'm not sure how much time I'll have in the near future). I think the best way to do this is to use react-dnd [1] for the column label dragging, and then CSS transform/transition styling to move the actual row cells to give the effect that DevExpress has when their columns are dragged (if you look at the dev console, you see that's how they're moving their columns).

However, if someone else sees this and wants at it, feel free to grab it up.

[1] react-beautiful-dnd is really cool and would be better for list movement like the View Columns dialog, but I think it's use cases are too specific for this and it wouldn't give a good dragging effect for the header columns - though if the ViewColumns dialog route is taken, react-beautiful-dnd is probably the way to go.

@gabrielliwerant
Copy link
Collaborator

Sounds good @patorjk! My preference would be to keep this as lightweight as possible, with the ability for devs to override stylistic concerns on their own rather than make too many appearance-based assumptions at the outset. I haven't used react-dnd yet, but it looks legit to me.

It would also be great if there were a way to keep any logic related to this self-contained, but I understand that may be a lot to ask given the current structure of the code.

@HumayounBaig
Copy link

Hey there, is anyone still working on the dynamic sorting? will it be added anytime soon cause i could really use it. Even dragging via columns dropdown would do.

@patorjk
Copy link
Collaborator

patorjk commented Jan 10, 2020

@HumayounBaig I finished a draft version of this feature and I was going to include it in my second round of PRs, but none of the 8 that I submitted ~6 weeks ago have been accepted yet so I don't really see myself going back and revisiting this.

However, I do have some suggestions based on my experience of working through this. The first step should be to create a new option called columnOrder which would be an array of integers representing the column indexes. If the table had n columns the array would default to [0, 1, ..., n - 1]. As you can see, this array represents the column indexes and the order that the columns should be presented in the table.

Changing this array would allow users to change the column order without messing with other aspects of the code. This is really important because if draggable columns actually change the columns array, they're not backward compatiable and they will break a lot of existing code.

Additionally, once the columnOrder functionality is in place (which is really simple - it mostly involves changing this file: https://github.com/gregnb/mui-datatables/blob/master/src/components/TableBody.js#L252), the drag and drop functionality can focus on getting drag and drop right and just modify the columnOrder property when it's done.

I used a library called React DnD and wrote all of my code using Hooks (which would not be compatible with the current library until #1077 is accepted). It's an awesome library, but my biggest gripe about it was that I was unable to change the cursor while dragging (react-dnd/react-dnd#325), which appears to be due to how HTML5 drag and drop works. I'm not sure how dev-express does their drag and drop, but from a visual standpoint I think changing the cursor is a better experience.

Additionally, I began to wonder if something like this might better be implement as a plug-in (#1089). However, there were some gotchas to this approach that would still mean React-DnD would have to be included in the main library (I can't remember at this point as it's been months since I worked on this). But even still, using the plug-in approach would allow the library to test run some ideas before fully committing to a particular approach.

@anudeepjusanu
Copy link

Hi any plans of including this feature anytime soon

@patorjk
Copy link
Collaborator

patorjk commented Jul 9, 2020

Draggable columns are now in the current version of the table: 3.1.5

@patorjk patorjk closed this as completed Jul 9, 2020
@HumayounBaig
Copy link

HumayounBaig commented Jul 13, 2020 via email

@patorjk
Copy link
Collaborator

patorjk commented Jul 13, 2020

@HumayounBaig Draggable columns requires code that's written in the TableHeadCell.js, when you do a customHeadReader, you are overriding the functionality of the TableHeadCell. So if you want the draggable functionality while using customHeadReader, you have to add that to your custom component / render function.

However, in the current release (3.1.5), I silently put in a feature called customHeadLabelRender (it will be documented in the upcoming 3.2.0 release), which allows you to render inside of the TableHeadCell without overriding the whole cell. Using this method along with setCellHeaderProps, probably allows most people to do 99% of the header customizations they need.

I would take a look at customHeadLabelRender and see if it fits your needs. If so, you can use that and draggable columns will work fine.

@HumayounBaig
Copy link

@patorjk thanks i'll try this let you know if it works for me :octocat:

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

8 participants