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

Laravel / PHP - Items being deleted after selecting another #5037

Closed
5 of 16 tasks
garbinmarcelo opened this issue Sep 12, 2017 · 6 comments
Closed
5 of 16 tasks

Laravel / PHP - Items being deleted after selecting another #5037

garbinmarcelo opened this issue Sep 12, 2017 · 6 comments

Comments

@garbinmarcelo
Copy link

Prerequisites

  • I have searched for similar issues in both open and closed tickets and cannot find a duplicate
  • The issue still exists against the latest master branch of Select2
  • This is not a usage question (Those should be directed to the community)
  • I have attempted to find the simplest possible steps to reproduce the issue
  • I have included a failing test as a pull request (Optional)

Steps to reproduce the issue

  1. Populate select with data coming from the database;
  2. HTML coded: {!! Form::select('id_cargo[]', $cargos, (isset($usuarioCargos))? $usuarioCargos : null, ['id' => 'id_cargo', 'class' => 'form-control select2', 'multiple' => 'multiple']) !!}
  3. When you delete an item from the list, or select a new item, the old items are deleted.

PS.: I'm not using ajax requests. I tried to use loop, and put the id's manually, the problem persists. Other functions of select2 work normally, but I have this problem in changing already populated data coming from the database. Here's the demo below.

Alt Text
Demo with data coming from the database, the first item is deleted, then the second is also removed. After that, I test with the data contained in the front end, add two items, I delete the first and the second remains, this would be the ideal behavior to be used also with the data coming from the database.
Note: My list has more than 2,000 items, but I believe this should not interfere.

Expected behavior and actual behavior

I would like to keep the data after selecting another, or after deleting a single item. This works if the data isn't populated from the database, only on the front-end.

Environment

Browsers

  • Google Chrome
  • Mozilla Firefox
  • Internet Explorer

Operating System

  • Windows
  • Mac OS X
  • Linux
  • Mobile

Libraries

  • jQuery version: 3.2.1
  • Select2 version: 4.0.3

Isolating the problem

  • This bug happens on the examples page
  • The bug happens consistently across all tested browsers
  • This bug happens when using Select2 without other plugins
  • I can reproduce this bug in a jsbin
@alexweissman
Copy link
Contributor

Could we please see your client-side (Javascript) code?

FYI, if you're going to be offering a list of 2000+ items, I'd strongly recommend that you consider using AJAX and pagination rather than rendering all of the items in the initial page load.

@garbinmarcelo
Copy link
Author

Thanks for the feedback @alexweissman.
So, my JS code is simple but functional, see:

$(document).ready(function() {
    $('select.select2').select2({
        locale: 'pt-BR',
        placeholder: 'Selecionar item',
        width: '100%'
    });
});

I will consider doing in ajax, how would that pagination you cited? does it automatically or do I need some code? If you can give me an example, I'm grateful.

@alexweissman
Copy link
Contributor

alexweissman commented Sep 12, 2017

Pagination isn't fully documented yet, but in the meantime there is an example here: https://select2.org/data-sources/ajax#additional-examples

Of course, you need to make sure that your remote data source can perform filtering and pagination on the server side. If you're using Laravel 5.5, they have a new feature that makes it easier to set up JSON APIs: https://laravel.com/docs/5.5/eloquent-resources

@alexweissman
Copy link
Contributor

Pagination is now documented: https://select2.org/data-sources/ajax#pagination

@alexweissman
Copy link
Contributor

I'm closing this because it sounds like you may have found another way to work around this. I'm not sure what's causing the problem with the non-AJAX data source, but it is most likely a duplicate of another open issue. For example, #3772 and #3773.

@garbinmarcelo
Copy link
Author

I have not found yet, if I find I'll come back with an answer. Thanks

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

No branches or pull requests

2 participants