Login

FilterManager

Author:
sergejdergatsjev
Posted:
November 25, 2008
Language:
JavaScript
Version:
Not specified
Score:
0 (after 0 ratings)

Save a filter in admin app

Example:

    http://localhost:8000/admin/org/registrationprofile/?title__exact=Dr&ot=asc&o=4&speciality__exact=gynaecology

you can save this path org/registrationprofile/?title__exact=Dr&ot=asc&o=4&speciality__exact=gynaecology
as your filter with name like "DrGynecologySortedCyty" and then select this filter  from selectbox

include JavaScript file FilterManager.js and jQuery in all admin templates.

=== Install ===

1. Add this in header of base.html for contrib.admin

You can download this files from

    http://code.google.com/p/jqueryjs/downloads/detail?name=jquery-1.2.6.min.js
  1. Change path for your model that you will save in javaScript file FilterManager.js

Example: /admin/org/registrationprofile/

  1. Add models and views. see code
  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
var FilterManager = window.FilterManager || {};

FilterManager.createElement = function(parent){
    template = "<div class=\"filter\" id=\"filter\"><fieldset><legend>Select filter</legend><select id=\"select" +
        "\"></select><br/><a class=\"addlink\" id=\"button"   + 
        "\">SAVE NEW FILTER</a>&nbsp;&nbsp;&nbsp;<span  id=\"out" + 
        "\"></span></fieldset></div>";
    $(template).appendTo(parent);
    $("#select").load("/filter/options/ #options option");
}


FilterManager.attachEvents = function(){
	$("#button").bind("click", function(e){
                var currentFilter = window.location.pathname + window.location.search;
                //var filterName = window.prompt("Filter name pleas...", "New filter name");

				var options = {"modal":"true" , overlay: {
                                                opacity: 0.5,
                                                background: "black"
                                                },
								buttons: { 
        							"Ok": function() {
												var filterName = $("#filtername").attr("value"); 
                   								$.post("/filter/add/", { "filter": currentFilter, "name": filterName },
                          							function(data){
                                							$("#out").text(data).fadeOut(7000);
                                							$("select").load("/filter/options/ #options option");
															dialog.dialog("close");
                            					});

       								}, 
        							"Cancel": function() { 
            							$(this).dialog("close"); 
       								 }
                    			}
				}

            	var dialog = $("<div id=\"progress\" class=\"flora\" title=\"New filter\">" + 
								"<label for=\"filtername\">Filter name pleas...</label>" + 
								"<br/><input type=\"text\" value=\"New filter name\" id=\"filtername\"/></div>").dialog(options);

				 $("#filtername").focus(function() {
             		if ( $(this).val() == "New filter name") $(this).val('');
            	}); 	
    
                                
            });

			var u = document.location;

            $("#select").bind("change", function(e){
                var url = $("#select").val();
				var port = "";
				if(u.port != ""){
					port = ":" + u.port;
				}
                window.document.location = u.protocol + "//" + u.hostname + port + url; 
             });
}

// ********************* usage ******************************


 $(document).ready(function() {
	//do stuff when DOM is ready jQury

    if(document.location.pathname == "/admin/org/registrationprofile/"){
			FilterManager.createElement("#toolbar");
			FilterManager.attachEvents();
	}
		
 });


//******************* in view.py of django app //*****************



@login_required
def filter(request, action=None,           
             template_name='www/filter.html',
             extra_context=None):    
    
    data = {}  
       
    
    if extra_context is None:
        extra_context = {}    
        
    if action == 'options':        
        data["filters"] = Filter.objects.all() 
        
        
    if action == 'add':
        f = FilterForm(request.POST)
        f.save()
        data["response"] = "Ok, filter: " + request.POST.get("filter") + " with name " + request.POST.get("name") + "is added."
        #data["response"] = "Ok, filter: " + request.POST.get("filter") + " with name " + request.POST.get("name") + "is added."
   
    
        
    context = RequestContext(request)
    for key, value in extra_context.items():
        context[key] = callable(value) and value() or value
        
    return render_to_response(template_name, data, context_instance=RequestContext(request))


//*********** in model.py ************************/


class Filter(models.Model):
    filter = models.CharField(_('Filter'), max_length=250, primary_key=True)
    name = models.CharField(_('Name'), max_length=250, blank=False)
    
    def __unicode__(self):
        return u" %(name)s %(filter)s  " % {'filter':self.filter, 
                                                'name':self.name}  

More like this

  1. Django Collapsed Stacked Inlines by applecat 3 years ago
  2. Django Collapsed Stacked Inlines by mkarajohn 5 years, 2 months ago
  3. Dynamically adding forms to a formset. OOP version. by halfnibble 10 years, 10 months ago
  4. Convert multiple select for m2m to multiple checkboxes in django admin form by abidibo 12 years, 11 months ago
  5. Django admin inline ordering - javascript only implementation by ojhilt 13 years, 3 months ago

Comments

Please login first before commenting.