From f4490b14f86fdef9ce319d0ea3337c30c4683ca3 Mon Sep 17 00:00:00 2001 From: snipe Date: Sat, 14 May 2016 14:06:59 -0700 Subject: [PATCH] [Feature] Re-order custom fields (drag and drop) --- .../Controllers/CustomFieldsController.php | 23 +++++++++++++++++++ app/Http/routes.php | 10 ++++++++ resources/views/custom_fields/show.blade.php | 20 ++++++++++------ 3 files changed, 46 insertions(+), 7 deletions(-) diff --git a/app/Http/Controllers/CustomFieldsController.php b/app/Http/Controllers/CustomFieldsController.php index eb67fa819e..c966b92f6c 100644 --- a/app/Http/Controllers/CustomFieldsController.php +++ b/app/Http/Controllers/CustomFieldsController.php @@ -250,4 +250,27 @@ class CustomFieldsController extends Controller return redirect()->route("admin.custom_fields.index")->with("error", trans('admin/custom_fields/message.fieldset.delete.in_use')); //->with("models",$models); } } + + + /** + * Reorder the custom fields within a fieldset + * + * @author [Brady Wetherington] [] + * @param int $id + * @since [v3.0] + * @return Array + */ + public function postReorder($id) { + $fieldset=CustomFieldset::find($id); + $fields = array(); + + $items = Input::get('item'); + foreach ($fieldset->fields as $field) { + $value = array_shift($items); + $fields[$field->id] = ['required' => $field->pivot->required, 'order' => $value]; + } + return $fieldset->fields()->sync($fields); + + + } } diff --git a/app/Http/routes.php b/app/Http/routes.php index 76010b7a5b..1e3ee50420 100755 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -148,6 +148,16 @@ Route::group([ 'prefix' => 'api', 'middleware' => 'auth' ], function () { Route::resource('/', 'SuppliersController'); }); + + /*-- Custom fields API --*/ + Route::group([ 'prefix' => 'custom_fields' ], function () { + Route::post( + '{fieldsetID}/order', + [ 'as' => 'api.customfields.order', 'uses' => 'CustomFieldsController@postReorder' ] + ); + }); + + }); /* diff --git a/resources/views/custom_fields/show.blade.php b/resources/views/custom_fields/show.blade.php index 446c5a6444..151d4d752b 100644 --- a/resources/views/custom_fields/show.blade.php +++ b/resources/views/custom_fields/show.blade.php @@ -41,7 +41,7 @@ - {{ Form::open(['route' => ["admin.custom_fields.associate",$custom_fieldset->id], 'class'=>'form-horizontal']) }} + {{ Form::open(['route' => ["admin.custom_fields.associate",$custom_fieldset->id], 'class'=>'form-horizontal', 'id' => 'ordering']) }} {{ Form::checkbox("required","on") }} {{ trans('admin/custom_fields/general.required') }} {{ Form::text("order",$maxid)}} @@ -52,8 +52,8 @@ - @foreach($custom_fieldset->fields AS $field) - + @foreach($custom_fieldset->fields as $field) + @@ -94,11 +94,17 @@ $('td.index', ui.item.parent()).each(function (i) { $(this).html(i + 1); $.ajax({ - url: "{{config('app.url') }}/api/custom_fields/order", - success: function(data) { - // do some stuff here + method: "POST", + url: "{{ route('api.customfields.order', $custom_fieldset->id) }}", + data: $("#sort tbody").sortable('serialize', { + // key: + }), - } + success: function(data) { + //console.log('ajax fired'); + // do some stuff here + + } }); }); };