--- /dev/null
+<?php
+
+namespace Cubist\Backpack\Http\Controllers;
+
+use Cubist\Backpack\Http\Controllers\Base\XSendFileController;
+use Illuminate\Support\Facades\Auth;
+
+class CubistSelect2Controller extends XSendFileController
+{
+ public function search($hash)
+ {
+ if (!Auth::check()) {
+ return response(null)->setStatusCode('401');
+ }
+ if (!can('edition')) {
+ return response(null)->setStatusCode('403');
+ }
+ $data = json_decode(storage_path('select2/' . $hash . '.json'));
+ $res = [];
+ return response()->json($res);
+ }
+}
protected $_options = [];
protected $_options_aliases = [];
protected $_filterType = 'dropdown';
+ protected $_ajax = false;
public function getDefaultAttributes()
{
- return array_merge(parent::getDefaultAttributes(), ['options' => $this->getOptions(), 'options_aliases' => $this->_options_aliases, 'allows_null' => $this->_allowNull, 'allows_multiple' => $this->_multiple]);
+ return array_merge(parent::getDefaultAttributes(), ['options' => $this->getOptions(), 'options_aliases' => $this->_options_aliases, 'allows_null' => $this->_allowNull, 'allows_multiple' => $this->_multiple, 'ajax' => $this->_ajax]);
}
protected function _postSetAttributes()
if ($this->getAttribute('default') !== null && $this->getAttribute('allows_null')) {
$this->setAttribute('allows_null', false);
}
+ if ($this->getAttribute('ajax', false)) {
+ $results = [];
+ foreach ($this->getOptions() as $k => $v) {
+ $results[] = ['id' => $k, 'text' => $v];
+ }
+ $data = ['results' => $results, 'pagination' => ['more' => false]];
+ $hash = hash('sha256', print_r($data, true));
+ $name = $hash . '.json';
+ $dir = \Cubist\Util\Files\Files::mkdir(public_path('select2'));
+ $file = $dir . '/' . $name;
+ if (!file_exists($file)) {
+ file_put_contents($file, json_encode($data));
+ }
+ $this->setAttribute('ajax', $name);
+ }
}
// public function getDatabaseLength()
return false;
}
}
+
+ $ajax=isset($field['ajax']) && $field['ajax'];
@endphp
<!-- select2 from array -->
@include('crud::fields.inc.wrapper_start')
<label>{!! $field['label'] !!}</label>
@if (isset($field['allows_multiple']) && $field['allows_multiple']==true)
- <input type="hidden" name="{{ $field['name'] }}" value="" />
+ <input type="hidden" name="{{ $field['name'] }}" value=""/>
@endif
<select
name="{{ $field['name'] }}@if (isset($field['allows_multiple']) && $field['allows_multiple']==true)[]@endif"
style="width: 100%"
@include('crud::fields.inc.attributes', ['default_class' => 'form-control select2_from_array'])
@if (isset($field['allows_multiple']) && $field['allows_multiple']==true)multiple @endif
+ @if(isset($field['ajax']) && $field['ajax']) data-ajax="{{$field['ajax']}}" @endif
>
-
@if (isset($field['allows_null']) && $field['allows_null']==true)
@if($is_null)
<option value="" selected>-</option>
@endphp
@if($selected)
<option value="{{ $key }}" selected>{{ $value }}</option>
- @else
- <option value="{{ $key }}">{{ $value }}</option>
+ @else if(!$ajax)
+ <option value="{{ $key }}">{{ $value }}</option>
@endif
@endforeach
@endif
+
</select>
{{-- HINT --}}
if ($(obj).closest('.item.sample').length > 0) {
return;
}
+ var options = {
+ theme: "bootstrap"
+ };
+ if ($(this).is(['data-ajax'])) {
+ options.ajax = {url: $(this).data('ajax'), dataType: 'json'};
+ }
if (!$(obj).hasClass("select2-hidden-accessible")) {
- $(obj).select2({
- theme: "bootstrap"
- });
+ $(obj).select2(options);
}
});
}
--- /dev/null
+<?php
+
+Route::group([
+ //'prefix' => config('backpack.base.route_prefix', 'admin'),
+ 'middleware' => ['web', config('backpack.base.middleware_key', 'admin')],
+], function () { // custom admin routes
+ Route::any('storage/{hash?}', '\Cubist\Backpack\Http\Controllers\CubistSelect2Controller@search')->where(['hash' => '.*']);
+});