mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-11-03 22:55:43 +00:00 
			
		
		
		
	Start of custom rendering support based on model
This commit is contained in:
		@@ -343,6 +343,22 @@ function initializeRelatedFields(modal, url, fields, options) {
 | 
				
			|||||||
            continue;
 | 
					            continue;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        initializeRelatedField(modal, name, field, options);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * Initializea single related-field
 | 
				
			||||||
 | 
					 * 
 | 
				
			||||||
 | 
					 * argument:
 | 
				
			||||||
 | 
					 * - modal: DOM identifier for the modal window
 | 
				
			||||||
 | 
					 * - name: name of the field e.g. 'location'
 | 
				
			||||||
 | 
					 * - field: Field definition from the OPTIONS request
 | 
				
			||||||
 | 
					 * - options: Original options object provided by the client
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					function initializeRelatedField(modal, name, field, options) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Find the select element and attach a select2 to it
 | 
					    // Find the select element and attach a select2 to it
 | 
				
			||||||
    var select = $(modal).find(`#id_${name}`);
 | 
					    var select = $(modal).find(`#id_${name}`);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -392,22 +408,26 @@ function initializeRelatedFields(modal, url, fields, options) {
 | 
				
			|||||||
            },
 | 
					            },
 | 
				
			||||||
        },
 | 
					        },
 | 
				
			||||||
        templateResult: function(item, container) {
 | 
					        templateResult: function(item, container) {
 | 
				
			||||||
                console.log('templateResult:', item);
 | 
					            // Custom formatting for the search results
 | 
				
			||||||
                return item.text;
 | 
					 | 
				
			||||||
            },
 | 
					 | 
				
			||||||
            templateSelection: function(item, container) {
 | 
					 | 
				
			||||||
                // Custom formatting for the item
 | 
					 | 
				
			||||||
                console.log("templateSelection:", item);
 | 
					 | 
				
			||||||
            if (field.model) {
 | 
					            if (field.model) {
 | 
				
			||||||
                // If the 'model' is specified, hand it off to the custom model render
 | 
					                // If the 'model' is specified, hand it off to the custom model render
 | 
				
			||||||
                    return renderModelData(field.model, item, field, options);
 | 
					                return renderModelData(name, field.model, item, field, options);
 | 
				
			||||||
 | 
					            } else {
 | 
				
			||||||
 | 
					                // Simply render the 'text' parameter
 | 
				
			||||||
 | 
					                return item.text;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        templateSelection: function(item, container) {
 | 
				
			||||||
 | 
					            // Custom formatting for selected item
 | 
				
			||||||
 | 
					            if (field.model) {
 | 
				
			||||||
 | 
					                // If the 'model' is specified, hand it off to the custom model render
 | 
				
			||||||
 | 
					                return renderModelData(name, field.model, item, field, options);
 | 
				
			||||||
            } else {
 | 
					            } else {
 | 
				
			||||||
                // Simply render the 'text' parameter
 | 
					                // Simply render the 'text' parameter
 | 
				
			||||||
                return item.text;
 | 
					                return item.text;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -416,17 +436,33 @@ function initializeRelatedFields(modal, url, fields, options) {
 | 
				
			|||||||
 * Allows custom rendering with access to the entire serialized object.
 | 
					 * Allows custom rendering with access to the entire serialized object.
 | 
				
			||||||
 * 
 | 
					 * 
 | 
				
			||||||
 * arguments:
 | 
					 * arguments:
 | 
				
			||||||
 | 
					 * - name: The name of the field e.g. 'location'
 | 
				
			||||||
 * - model: The name of the InvenTree model e.g. 'stockitem'
 | 
					 * - model: The name of the InvenTree model e.g. 'stockitem'
 | 
				
			||||||
 * - data: The JSON data representation of the modal instance (GET request)
 | 
					 * - data: The JSON data representation of the modal instance (GET request)
 | 
				
			||||||
 * - parameters: The field definition (OPTIONS) request
 | 
					 * - parameters: The field definition (OPTIONS) request
 | 
				
			||||||
 * - options: Other options provided at time of modal creation by the client
 | 
					 * - options: Other options provided at time of modal creation by the client
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
function renderModelData(model, data, paramaters, options) {
 | 
					function renderModelData(name, model, data, paramaters, options) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    console.log(model, '->', data);
 | 
					    // TODO: Implement this function for various models
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // TODO: Implement?
 | 
					    var html = null;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    switch (model) {
 | 
				
			||||||
 | 
					        case 'company':
 | 
				
			||||||
 | 
					            html = `<span>${data.name}</span> - <i>${data.description}</i>`;
 | 
				
			||||||
 | 
					        default:
 | 
				
			||||||
 | 
					            break;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (html != null) {
 | 
				
			||||||
 | 
					        // Render HTML to an object
 | 
				
			||||||
 | 
					        var $state = $(html);
 | 
				
			||||||
 | 
					        return $state;
 | 
				
			||||||
 | 
					    } else {
 | 
				
			||||||
 | 
					        // Simple text rendering
 | 
				
			||||||
        return data.text;
 | 
					        return data.text;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user