Added snazzy rich user selection menu

TODO:
- Abstract this out so it can be used by other select2 menus
- Write a select2 transformer to standardize output
This commit is contained in:
snipe 2017-10-24 19:24:35 -07:00
parent c605984db0
commit 17d58d9cc5
5 changed files with 166 additions and 20 deletions

View file

@ -110,6 +110,78 @@ class UsersController extends Controller
} }
/**
* Display a listing of the resource.
*
* @author [A. Gianotto] [<snipe@snipe.net>]
* @since [v4.0]
*
* @return \Illuminate\Http\Response
*/
public function selectlist(Request $request)
{
$this->authorize('view', User::class);
$users = User::select(
[
'users.id',
'users.employee_num',
'users.first_name',
'users.last_name',
'users.gravatar',
'users.avatar',
'users.email',
]
);
$users = Company::scopeCompanyables($users);
if ($request->has('search')) {
$users = $users->where('first_name', '=', '%'.$request->get('search').'%')
->orWhere('last_name', 'LIKE', '%'.$request->get('search').'%')
->orWhere('username', 'LIKE', '%'.$request->get('search').'%')
->orWhere('employee_num', '=', '%'.$request->get('search').'%');
}
$users = $users->orderBy('last_name', 'asc');
$users = $users->paginate(50);
$users_array = [];
foreach ($users as $user) {
$name_str = '';
if ($user->last_name!='') {
$name_str .= e($user->last_name).', ';
}
$name_str .= e($user->first_name);
if ($user->employee_num!='') {
$name_str .= ' (#'.e($user->employee_num).')';
}
$users_array[] =
[
'id' => $user->id,
'text' => $name_str,
'image' => ($user->present()->gravatar) ? $user->present()->gravatar : null,
];
}
$results = [
'items' => $users_array,
'pagination' =>
[
'more' => ($users->currentPage() >= $users->lastPage()) ? false : true,
'per_page' => $users->perPage()
],
'total_count' => $users->total(),
'page' => $users->currentPage(),
'page_count' => $users->lastPage()
];
return $results;
}
/** /**
* Store a newly created resource in storage. * Store a newly created resource in storage.
* *

BIN
public/js/dist/all.js vendored

Binary file not shown.

View file

@ -27,7 +27,7 @@
<div class="box-body"> <div class="box-body">
{{csrf_field()}} {{csrf_field()}}
@if ($asset->model->name) @if ($asset->model->name)
<!-- Asset name --> <!-- Model name -->
<div class="form-group {{ $errors->has('name') ? 'error' : '' }}"> <div class="form-group {{ $errors->has('name') ? 'error' : '' }}">
{{ Form::label('name', trans('admin/hardware/form.model'), array('class' => 'col-md-3 control-label')) }} {{ Form::label('name', trans('admin/hardware/form.model'), array('class' => 'col-md-3 control-label')) }}
<div class="col-md-8"> <div class="col-md-8">
@ -45,14 +45,23 @@
</div> </div>
</div> </div>
<!-- User -->
<div id="assigned_user" class="form-group{{ $errors->has('assigned_to') ? ' has-error' : '' }}"> <div id="assigned_user" class="form-group{{ $errors->has('assigned_to') ? ' has-error' : '' }}">
{{ Form::label('assigned_user', trans('admin/hardware/form.checkout_to'), array('class' => 'col-md-3 control-label')) }} {{ Form::label('assigned_user', trans('admin/hardware/form.checkout_to'), array('class' => 'col-md-3 control-label')) }}
<div class="col-md-7 required"> <div class="col-md-7 required">
{{ Form::select('assigned_user', $users_list , Input::old('assigned_user', $asset->assigned_type == 'App\Models\User' ? $asset->assigned_to : 0), array('class'=>'select2', 'id'=>'assigned_user_select', 'style'=>'width:100%')) }} <select class="js-data-user-ajax" name="assigned_user" style="width: 350px;">
<option value="">{{ trans('general.select_user') }}</option>
</select>
</div>
{!! $errors->first('assigned_user', '<span class="alert-msg"><i class="fa fa-times"></i> :message</span>') !!} {!! $errors->first('assigned_user', '<span class="alert-msg"><i class="fa fa-times"></i> :message</span>') !!}
</div>
<div class="col-md-1 col-sm-1 text-left"> <div class="col-md-1 col-sm-1 text-left">
@can('create', \App\Models\User::class) @can('create', \App\Models\User::class)
<a href='{{ route('modal.user') }}' data-toggle="modal" data-target="#createModal" data-dependency="user" data-select='assigned_user_select' class="btn btn-sm btn-default">New</a> <a href='{{ route('modal.user') }}' data-toggle="modal" data-target="#createModal" data-dependency="user" data-select='assigned_user_select' class="btn btn-sm btn-default">New</a>

View file

@ -663,7 +663,63 @@
var datepicker = $.fn.datepicker.noConflict(); // return $.fn.datepicker to previously assigned value var datepicker = $.fn.datepicker.noConflict(); // return $.fn.datepicker to previously assigned value
$.fn.bootstrapDP = datepicker; $.fn.bootstrapDP = datepicker;
$('.datepicker').datepicker(); $('.datepicker').datepicker();
}) });
// Crazy select2 rich dropdowns with images!
$(".js-data-user-ajax").select2({
ajax: {
url: '{{ route('api.users.selectlist') }}',
dataType: 'json',
delay: 250,
headers: {
"X-Requested-With": 'XMLHttpRequest',
"X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr('content')
},
data: function (params) {
var data = {
search: params.term,
page: params.page || 1
};
return data;
},
processResults: function (data, params) {
params.page = params.page || 1;
var answer = {
results: data.items,
pagination: {
more: "true" //(params.page < data.page_count)
}
};
return answer;
},
cache: true
},
escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
templateResult: formatUserlist,
templateSelection: formatUserSelection
});
function formatUserlist (userlist) {
var loading_markup = '<i class="fa fa-spinner fa-spin" aria-hidden="true"></i> Loading...';
if (userlist.loading) {
return loading_markup;
}
var markup = "<div class='clearfix'>" ;
markup +="<div class='pull-left' style='padding-right: 10px;'>";
markup += "<img src='" + userlist.image + "' style='max-height: 20px'></div>";
markup += "<div>" + userlist.text + "</div>";
markup += "</div>";
return markup;
}
function formatUserSelection (userlist) {
return userlist.text;
}
</script> </script>

View file

@ -532,20 +532,7 @@ Route::group(['prefix' => 'v1','namespace' => 'Api'], function () {
/*--- Users API ---*/ /*--- Users API ---*/
Route::resource('users', 'UsersController',
[
'names' =>
[
'index' => 'api.users.index',
'show' => 'api.users.show',
'store' => 'api.users.store',
'update' => 'api.users.update',
'destroy' => 'api.users.destroy'
],
'except' => ['create', 'edit'],
'parameters' => ['user' => 'user_id']
]
); // Users resource
Route::group([ 'prefix' => 'users' ], function () { Route::group([ 'prefix' => 'users' ], function () {
@ -563,6 +550,13 @@ Route::group(['prefix' => 'v1','namespace' => 'Api'], function () {
] ]
); );
Route::get('selectlist',
[
'as' => 'api.users.selectlist',
'uses' => 'UsersController@selectList'
]
);
Route::get('{user}/assets', Route::get('{user}/assets',
[ [
'as' => 'api.users.assetlist', 'as' => 'api.users.assetlist',
@ -578,6 +572,21 @@ Route::group(['prefix' => 'v1','namespace' => 'Api'], function () {
); );
}); // Users group }); // Users group
Route::resource('users', 'UsersController',
[
'names' =>
[
'index' => 'api.users.index',
'show' => 'api.users.show',
'store' => 'api.users.store',
'update' => 'api.users.update',
'destroy' => 'api.users.destroy'
],
'except' => ['create', 'edit'],
'parameters' => ['user' => 'user_id']
]
); // Users resource
Route::get( Route::get(
'reports/activity', 'reports/activity',