Created
November 11, 2014 17:21
-
-
Save rjswenson/07b9ee68399971070f65 to your computer and use it in GitHub Desktop.
Active Admin in column updates!
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
//= require active_admin/base | |
var admin = { | |
init: function(){ | |
admin.set_admin_editable_events(); | |
}, | |
set_admin_editable_events: function(){ | |
$(".admin-editable").on("keypress", function(e){ | |
if ( e.keyCode==27 ) | |
$( e.currentTarget ).hide(); | |
if ( e.keyCode==13 ){ | |
var path = $( e.currentTarget ).attr("data-path"); | |
var attr = $( e.currentTarget ).attr("data-attr"); | |
var resource_id = $( e.currentTarget ).attr("data-resource-id"); | |
var val = $( e.currentTarget ).val(); | |
val = $.trim(val) | |
if (val.length==0) | |
val = " "; | |
$("div#"+$( e.currentTarget ).attr("id")).html(val); | |
$( e.currentTarget ).hide(); | |
var payload = {} | |
resource_class = path.slice(0,-1) // e.g. path = meters, resource_class = meter | |
payload[resource_class] = {}; | |
payload[resource_class][attr] = val; | |
$.ajax({ | |
type: 'PUT', | |
url: "/admin/"+path+"/"+resource_id, | |
data: payload | |
}); | |
} | |
}); | |
$(".admin-editable").on("blur", function(e){ | |
$( e.currentTarget ).hide(); | |
}); | |
}, | |
editable_text_column_do: function(el){ | |
var input = "input#"+$(el).attr("id") | |
$(input).width( $(el).width()+4 ).height( $(el).height()+4 ); | |
$(input).css({top: ( $(el).offset().top-2 ), left: ( $(el).offset().left-2 ), position:'absolute'}); | |
val = $.trim( $(el).html() ); | |
if (val==" ") | |
val = ""; | |
$(input).val( val ); | |
$(input).show(); | |
$(input).focus(); | |
} | |
} | |
$( document ).ready(function() { | |
admin.init(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment