Created
January 18, 2017 14:15
-
-
Save meysampg/bc656ebaa0c463b4eceb5529103e5828 to your computer and use it in GitHub Desktop.
Convert a table field to input
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
(function($) { | |
"use strict"; | |
$.fn.translationInput = function(options) { | |
options = $.extend({ | |
defaultClass: ".translationField", | |
defaultRoute: "update" | |
}, options); | |
return this.each(function(i, v) { | |
var index = $(v).data('key'); | |
$(v).find(options.defaultClass).each(function (i, v) { | |
var tableRow = $(v); | |
tableRow.on('click', function (e) { | |
var oldValue = tableRow.text(); | |
var translationInput = $( | |
'<input>', | |
{ | |
value: oldValue, | |
type: 'text', | |
class: 'form-control', | |
blur: function () { | |
tableRow.text(this.value); | |
if (oldValue != this.value) { | |
$.ajax({ | |
url: options.defaultRoute, | |
data: { | |
id: index, | |
value: this.value | |
}, | |
type: 'POST' | |
}); | |
} | |
}, | |
keyup: function (e) { | |
if (e.which == 13 || e.which == 27) { | |
translationInput.blur(); | |
} | |
} | |
} | |
).appendTo(tableRow.empty()).focus(); | |
}) | |
}); | |
}); | |
} | |
} (jQuery)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example: https://jsfiddle.net/meysampg/0jv6rb5g