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

Refactor delete #3379

Merged
merged 3 commits into from
Dec 14, 2020
Merged

Refactor delete #3379

merged 3 commits into from
Dec 14, 2020

Conversation

martijnb92
Copy link
Contributor

@martijnb92 martijnb92 commented Dec 4, 2020

This PR fixes the following:

  • [minor bug] When an item was deleted, Showing x to x of x entries was not updated. Fixed that by using the DataTables API to redraw the table
  • Removed (old?) call to close a modal. (I can't think of any modal that should be closed in the list view)
  • Refactor the delete button code by using some Sweetalert defaults. Which also makes the cancel button the default selected button (for data safety this is better).

@tabacitu
Copy link
Member

tabacitu commented Dec 8, 2020

Totally agree with all the decisions you've made here @martijnb92 - good work! It's definitely something we should do.

I'll let @promatik or @pxpm pick this up to test it & review it, afterwards I'll hit merge. This new process where everything needs to be reviewed by one of them takes a little longer, but it lets fewer mistakes slip, so it's better in the long run 😄

Thanks again, cheers!

Copy link
Contributor

@pxpm pxpm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left comments in code.

Thanks again @martijnb92

// Remove the row from the datatable
row.remove();
crud.table.row($("#crudTable a[data-route='"+route+"']").parents('tr')).remove().draw();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @martijnb92

Thanks again for one more contribution. I was testing this out, and I am afraid that this is a change we cannot aford to do.

Redrawing the table is a very expensive operation, if you want to have a better understanding of what I mean load 200 entries in the table and delete one, using both versions of code. (plus the cost of drawing the table goes up as the number of columns go up too not only the number of entries).

Let me know @martijnb92 :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did some testing with a table with 350 entries and 10 columns, never did it take more than a second to redraw the table, even on my phone.
The old version of the code is a bit faster/instant, since it only removes the table row. But that means the totale entries line at the top is incorrect, since it’s not updated on delete.
That’s why in my opinion the re-draw option in datatable is a good idea, since it actually removes the data instead of just removing the rendered table row.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for clarification on this.

Unfortunatelly we are aware of some issues in Datatables that make some tables take some amount of time to load. It's a combination of the number of columns, type of columns, number of entries etc, so I must sustain what I said before:

  • the redraw option is only good in already fast tables, if you happen to have a table that takes 4 seconds to load, it will take 4 seconds to redraw (4 seconds it's not an HUGE issue in real big data cruds), but it will be 4 times faster to do it like previously than redrawing the table.

Let's keep in mind what you mentioned: the totale entries line at the top is incorrect, since it’s not updated on delete, are you aware of someway we could update this value upon deletion or is redrawing the only option ?

Thanks for your effort in solving this, I am pretty sure we will end this with a better solution than what we have in current backpack code 👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm good thinking... both of you 😂

  • re-drawing the table is indeed more expensive;
  • that number is wrong, so... this is a bug; a small one, but a bug nonetheless;

Tricky one...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok so... here's how I see it:

  • Personally I don't see how we'd be able to redraw just that number, instead of redrawing the whole table. Not without breaking changes which are definitely not worth it.
  • I don't think it's worth trying to just change the number at the top, because in some cases, the count() operation is the most expensive one (think tables with millions upon millions of entries - over there getting 10 entries is fast, counting 99 million entries is what's expensive).
  • Decrementing that number with JS... yeah... that feels like a hack so I'd rather we not do that either.
  • Redrawing the datatable also prevents this one situation, that was annoying to me in the past: deleting a bunch of stuff one by one leads you to look at an empty table;
  • Redrawing the datatable also fixes an inconsistency we currently have:
    • Delete doesn't do a table redraw
    • BulkDelete does do a datatable redraw;
      (so if you use both Delete and BulkDelete, the not-redrawing thing looks like a bug, not a feature)

To me, it looks like the redraw is more expensive indeed, but it's a reasonable drawback for fixing this bug (and a few other minor bugs in the process).

I think the ones considerably affected by the redraw will be CRUDs paginated by 100+ items per page and/or with a large number of columns. But if redrawing is a problem there, it means it was also a problem at the first page load. And upon search. And upon filtering. And upon reordering. So it's a problem worth fixing.

I say... let's redraw 🥳 I'm still open to being convinced otherwise @pxpm if you feel strongly about this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That being said, I did find a bug with redrawing the table. And I've noticed it's also a bug inside the BulkDelete operation, so we could fix it there too.


If you're on page 33/100, and you delete an item, you'd expect the item to be deleted but stay on page 33/100. But no, the way this PR redraws the table, the user is sent to page 1. Which I think will make it super-frustrating to users.

2020-12-09 11 34 10

I've checked filters, they are kept.
I've checked search, that is kept.
But the page number is not, that is reset upon datatable redraw.

@promatik
Copy link
Contributor

promatik commented Dec 9, 2020

This is an awesome change, because this delete button content is merged once for every crud table entry. So as much we can shorten this file, the better.

By my calculations, this small change can save around 14Kb/request (uncompressed) on a crud table with 20 entries/page.

Thank you for this @martijnb92 🙌

// Remove the row from the datatable
row.remove();
crud.table.row($("#crudTable a[data-route='"+route+"']").parents('tr')).remove().draw();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That being said, I did find a bug with redrawing the table. And I've noticed it's also a bug inside the BulkDelete operation, so we could fix it there too.


If you're on page 33/100, and you delete an item, you'd expect the item to be deleted but stay on page 33/100. But no, the way this PR redraws the table, the user is sent to page 1. Which I think will make it super-frustrating to users.

2020-12-09 11 34 10

I've checked filters, they are kept.
I've checked search, that is kept.
But the page number is not, that is reset upon datatable redraw.

@martijnb92
Copy link
Contributor Author

New commit to return the modal closing, totally missed it..
Also page will now be kept on redraw, datatables has a parameter for that

@martijnb92 martijnb92 changed the title Refactor delete and remove modal call Refactor delete Dec 9, 2020
@tabacitu
Copy link
Member

tabacitu commented Dec 9, 2020

Excellent! Just tested this and it seems to me like all problems are fixed and it works great! Excellent job @martijnb92 .

I'll let @promatik or @pxpm give it one more test, maybe they find something I didn't. But it looks like this is super-close to merging 🥳

@tabacitu tabacitu requested a review from pxpm December 9, 2020 20:13
@scrutinizer-notifier
Copy link

The inspection completed: No new issues

Copy link
Contributor

@pxpm pxpm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems good to me.

Thanks @martijnb92

Copy link
Contributor

@promatik promatik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested with non responsive tables, and some edge cases, like having only one entry on the last page, and everything works as expected.

Thank you for another great improvement @martijnb92 🙌

@tabacitu tabacitu merged commit 4314558 into Laravel-Backpack:master Dec 14, 2020
@soufiene-slimi
Copy link
Contributor

Hi @tabacitu, @promatik, re-drawing the datatable in the show view will fire an error.

image

@martijnb92
Copy link
Contributor Author

Ooch.. I missed the show view..

Indeed crud does not exist in the show view.
We either need to update the script check if crud exists or revert this commit.

@tabacitu
Copy link
Member

tabacitu commented Dec 17, 2020

Ugh! Thanks a lot for the heads-up @soufiene-slimi ! Fixed in #3404, merged and tagged.
A composer update should have it fixed now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants