mirror of
https://github.com/inventree/InvenTree.git
synced 2025-05-01 13:06:45 +00:00
Start of custom rendering support based on model
This commit is contained in:
parent
4cf69a5a4c
commit
d411728be6
@ -343,6 +343,22 @@ function initializeRelatedFields(modal, url, fields, options) {
|
||||
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
|
||||
var select = $(modal).find(`#id_${name}`);
|
||||
|
||||
@ -392,15 +408,20 @@ function initializeRelatedFields(modal, url, fields, options) {
|
||||
},
|
||||
},
|
||||
templateResult: function(item, container) {
|
||||
console.log('templateResult:', item);
|
||||
return item.text;
|
||||
},
|
||||
templateSelection: function(item, container) {
|
||||
// Custom formatting for the item
|
||||
console.log("templateSelection:", item);
|
||||
// Custom formatting for the search results
|
||||
if (field.model) {
|
||||
// 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 {
|
||||
// Simply render the 'text' parameter
|
||||
return item.text;
|
||||
@ -408,7 +429,6 @@ function initializeRelatedFields(modal, url, fields, options) {
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
@ -416,18 +436,34 @@ function initializeRelatedFields(modal, url, fields, options) {
|
||||
* Allows custom rendering with access to the entire serialized object.
|
||||
*
|
||||
* arguments:
|
||||
* - name: The name of the field e.g. 'location'
|
||||
* - model: The name of the InvenTree model e.g. 'stockitem'
|
||||
* - data: The JSON data representation of the modal instance (GET request)
|
||||
* - parameters: The field definition (OPTIONS) request
|
||||
* - 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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
|
Loading…
x
Reference in New Issue
Block a user