Skip to content

Instantly share code, notes, and snippets.

@meysampg
Created January 18, 2017 14:15
Show Gist options
  • Save meysampg/bc656ebaa0c463b4eceb5529103e5828 to your computer and use it in GitHub Desktop.
Save meysampg/bc656ebaa0c463b4eceb5529103e5828 to your computer and use it in GitHub Desktop.
Convert a table field to input
(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));
@meysampg
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment