living on the grid - unlock the power of dojo data grids in xpages - mwlug 2013
DESCRIPTION
Explore the rich functionality of dojo data grids in XPages. This presentation starts with how to create a Dojo Data Grid and REST service in XPages and discusses the features available with the standard grid. It proceeds to demonstrate how to convert the grid to a Dojo EnhancedGrid and take advantage of additional plugins for enhanced functionality. Finally, it demonstrates how to use a Dojo TreeGrid in XPages to create categorized grids with or without calculated totals and counts.TRANSCRIPT
MWLUG 2013
Living on the Grid – Unlock the Power of Dojo Data Grids (AD104)
Brad Balassaitis, PSC Group
Brad Balassaitis
• Notes/Domino developer for 17 years, XPages 3+ years• Senior Consultant at PSC Group
– XPages Developer– Project Lead
• Contact Information– Blog: xcellerant.net– Twitter: @Balassaitis– LinkedIn: linkedin.com/in/bradbalassaitis
Agenda
• Dojo Data Grid Control– Creation and Default Features
• Dojo Data Grid – Additional Features– Sorting, Opening Docs, Searching, Editing
• Dojo EnhancedGrid– Conversion– Plugins
• Dojo TreeGrid– Categorization– Counts and Totals
Dojo Data Grid Control
• Availability– Extension Library– 8.5.3 Upgrade Pack 1– Notes 9
• Creates a Dojo DataGrid
Why use the Dojo Data Grid?
• It’s not a view panel• Feature-rich
– Adjustable columns, storing, editing, and much more• Dojo is already on the Domino server• Enhanced plugins are available
Creating a Dojo Data Grid
1. Create REST Service to provide data2. Add the Dojo Data Grid control and bind to the REST service3. Add Dojo Data Grid Columns
REST Service
• REST is a standard for client-server data exchange on the web• Common method for providing data to a grid• REST Service Control (ExtLib, 8.5.3 UP1, Notes9)
– Data Access > REST Service
REST Service - Creation
1. Add REST Service control to the page2. Select the service type– basics > service > + > xe:viewJsonService
3. Set the contentType to application/json– basics > service > contentType
4. Select the view– basics > service > viewName
5. Set defaultColumns to true– basics > service > defaultColumns
REST Service - Customization
• Compute columns• System columns• Roll your own
REST Service - Verification
• Set pathInfo– All Properties > basics > pathInfo
• Test it– Server.com/dbname.nsf/XPage.xsp/pathInfoName
Creating a Dojo Data Grid
1. Create REST Service to provide data2. Add the Dojo Data Grid control and bind to the REST service3. Add Dojo Data Grid Columns
Demo
Default Grid Features
• Infinite scrolling• Adjustable column widths• Row highlighting• Extended selection mode• Sortable columns• Multiple row entries
Additional Grid Properties
• rowSelector• autoHeight• rowsPerPage• escapeHTMLInData• loadingMessage• errorMessage• dir
Dojo Attributes
• More grid features are available, but not via properties– e.g. Column Reordering, Grid Width
• Dojo > Dojo attributes– or
• All Properties > dojo > dojo attributes
Column Reordering
• Allows user to rearrange columns• Set via dojo attribute
Column Reordering
• Alternate method of setting attribute
Grid Width
• By default, takes up width of containers• autoWidth attribute sizes to the required width• Set via dojo attribute
Demo
Grid Column Properties
• label• width• hidden• editable• formatter
– Client JavaScript function to manipulate column data– Example: format to all upper case
function allUpper(value) { return value.toUpperCase();}
Sorting
• Automatically attempts to provide ascending and descending sorting on all columns
• Each sort only works if enabled in underlying view column
Preventing Sort Options
• Attach a function to the canSort property of the grid– Receives column index number
• 1-based index• Ascending sort is positive number, descending is negative
– Returns true or false• Run code in onClientLoad event of page to attach function
– Example: Allow even-numbered columns to sort
dijit.byId('#{id:djxDataGrid1}').canSort = function(col){ if (col % 2 == 0) { return true; } else { return false; }}; Demo
Opening Documents
• Link not built-in• Row click events: onRowClick, onRowDblClick
– Client JavaScript– Events receive an argument with many properties
• Code varies based on REST service type
REST Service Output
viewJsonService viewItemFileService
Open Doc - viewJsonService
var grid = arguments[0].grid;var index = arguments[0].rowIndex;var item = grid.getItem(index);var unid = item[‘@unid’];var url = ‘MyPageName.xsp?documentId=’ + unid + ‘&action=openDocument’;window.document.location.href = url;
Open Doc - viewItemFileService
var index = arguments[0].rowIndex;var unid = REST_SERVICE._items[index].attributes[‘@unid’];var url = ‘MyPageName.xsp?documentId=’ + unid + ‘&action=openDocument’;window.document.location.href = url;
Search – Full Text
• Same steps as other display controls1. Create search field and bind to scope variable2. Set REST service’s search property to the scope variable3. Add button that triggers partial refresh on grid and REST service
• Cannot sort results• DB must be full-text indexed
Editable Columns
• Set column’s editable property to true• Double-click cell to change to edit mode• Save the changes – one line of client JavaScript
– REST_SERVICE_ID.save()– Use the jsId property, if defined (otherwise id)– ** Does not work without grid autoHeight set
• Undo changes – one line of client JavaScript (in theory)– REST_SERVICE_ID.revert()– Partial refresh on grid has same effect
Editable Columns – Field Types
• cellType property– Cell (default) – plain text– Select – drop-down list (compute options w/ SSJS)– Bool – checkbox– AlwaysEdit – not a field type– RowIndex – has nothing to do with editing
Demo
Editable Columns – Field Types
• Combo Box1. Include module: dojox.grid.cells.dijit2. Set cell type: dojox.grid.cells.ComboBox
Editable Columns – Field Types
• Multi-line Edit box1. Include module: dojox.grid.cells.dijit2. Set cell type: dojox.grid.cells.Editor
Editable Columns – Field Types
• Date Picker1. Include module: dojox.grid.cells.DateTextBox2. Set cell type: dojox.grid.cells.DateTextBox3. Only works in Notes 9
Editable Columns
• REST service’s save() function can accept callbacks– Useful for displaying an error if save fails
var saveCallbacks = {onError: function() {alert('There was an error saving your
changes.');}};restServiceID.save(saveCallbacks);
• Editable Columns work with viewItemFileService services, but require a Web Site document to allow put operations in order to work with viewJsonService
• Can only edit columns mapped to a single field• Grid has a singleClickEdit property• Highlighting changes
HTML Columns
• Create custom column in REST service (or use existing view column) with HTML content
• Add Grid Column to display HTML content column• Set Grid property escapeHTMLInData = false
– Not good practice – vulnerable to XSS
Icon Columns
• Images don’t work as pass-thru HTML• Requires column formatter function (client JS)• Formatter can conditionally return <IMG> tag• Does not require the escapeHTMLInData property
– Much more secure for all passthru HTML
Demo
Dojo EnhancedGrid
• Dojo module that extends DataGrid• Already available on the server• Provides enhanced features via plugins• Check for available features based on Domino/Dojo version
– Domino 8.5.3 – Dojo 1.6– Domino 9 – Dojo 1.8– http://dojotoolkit.org/reference-guide/1.6/dojox/grid/
EnhancedGrid.html
Converting to EnhancedGrid
• Dojo Data Grid control creates a Dojo DataGrid
• Can modify to create a Dojo EnhancedGrid1. Load the EnhancedGrid module2. Set the grid Dojo type3. Load required style sheets
Converting to EnhancedGrid
1. Load the EnhancedGrid module on the page– Resources > Add > Dojo Module– dojox.grid.EnhancedGrid
Converting to EnhancedGrid
2. Set grid Dojo type to dojox.grid.EnhancedGrid– Properties > Dojo > Dojo type
Converting to EnhancedGrid
3. Load required style sheets– Load relative to Domino server path
<xp:styleSheet href="/.ibmxspres/dojoroot/dojox/grid/resources/Grid.css"></xp:styleSheet><xp:styleSheet href="/.ibmxspres/dojoroot/dojox/grid/resources/EnhancedGrid.css"></xp:styleSheet>
Verify EnhancedGrid
EnhancedGrid Plugin - DnD
• Provides ability to drag and drop columns, rows, and cells– Configuration object defines options– Add a row selector in order to drag and drop rows
• Usage1. Select content to drag2. Release mouse button3. Click on the data and drag
• IMPORTANT: When cells are moved, changes are automatically saved
Demo
EnhancedGrid Plugin - DnD
1. Load the DnD plugin– Properties > Resources > Add > Dojo Module– dojox.grid.enhanced.plugins.DnD
EnhancedGrid Plugin - DnD
2. Add plugin to grid as an attribute– Dojo > Add
• Name: plugins• Value: {dnd: {dndConfig:{}}}
– All selection types enabled by default
EnhancedGrid Plugin - Filter
• Adds filter bar• Up to 3 filtering rules
– Match all rules or any rule– Select column to filter– Select matching condition (is, contains, starts with, ends with…)
• No coding required!!• Results are sortable
Demo
EnhancedGrid Plugin - Filter
1. Load the Filter plugin– Properties > Resources > Add > Dojo Module– dojox.grid.enhanced.plugins.Filter
EnhancedGrid Plugin - Filter
2. Add plugin to grid as an attribute– Dojo > Add
• Name: plugins• Value: {filter: true}
EnhancedGrid Plugin - Filter
3. Add a stylesheet
<xp:styleSheet href="/.ibmxspres/dojoroot/dojox/grid/enhanced/resources/
claro/EnhancedGrid.css"> </xp:styleSheet>
EnhancedGrid – More Plugins
• Print– Provides ability to preview or print grid contents– Print/Preview “All”, Selected, Custom
• Exporter– Provides grid data for export – you handle the rest– Export “All”, Selected, Custom
• Menu– Context menus for columns, rows, cells, selected– Build with dijit menus/menu items
Dojo TreeGrid
• Provides multi-level categorization• Can provide column totals and counts• Extends Dojo DataGrid
– Cannot use EnhancedGrid plugins• Poor documentation• Apparently cannot use Dojo Data Grid control
Demo
TreeGrid - Implementation
• Programmatic – NOT the Dojo Data Grid control1. Include required dojo modules and stylesheets2. Set XPage property to parse dojo on load3. Define a <div> to render the grid and set the size4. Execute code onClientLoad to configure and generate the grid5. Provide data for the grid (XAgent)– Complicated format
...{ id: 'AS', name:'Asia', type:'continent',children:[{_reference:'CN'}, {_reference:'IN'}] },{ id: 'CN', name:'China', type:'country' },{ id: 'IN', name:'India', type:'country' },...
TreeGrid – Totals and Counts
• Inline when collapsed, below when expanded• Requires a different (but simpler) JSON model
{id:'AK', type: 'state', state:'AK', numPeople: 3, childItems:[ {id:'B3093953178C98E905257838007ABC48', firstname:'Bella',
lastname: 'Martin', valueToAdd: 2}, {id:'7FDB9CCDE7D6923E05257838007ABC1E', firstname:'Brian',
lastname: 'Leggett', valueToAdd: 2}] },
• Add an aggregate property– Set to “sum” or “cnt”– Numeric columns handled properly– Text columns concatenated– Formatter functions can modify the display (and hide text)
Demo
TreeGrid – Data Format Caveat
• Simpler JSON format causes blank rows when used without counts/totals
TreeGrid - Features
• defaultOpen – expanded or collapsed• openAtLevels – auto expand all categories with less items than
the defined amount– Can accept an array for multiple levels of categorization– Doesn’t work with ForestStoreModel
• expandoCell – column in which the expand/collapse icon displays
Questions?
• Data Grid Blog Series: http://xcellerant.net/dojo-grids-in-xpages
• Sample database coming to openNTF soon
• Twitter: @Balassaitis