mirror of
https://github.com/inventree/InvenTree.git
synced 2025-05-02 05:26: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;
|
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,15 +408,20 @@ 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;
|
||||||
@ -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.
|
* 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;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
Loading…
x
Reference in New Issue
Block a user