Want to display the remaining characters on a text field in admin? (based off of maxlength or an override)
also lives here: http://github.com/broderboy/django-admin-remainingcharacters
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | #have your ModelAdmin inherit this to use
class CounterAdmin(admin.ModelAdmin):
counted_fields = ()
#really for textareas
max_lengths = {'abstract': 400,}
class Media:
js = ('js/jquery.js',
'js/jquery.charCount.js',)
def formfield_for_dbfield(self, db_field, **kwargs):
field = super(CounterAdmin, self).formfield_for_dbfield(db_field, **kwargs)
print db_field.name
print self.counted_fields
if db_field.name in self.counted_fields:
try:
len = self.max_lengths[db_field.name]
field.widget.attrs['maxlength'] = len
except: pass
field.widget.attrs['class'] = 'counted ' + field.widget.attrs.get('class','')
return field
"""
jquery,charCount.js
/*
* Character Count Plugin - jQuery plugin
* Dynamic character count for text areas and input fields
* written by Alen Grakalic
* http://cssglobe.com/post/7161/jquery-plugin-simplest-twitterlike-dynamic-character-count-for-textareas
*
* Copyright (c) 2009 Alen Grakalic (http://cssglobe.com)
* Dual licensed under the MIT (MIT-LICENSE.txt)
* and GPL (GPL-LICENSE.txt) licenses.
*
* Built for jQuery library
* http://jquery.com
*
*/
/*
* Modified 2010 Tim Broder for django-admin-charcount
* http://gpowered.net
*/
(function($) {
$.fn.charCount = function(options){
// default configuration properties
var defaults = {
allowed: 140,
warning: 20,
css: 'help',
counterElement: 'p',
cssWarning: 'warning',
cssExceeded: 'exceeded',
counterText: ''
};
var options = $.extend(defaults, options);
function calculate(obj){
var count = $(obj).val().length;
var available = options.allowed - count;
if(available <= options.warning && available >= 0){
$(obj).next().addClass(options.cssWarning);
} else {
$(obj).next().removeClass(options.cssWarning);
}
if(available < 0){
$(obj).next().addClass(options.cssExceeded);
} else {
$(obj).next().removeClass(options.cssExceeded);
}
$(obj).next().html(options.counterText + available);
};
this.each(function() {
$(this).after('<'+ options.counterElement +' class="' + options.css + '">'+ options.counterText +'</'+ options.counterElement +'>');
calculate(this);
$(this).keyup(function(){calculate(this); });
$(this).change(function(){calculate(this)});
});
};
})(jQuery);
/*function init_counters(selector, len){
$(selector).each(function() {
//console.log($(this).attr('maxlength'));
if(len==null){
len = $(this).attr('maxlength');
}
$(this).charCount({
counterText: 'Characters Remaining: ',
allowed: len,
});
});
}*/
$(document).ready(function(){
$(".counted").each(function(){
console.log($(this));
len = $(this).attr('maxlength');
$(this).charCount({
counterText: 'Characters Remaining: ',
allowed: len,
});
});
//init_counters("input[maxlength]", 80);
//init_counters("textarea[maxlength]");
//init_counters("#id_abstract", 400);
});
/*
* <style type="text/css">
form .counter{
}
form .warning{color:#600;}
form .exceeded{color:#e00;}
</style>
*/
"""
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 6 months ago
Comments
Please login first before commenting.