Out of the box, the LightSwitch HTML Client lets you replace a built-in control such as a List, Text Box, or Date Picker with custom JavaScript code that inserts new DOM elements or creates an instance of a third-party control. First, in the designer, change the control type to Custom Control.
Next, in the Properties window, click the Edit Render Code link to generate code for the custom control. This opens the JavaScript code-behind file for the current screen, which contains the newly-created render function:
JavaScript |
Copy Code
|
---|---|
myapp.ScreenName.DateValue_render = function (element, contentItem) {
// Write code here.
};
|
Now what? In order to make this work, you need to know how to:
Or, you can use the extensibility features of LightSwitch HTML Edition to create a fully functional data-bound control without coding. Getting back to our Date Picker example, you would first change the control type in the designer to Wijmo Value Control.
Next, in the Properties window, change the value of the Widget property from Text Input to Calendar, since we are working with a Date value.
Then, click the Edit Render Code link just as you would for a regular custom control. Now the render function looks something like this:
JavaScript |
Copy Code
|
---|---|
myapp.ScreenName.DateValue_render = function (element, contentItem) { $.wijmo.wijcalendar.prototype.options.wijCSS.stateDefault = "ui-btn-up-a"; $.wijmo.wijcalendar.prototype.options.wijCSS.content = "ui-body ui-body-a"; var div = $("<div data-role='wijcalendar'/>"); div.appendTo($(element)); div.wijcalendar({ selectionMode: {day: true}, displayDate: contentItem.value }); div.wijcalendar("selectDate", contentItem.value); var displayDate = true; div.wijcalendar({ selectedDatesChanged: function (e, args) { var newValue = div.wijcalendar("getSelectedDate"); if (contentItem.value != newValue) { displayDate = false; contentItem.value = newValue; displayDate = true; } } }); contentItem.dataBind("value", function (newValue) { div.wijcalendar("unSelectAll"); if (displayDate) { div.wijcalendar({ displayDate: newValue }); } div.wijcalendar("selectDate", newValue); }); }; |
Press F5 to run the project and view the new calendar control. Use the angle brackets in the header to navigate to the next or previous month. Click the header text to quickly navigate to any month in the current year.
Also, implement the following steps to understand the functionality of the above given code:
JavaScript |
Copy Code
|
---|---|
myapp.ScreenName.created = function (screen) {
screen.DateValue = new Date();
};
|
Run the application and note that both controls are set to today’s date. When you change the selected date in the Wijmo calendar, the Date Picker control is updated. When you change any of the month/day/year values in the Date Picker control, the Wijmo calendar is updated.
All of the other Wijmo Value Control widgets are designed to work in this fashion. They handle their own change events as appropriate, and use LightSwitch data binding to remain in sync with the underlying data elements. Best of all, no coding is required, although advanced users can modify the generated code to suit their needs.