I have a Grails project that I'm using the [jquery dual listbox plugin] to populate box2 (granted) with options from box1 (available).
My problem is that in the case of a record that already exists (updating), options that have already been selected do not show up in box 2. All options appear in box 1 as though they have not already been selected. What can be done to remove the selected options from box1 (available) and have them appear in box2 (granted)?
The jquery dual list plugin is working as far as moving items from box1 to box2 and vice-versa when the Grant/Deny buttons are used.
Javascript in edit.gsp:
$(function() {
$.configureBoxes({
transferMode: 'move',
useFilters: false,
useCounters: false,
box1View: 'available',
box2View: 'granted',
to1: 'toAvail',
to2: 'toGranted'
});
});
Selects in edit.gsp:
<table>
<tr>
<td>
Available<br/>
<g:select name="available"
id="available"
from="${availableList}"
value="${user?.granted}"
optionKey="id"
multiple="multiple"
style="height:200px;"
/>
</td>
<td>
<button id="toGranted" type="button" class="button_right">Grant</button>
<br/>
<button id="toAvail" type="button" class="button_left">Deny</button>
</td>
<td>
Granted<br/>
<g:select name='granted'
id="granted"
from="[]"
multiple="multiple"
style="height:200px;" />
</td>
</tr>
</table>
To make things slightly more complicated, box 1 (available) may be populated from an onChange event from another select in the edit.gsp:
<g:select name="parent" id="parent"
from="${Parent.list()}"
optionKey="id"
noSelection="[null:'']"
value="${user?.parent}"
onchange="${remoteFunction(
controller: 'user',
action: 'ajaxGetAvailable',
onSuccess: 'updateAvailable(data)',
params: '\'id=\' + this.value')}"
/>
function updateAvailable(data) {
var $element = $('#available');
var $granted = $('#granted');
$element.empty();
$granted.empty();
$.each(data, function(i, item) {
$element.append($('<option>').val(data[i].id).text(data[i].name))
});
}
The function above uses a JSON object, from the controller:
def ajaxGetAvailable = {
def parent = Parent.get(params.id)
render parent?.available as JSON
}
Classes referenced above:
class Parent {
static mapWith = "mongo"
String entityId
String name
static hasMany = [ available: Entity ]
}
class Entity {
static mapWith = "mongo"
String entityId
String name
static belongsTo = [parent:Parent]
}