The standard button bars for any query or table can be customized through XML or the JavaScript client API. You can add, replace, delete buttons. Buttons can be linked words, icons, or drop-down menus. You can also control the visibility of custom buttons based on a user's security permissions. Custom button bars can leverage the functionality supplied by default buttons.
This topic covers:
An administrator can add additional buttons using XML metadata for a table or query. To add or edit XML metadata within the UI:
This example was used to create a "Custom Dropdown" menu item on the Medical History dataset in the demo study. Two examples of actions that can be performed by custom buttons are included:
<tables xmlns="http://labkey.org/data/xml">
<table tableName="MedicalHistory" tableDbType="TABLE">
<buttonBarOptions includeStandardButtons="true">
<item text="Custom Dropdown" insertPosition="end">
<item text="Say Hello">
<onClick>alert('Hello');</onClick>
</item>
<item text="LabKey.com">
<target>http://www.labkey.com</target>
</item>
</item>
</buttonBarOptions>
</table>
</tables>
Click here to see and try this custom button.
The following example returns the number of records selected in the grid.
<tables xmlns="http://labkey.org/data/xml">
<table tableName="PhysicalExam" tableDbType="TABLE">
<buttonBarOptions includeStandardButtons="true">
<item text="Number of Selected Records" insertPosition="end">
<onClick>
var dataRegion = LABKEY.DataRegions[dataRegionName];
var checked = dataRegion.getChecked();
alert(checked.length);
</onClick>
</item>
</buttonBarOptions>
</table>
</tables>
The following example hides a button (Grid Views) on the default button bar.
<tables xmlns="http://labkey.org/data/xml">
<table tableName="Participants" tableDbType="NOT_IN_DB">
<buttonBarOptions includeStandardButtons="true">
<item hidden="true">
<originalText>Grid views</originalText>
</item>
</buttonBarOptions>
</table>
</tables>
Premium users can access additional code examples here:
Review the API documentation for all parameters available, and valid values, for the buttonBarOptions and buttonBarItem elements:
A note about positioning: As shown in the example, you can add new buttons while retaining the standard buttons. By default, any new buttons you define are placed after the standard buttons. To position your new buttons along the bar, use one of these buttonBarItem parameters:
You can set buttons to require that one or more rows are selected before they are activated. Buttons that require selections will be grayed out/inactive when no rows are selected.
Using the "hidden" parameter controls whether a button is shown at all. Two parameters are available for finer grained control of button visibility:
<buttonBarOptions position="top" includeStandardButtons="false">
<item hidden="false" permission="INSERT">
<originalText>Charts</originalText>
</item>
<item hidden="false" permission="INSERT">
<originalText>Insert</originalText>
</item>
<item hidden="false" permission="ADMIN">
<originalText>Audit</originalText>
</item>
</buttonBarOptions>
You can also define a button to invoke a JavaScript function. The function itself must be defined in a .js file included in the resources of a module, typically by using the includeScript element. This excerpt can be used in the XML metadata for a table or query, provided you have "moreActionsHandler" defined.
<item requiresSelection="true" text="More Actions">
<onClick>
moreActionsHandler(dataRegion);
</onClick>
</item>
The LABKEY.QueryWebPart API includes the buttonBar parameter for defining custom button bars. The custom buttons defined in this example include:
<div id='queryTestDiv1'/>
<script type="text/javascript">
///////////////////////////////////////
// Custom Button Bar Example
var qwp1 = new LABKEY.QueryWebPart({
renderTo: 'queryTestDiv1',
title: 'Query with Custom Buttons',
schemaName: 'study',
queryName: 'MedicalHistory',
buttonBar: {
includeStandardButtons: true,
items:[
LABKEY.QueryWebPart.standardButtons.views,
{text: 'Folder Home', url: LABKEY.ActionURL.buildURL('project', 'begin')},
{text: 'Test Script', onClick: "alert('Test Script button works!'); return false;"},
{text: 'Test Handler', handler: onTestHandler},
{text: 'Test Menu', items: [
{text: 'Item 1', handler: onItem1Handler},
{text: 'Fly Out', items: [
{text: 'Sub Item 1', handler: onItem1Handler}
]},
'-', //separator
{text: 'Item 2', handler: onItem2Handler}
]},
LABKEY.QueryWebPart.standardButtons.exportRows
]}
});
function onTestHandler(dataRegion)
{
alert("onTestHandler called!");
return false;
}
function onItem1Handler(dataRegion)
{
alert("onItem1Handler called!");
}
function onItem2Handler(dataRegion)
{
alert("onItem2Handler called!");
}
</script>
</div>
Documentation:
Notes:To see the above example in action, you can install it in your own local build following these steps.
If you do not already have a module where you can add this example, create the basic structure of the "HelloWorld" module, and understand the process of building and deploying it following this tutorial first:
<view xmlns="http://labkey.org/data/xml/view"
title="Custom Buttons"
frame="portal">
<permissions>
<permission name="read"/>
</permissions>
</view>
Subscribers to premium editions of LabKey Server can learn more with the example code in these topics: