Tuesday, November 17, 2020

select 2 select all value and unselect all value js

<label for="tags">{{ trans('cruds.article.fields.tags'}}
      <span class="btn btn-info btn-xs select-all">{{ trans('global.select_all'}}</span>
      <span class="btn btn-info btn-xs deselect-all">{{ trans('global.deselect_all'}}</span></label>
      <select name="tags[]" id="tags" class="form-control select2" multiple="multiple">
          @foreach($tags as $id => $tags)
              <option value="{{ $id }}" {{ (in_array($idold('tags', [])) || isset($article&& $article->tags->contains($id)) ? 'selected' : '' }}>{{ $tags }}</option>
          @endforeach
</select> 

js part : 

$('.select-all').click(function () {
let $select2 = $(this).parent().siblings('.select2')
    $select2.find('option').prop('selected''selected')
    $select2.trigger('change')
  })
  $('.deselect-all').click(function () {
    let $select2 = $(this).parent().siblings('.select2')
    $select2.find('option').prop('selected''')
    $select2.trigger('change')
  })

No comments:

Post a Comment