Added companies ajax select2 endpoint

This commit is contained in:
snipe 2017-10-26 16:37:41 -07:00
parent 1fa6228fb7
commit bdb95e4e3d
3 changed files with 71 additions and 0 deletions

View file

@ -141,4 +141,57 @@ class CompaniesController extends Controller
}
}
/**
* Display a formatted JSON response for the select2 menus
*
* @todo: create a transformer for handling these responses
* @author [A. Gianotto] [<snipe@snipe.net>]
* @since [v4.0]
*
* @return \Illuminate\Http\Response
*/
public function selectlist(Request $request)
{
$this->authorize('view', Company::class);
$companies = Company::select([
'companies.id',
'companies.name',
'companies.image',
]);
if ($request->has('search')) {
$companies = $companies->where('companies.name', 'LIKE', '%'.$request->get('search').'%');
}
$companies = $companies->paginate(50);
$companies_array = [];
foreach ($companies as $company) {
$companies_array[] =
[
'id' => (int) $company->id,
'text' => e($company->name),
'image' => ($company->image) ? url('/').'/uploads/companies/'.$company->image : null,
];
}
array_unshift($companies_array, ['id'=> '', 'text'=> trans('general.select_company'), 'image' => null]);
$results = [
'items' => $companies_array,
'pagination' =>
[
'more' => ($companies->currentPage() >= $companies->lastPage()) ? false : true,
'per_page' => $companies->perPage()
],
'total_count' => $companies->total(),
'page' => $companies->currentPage(),
'page_count' => $companies->lastPage()
];
return $results;
}
}

View file

@ -0,0 +1,12 @@
<!-- Company -->
<div id="{{ $fieldname }}" class="form-group{{ $errors->has($fieldname) ? ' has-error' : '' }}">
{{ Form::label($fieldname, $translated_name, array('class' => 'col-md-3 control-label')) }}
<div class="col-md-7">
<select class="js-data-ajax" data-endpoint="companies" name="{{ $fieldname }}" style="width: 100%" id="company_select">
</select>
</div>
{!! $errors->first($fieldname, '<div class="col-md-8 col-md-offset-3"><span class="alert-msg"><i class="fa fa-times"></i> :message</span></div>') !!}
</div>

View file

@ -63,6 +63,12 @@ Route::group(['prefix' => 'v1','namespace' => 'Api'], function () {
/*--- Companies API ---*/
Route::get( 'companies/selectlist', [
'as' => 'companies.selectlist',
'uses' => 'CompaniesController@selectlist'
]);
Route::resource('companies', 'CompaniesController',
[
'names' =>