Variable-Length Line Item Edits: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
|||
| Line 54: | Line 54: | ||
=== Binding handlers and buttons === | === Binding handlers and buttons === | ||
Default binding handlers and buttons: | |||
<syntaxhighlight lang="javascript" highlight=" | <syntaxhighlight lang="javascript" highlight="2"> | ||
$(document).ready(function() { | |||
$('.line-items-module').lineitemModule('init'); | |||
}); | |||
</syntaxhighlight> | |||
The defaults can be overridden by passing in custom configuration in the 2nd argument: | |||
<syntaxhighlight lang="javascript" highlight="3-13"> | |||
$(document).ready(function() { | |||
$('.line-items-module').lineitemModule('init', { | |||
edit: { | |||
selector: '.custom-selector', | |||
event: 'click', | |||
uri: '/path/to/custom/edit_ajax.php' | |||
}, | }, | ||
del: { | |||
uri: '/path/to/custom/del_ajax.php' | |||
handler: function(evt, options) { | |||
console.log('Custom handler called.'); | |||
} | |||
} | |||
}); | |||
}); | |||
} | |||
} | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Revision as of 16:33, 30 October 2013
Overview
Describes how to add modules to CMS edit pages where variable-length arrays of objects can be added to a content record.
Markup
Cross-site request forgery
- A CSRF token is created and saved as a session variable.
- The value of this variable is saved in a hidden element on the page:
#csrf-tokenwhere it will then be available in jQuery.
Line item listings
- Outer container has
.line-items-containerclass.- This element does not get modified by jQuery.
- The class is used as a selector in jQuery to target the parent of the individual line items.
- Each individual line item element has
.line-itemclass.- The class name is used as a selector in jQuery to assign edit handlers to the individual line items.
- When iterating through the line item objects in PHP, they should be rendered using a separate PHP include file. This way, that line item include file can be shared between the front-end PHP template and the AJAX script that updates page content.
- The
.line-itemelement must have the following attributes defined:data-id: Record id of the line item in the database.data-type: PHP class name of the object that can fetch and save the line item data to and from the database.
- New item button:
<button class="add-line-btn" title="add new [object_name]">add new [object_name]</button>
Edit form
- The edit form markup will include the
.line-itemelement. - The flattened display of the line item will be replaced in its entirety with the edit form markup.
- The edit form will be replaced in its entirety with the updated flattened display of the line item once editing has been completed.
CSS
There is nothing critical here to the operation of the line item edits.
The line item elements can be table rows or divs.
If they are divs, consider creating CSS rules for .line-item that will render divs contained within it horizontally, and arrange them so that they line up vertically, e.g:
.line_item div {
display: table-cell;
vertical-align: top;
margin-right: 8px;
}
JavaScript
Libraries
[COMMON_LIB]littled/lineitemModule.js
Binding handlers and buttons
Default binding handlers and buttons:
$(document).ready(function() {
$('.line-items-module').lineitemModule('init');
});
The defaults can be overridden by passing in custom configuration in the 2nd argument:
$(document).ready(function() {
$('.line-items-module').lineitemModule('init', {
edit: {
selector: '.custom-selector',
event: 'click',
uri: '/path/to/custom/edit_ajax.php'
},
del: {
uri: '/path/to/custom/del_ajax.php'
handler: function(evt, options) {
console.log('Custom handler called.');
}
}
});
});
PHP
Classes
[COMMON_LIB]content/ajax_page_class.php- Custom class to fetch and save the line item data from and to the database.
Utility scripts
[COMMON_LIB]_ajax/utils/edit_line_item.php[COMMON_LIB]_ajax/utils/del_line_item.php
Examples
- littledamien.com ontacts CMS Contact status and contact history
- Luzern Labs Advice CMS (deprecated)