Last active
December 25, 2019 05:32
-
-
Save owlpro/85d3db617a2ff98a9cfd1436e9ff4cb7 to your computer and use it in GitHub Desktop.
Easy Ajax Edit - laravel (Jquery plugin)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <title>Editable - Example</title> | |
| <script type="text/javascript" src="jquery.min.js"></script> | |
| <script type="text/javascript" src="editable.js"></script> | |
| </head> | |
| <body> | |
| <!-- canEdit Attr just is a seletor --> | |
| <div canEdit table="posts" column="title" column-id="1">Title</div> | |
| <div canEdit table="posts" column="type" column-id="1" type="select" items="{'title1':'value1','title2':'value2'}">Type</div> | |
| <script type="text/javascript"> | |
| $(document).ready(function(){ | |
| $("[canEdit]").editable(); | |
| }); | |
| </script> | |
| </body> | |
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Editable Package | |
| * Author: Mahdi Amiri | |
| * a simple package for easy edit | |
| * Options: | |
| * api [default="/api/editable"] => api for edit contents | |
| * class [default=""] => set a class for customize styles | |
| */ | |
| (function ($) { | |
| $.fn.editable = function (options = {}) { | |
| options.api = options.api ? options.api : "/api/editable"; | |
| options.class = options.class ? options.class : ""; | |
| $(this).css({ | |
| 'cursor':'pointer', | |
| }); | |
| let lastColor = ''; | |
| $(this).hover(function(){ | |
| lastColor = $(this).css('color'); | |
| $(this).css('color','#337ab7'); | |
| }, function(){ | |
| $(this).css('color', lastColor); | |
| }); | |
| let editableTitle = "dblclick for edit" ; | |
| $(this).attr('title', editableTitle); | |
| $(this).unbind('dblclick').dblclick(function (e) { | |
| e.stopPropagation(); | |
| let val = $(this).text().trim(); | |
| let table = $(this).attr('table'); | |
| let column = $(this).attr('column'); | |
| let column_id = $(this).attr('column-id'); | |
| let type = $(this).attr('type'); | |
| if (type === undefined || type === 'text' || type === 'string') { | |
| $(this).html(`<input id="editable_input" class="form-control ${options.class}" value="${val}">`) | |
| .find('#editable_input') | |
| .unbind('dblclick click') | |
| .on('click dblclick', function (e) { | |
| e.stopPropagation(); | |
| }).blur(function () { | |
| save(this); | |
| }).keyup(function (e) { | |
| if (e.keyCode === 13 || e.keyCode === 27) save(this); | |
| }).focus(); | |
| } else if (type === 'select') { | |
| let items = JSON.parse($(this).attr('items')); | |
| console.log(items); | |
| let selectBox = `<select id="editable_input" class="form-control ${options.class}">`; | |
| for (let index in items) { | |
| let value = items[index]; | |
| let selected = ''; | |
| if (val === value.title.trim()) { | |
| selected = 'selected'; | |
| } | |
| selectBox += `<option ${selected} value="${value.id}">${value.title}</option>`; | |
| } | |
| selectBox += `</select>`; | |
| $(this).html(selectBox).find('#editable_input') | |
| .unbind('dblclick click') | |
| .on('click dblclick', function (e) { | |
| e.stopPropagation(); | |
| }).blur(function () { | |
| save(this); | |
| }).focus(); | |
| } else if (type === 'enum') { | |
| let items = JSON.parse($(this).attr('items')); | |
| let selectBox = `<select id="editable_input" class="form-control">`; | |
| for (let index in items) { | |
| let value = items[index]; | |
| let selected = ''; | |
| if (val === value.trim()) { | |
| selected = 'selected'; | |
| } | |
| selectBox += `<option ${selected} value="${value}">${value}</option>`; | |
| } | |
| selectBox += `</select>`; | |
| $(this).html(selectBox).find('#editable_input') | |
| .unbind('dblclick click') | |
| .on('click dblclick', function (e) { | |
| e.stopPropagation(); | |
| }).blur(function () { | |
| save(this); | |
| }).focus(); | |
| } | |
| function save(element) { | |
| let self = $(element); | |
| let newVal = self.val().trim(); | |
| let title = newVal; | |
| if (type === 'select' || type === 'enum') { | |
| title = self.find('option:selected').text(); | |
| } | |
| self.parent().text(title); | |
| if (val !== newVal) { | |
| axios.post(options.api, { | |
| table: table, | |
| column: column, | |
| id: column_id, | |
| val: newVal | |
| }).then((res) => { | |
| console.log(res); | |
| }); | |
| } | |
| } | |
| }); | |
| }; | |
| }(jQuery)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace App\Http\Controllers; | |
| use Illuminate\Http\Request; | |
| class EditableController extends Controller | |
| { | |
| public function edit(Request $r){ | |
| $result = \DB::table($r->table)->where('id', $r->id)->update([ | |
| $r->column => $r->val | |
| ]); | |
| return response()->json($result); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Route::post('/editable', 'EditableController@edit'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment