Consider a default Frappe docType. The layout on screen of the data is not changeable. I…
- User
Consider a default Frappe docType. The layout on screen of the data is not changeable. I would like to add an extra layer, so I can drag and drop the layout of a docType.
- Assistant
To add a drag-and-drop layout feature to a default Frappe docType, you can create a custom script that interacts with the Frappe framework and a third-party library like GridStack.js or jQuery UI's Sortable. Here's a step-by-step guide to help you achieve this:
-
Install a third-party library for drag-and-drop functionality. For this example, we'll use GridStack.js. You can install it using npm:
bashnpm install gridstack.js --save -
Create a custom script in your Frappe app. For instance, create a file named
custom_layout.jsin thepublic/jsdirectory of your app. -
In the
custom_layout.jsfile, import the necessary libraries and create a function to initialize the drag-and-drop layout:javascriptimport gridstack from 'gridstack'; import 'gridstack/dist/gridstack.min.css'; function initCustomLayout(docType, wrapperElement) { const grid = gridstack(wrapperElement, { float: true, cellHeight: 50, verticalMargin: 10, }); // Fetch the fields of the docType frappe.call({ method: 'frappe.client.get_doc_fields', args: { docType: docType, }, callback: (r) => { if (r.message) { const fields = r.message; const gridItems = fields.map((field) => { return { x: 0, y: 0, width: 6, height: 1, content: `<div class="grid-stack-item-content">${field.label}</div>`, }; }); grid.addWidget.apply(grid, gridItems); } }, }); } -
In your docType's form script, call the
initCustomLayoutfunction after the form is loaded:javascriptfrappe.ui.form.on(docType, { refresh: function(frm) { initCustomLayout(docType, frm.fields_dict.$wrapper.get(0)); }, });
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.