Wiki source code of Master Detail Tutorial
Version 6.1 by Vincent Massol on 2015/09/20
Show last authors
author | version | line-number | content |
---|---|---|---|
1 | Explains how to implement a [[master-detail view>>https://en.wikipedia.org/wiki/Master%E2%80%93detail_interface]] in XWiki using [[Applications Within Minutes>>extensions:Extension.App Within Minutes Application]] (AWM) and some custom coding. More specifically we'd like to have an HTML Form with 2 fields and when selecting a value in one field, the values available in the other field automatically update based on the first selection. | ||
2 | |||
3 | We'll take the example of States and Cities: one field will let the user pick a State and the other field a City. | ||
4 | |||
5 | {{toc/}} | ||
6 | |||
7 | = Step 1: Create a State Data Application = | ||
8 | |||
9 | The first step is to create an application with AWM that will allow to enter some states and cities and to link them. So create an app called "StateData" and when designing the form do as shown in the following screenshot: | ||
10 | |||
11 | {{image reference="statedata-form.png"/}} | ||
12 | |||
13 | = Step 2: Add Entries for the State Data Application = | ||
14 | |||
15 | Now that the State Data Application has been created fill it with 5 entries as shown in the screenshot: | ||
16 | |||
17 | {{image reference="statedata-entries.png"/}} | ||
18 | |||
19 | Note: XWiki forces us to give a name to each entry so we have used ##entry1## till ##entry5##. | ||
20 | |||
21 | = Step 3: Create a State Application = | ||
22 | |||
23 | Let's now use AWM again to create the State Application which is the application in which we wish to have the master-detail implemented. | ||
24 | |||
25 | In the design view, start by adding a ##State## field of type ##Database List##: | ||
26 | |||
27 | {{image reference="state-form1.png"/}} | ||
28 | |||
29 | Note that we link this field with the ##state## field of the first State Data Application we have created through its ##StateDataCode.StateDataClass##. | ||
30 | |||
31 | Then add a ##City## field of type ##Static List## with no entries: | ||
32 | |||
33 | {{image reference="state-form2.png"/}} | ||
34 | |||
35 | = Step 4: Modify the State Class Sheet = | ||
36 | |||
37 | Let's now modify ##StateCode.StateSheet## to implement dynamically displaying the City HTML Select. Modify the default content to be: | ||
38 | |||
39 | {{code}} | ||
40 | {{velocity}} | ||
41 | {{html wiki="true"}} | ||
42 | #set ($discard = $doc.use('StateCode.StateSheet')) | ||
43 | #set ($discard = $services.localization.use('document', 'StateCode.StateTranslations')) | ||
44 | (% class="xform" %) | ||
45 | ((( | ||
46 | <dl> | ||
47 | <dt><label for="StateCode.StateClass_0_state">$escapetool.xml($doc.displayPrettyName('state', false, false))</label></dt> | ||
48 | <dd>$doc.display('state')</dd> | ||
49 | <dt><label for="StateCode.StateClass_0_city">$escapetool.xml($doc.displayPrettyName('city', false, false))</label></dt> | ||
50 | <dd> | ||
51 | #if ($xcontext.action == 'edit') | ||
52 | $xwiki.jsx.use('StateCode.StateClass') | ||
53 | <select id="StateCode.StateClass_0_city" name="StateCode.StateClass_0_city" size="1"> | ||
54 | <option></option> | ||
55 | </select> | ||
56 | #else | ||
57 | $!doc.getValue('city') | ||
58 | #end | ||
59 | </dd> | ||
60 | </dl> | ||
61 | ))) | ||
62 | {{/html}} | ||
63 | {{/velocity}} | ||
64 | {{/code}} | ||
65 | |||
66 | Note that we're using a [[Javascript Skin Extension>>platform:DevGuide.SkinExtensionsTutorial]] that we'll be creating in the current page (i.e. ##StateCode.StateSheet##): {{code}}#set ($discard = $doc.use('StateCode.StateSheet')){{/code}} | ||
67 | |||
68 | = Step 5: Add a Javascript Skin Extension = | ||
69 | |||
70 | Edit ##StateCode.StateSheet## in the Object editor and add a ##XWiki.JavaScriptExtension## Object: | ||
71 | |||
72 | {{image reference="state-jsx.png"/}} | ||
73 | |||
74 | You can copy-paste this code: | ||
75 | |||
76 | {{code}} | ||
77 | require(['jquery'], function ($) { | ||
78 | var stateSelect = $('#StateCode\\.StateClass_0_state'); | ||
79 | var citySelect = $('#StateCode\\.StateClass_0_city'); | ||
80 | var jsonDocument = new XWiki.Document('WebHome', 'StateCode.JSON'); | ||
81 | stateSelect.change(function() { | ||
82 | $.get(jsonDocument.getURL('get', 'outputSyntax=plain&state=' + stateSelect.val()), function(json) { | ||
83 | var output = ''; | ||
84 | $.each(json, function(index, value) { | ||
85 | output += '<option>' + value + '</option>'; | ||
86 | }); | ||
87 | citySelect.empty().append(output); | ||
88 | }); | ||
89 | }).change(); | ||
90 | }); | ||
91 | {{/code}} | ||
92 | |||
93 | Note that we expect an XWiki page named ##StateCode.JSON.WebHome## to return some JSON containing a list of City values for the passed State. We'll add it in the next step. | ||
94 | |||
95 | = Step 6: Create a JSON Service = | ||
96 | |||
97 | Create a page named ##StateCode.JSON.WebHome## (just add a page named ##JSON## in the ##StateCode## space in the Create Page UI): | ||
98 | |||
99 | {{image reference="state-json.png"/}} | ||
100 | |||
101 | You can copy paste the following as its content: | ||
102 | |||
103 | {{code}} | ||
104 | {{velocity}} | ||
105 | #if($xcontext.action == 'get' && "$!{request.outputSyntax}" == 'plain') | ||
106 | $response.setContentType('application/json') | ||
107 | #end | ||
108 | #set($list = []) | ||
109 | #set ($state = $request.state) | ||
110 | #if ("$!state" != '') | ||
111 | #set ($itemList = $services.query.xwql("where doc.object(StateDataCode.StateDataClass).state like :state").bindValue('state', $state).execute()) | ||
112 | #foreach ($item in $itemList) | ||
113 | #set ($itemDoc = $xwiki.getDocument($item)) | ||
114 | #set ($discard = $list.add($itemDoc.getValue('city'))) | ||
115 | #end | ||
116 | $jsontool.serialize($list) | ||
117 | #end | ||
118 | {{/velocity}} | ||
119 | {{/code}} | ||
120 | |||
121 | You're all set! Let's now try it! | ||
122 | |||
123 | = Step 7: Create an entry in the State Application = | ||
124 | |||
125 | Create an entry and verify that when you change the State, the list of available Cities is updated :) | ||
126 | |||
127 | {{image reference="state-result1.png"/}} | ||
128 | |||
129 | And when viewing the page: | ||
130 | |||
131 | {{image reference="state-result2.png"/}} |