mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-11-04 07:05:41 +00:00 
			
		
		
		
	Added dropdown menu to the example panel (#5898)
* Fix errors in variable names of examples * Added drop down example * Added drop down example * Fixed picture name * Fixed context variable in purchase order report
This commit is contained in:
		
							
								
								
									
										
											BIN
										
									
								
								docs/docs/assets/images/plugin/panel_with_dropdown.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								docs/docs/assets/images/plugin/panel_with_dropdown.png
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							| 
		 After Width: | Height: | Size: 16 KiB  | 
@@ -187,14 +187,15 @@ class ExamplePanel(PanelMixin, InvenTreePlugin, UrlsMixin):
 | 
			
		||||
    AUTHOR = "Michael"
 | 
			
		||||
    DESCRIPTION = "This plugin passes user input from the panel to the plugin"
 | 
			
		||||
 | 
			
		||||
# Create the panel that will display on every view
 | 
			
		||||
# Create the panel that will display on build detail view
 | 
			
		||||
    def get_custom_panels(self, view, request):
 | 
			
		||||
        panels = []
 | 
			
		||||
        panels.append({
 | 
			
		||||
            'title': 'Example Info',
 | 
			
		||||
            'icon': 'fa-industry',
 | 
			
		||||
            'content_template': 'example_panel/example.html',
 | 
			
		||||
        })
 | 
			
		||||
        if isinstance(view, BuildDetail):
 | 
			
		||||
		panels.append({
 | 
			
		||||
		    'title': 'Example Info',
 | 
			
		||||
		    'icon': 'fa-industry',
 | 
			
		||||
		    'content_template': 'example_panel/example.html',
 | 
			
		||||
		})
 | 
			
		||||
        return panels
 | 
			
		||||
 | 
			
		||||
    def setup_urls(self):
 | 
			
		||||
@@ -221,10 +222,10 @@ Now the html template:
 | 
			
		||||
{% raw %}
 | 
			
		||||
<script>
 | 
			
		||||
async function example_select(){
 | 
			
		||||
    const layernumber = parseInt(document.getElementById("layer_number").value)
 | 
			
		||||
    const layer_number = parseInt(document.getElementById("layer_number").value)
 | 
			
		||||
    const size = document.getElementById("string").value
 | 
			
		||||
    response = inventreeFormDataUpload(url="{% url 'plugin:examplepanel:transfer' '9999' 'Size' %}"
 | 
			
		||||
                                          .replace("9999", layernumber)
 | 
			
		||||
                                          .replace("9999", layer_number)
 | 
			
		||||
                                          .replace("Size", size)
 | 
			
		||||
                                      );
 | 
			
		||||
}
 | 
			
		||||
@@ -272,7 +273,7 @@ the javascript:
 | 
			
		||||
{% raw %}
 | 
			
		||||
<script>
 | 
			
		||||
async function example_select(){
 | 
			
		||||
    const layernumber = parseInt(document.getElementById("layer_number").value)
 | 
			
		||||
    const layer_number = parseInt(document.getElementById("layer_number").value)
 | 
			
		||||
    const size = document.getElementById("string").value
 | 
			
		||||
    const cmd_url="{% url 'plugin:examplepanel:transfer' %}";
 | 
			
		||||
    data = {
 | 
			
		||||
@@ -312,3 +313,42 @@ import json
 | 
			
		||||
The URL and the called function have no parameter names any longer. All data is in the
 | 
			
		||||
request message and can be extracted from this using json.loads. If more data is needed
 | 
			
		||||
just add it to the json container. No further changes are needed. It's really simple :-)
 | 
			
		||||
 | 
			
		||||
#### Populate a drop down field
 | 
			
		||||
 | 
			
		||||
Now we add a dropdown menu and fill it with values from the InvenTree database.
 | 
			
		||||
 | 
			
		||||
{% with id="panel_with_dropwdown", url="plugin/panel_with_dropdown.png", description="Panel with dropdown menu" %}
 | 
			
		||||
{% include "img.html" %}
 | 
			
		||||
{% endwith %}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
```python
 | 
			
		||||
from company.models import Company
 | 
			
		||||
 | 
			
		||||
...
 | 
			
		||||
 | 
			
		||||
    def get_custom_panels(self, view, request):
 | 
			
		||||
        panels = []
 | 
			
		||||
        if isinstance(view, BuildDetail):
 | 
			
		||||
            self.companies=Company.objects.filter(is_supplier=True)
 | 
			
		||||
            panels.append({
 | 
			
		||||
            ...
 | 
			
		||||
```
 | 
			
		||||
Here we create self.companies and fill it with all companies that have the is_supplier flag
 | 
			
		||||
set to true. This is available in the context of the template. A drop down menu can be created
 | 
			
		||||
by looping.
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
```html
 | 
			
		||||
{% raw %}
 | 
			
		||||
<select id="ems">
 | 
			
		||||
    {% for company in plugin.companies %}
 | 
			
		||||
	<option value="{{ company.id }}"> {{ company.name }} </option>
 | 
			
		||||
    {% endfor %}
 | 
			
		||||
</select>
 | 
			
		||||
{% endraw %}
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
The value of the select is the pk of the company. It can simply be added to the
 | 
			
		||||
json container and transferred to the plugin.
 | 
			
		||||
 
 | 
			
		||||
@@ -37,8 +37,7 @@ Each line item have sub variables, as follows:
 | 
			
		||||
| Variable | Description |
 | 
			
		||||
| --- | --- |
 | 
			
		||||
| quantity | The quantity of the part to be ordered |
 | 
			
		||||
| part | The supplier part to be ordered |
 | 
			
		||||
| part | The [supplierpart ](./context_variables.md#supplierpart) object that the build references |
 | 
			
		||||
| part | The [supplierpart ](./context_variables.md#supplierpart) object to be ordered |
 | 
			
		||||
| reference | The reference given in the part of the order |
 | 
			
		||||
| notes | The notes given in the part of the order |
 | 
			
		||||
| target_date | The date when the part should arrive. Each part can have an individual date |
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user