0

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]
}
4

2 回答 2

0

该问题已通过更新“可用”和“已授予”选择来解决。请注意在“从”上使用 Groovy 从可用选项中删除已授予选项。

<g:select name="available"
          id="available"
          from="${availableList - user?.granted}"
          value="${user?.granted}"
          optionKey="id"
          multiple="multiple"
          style="height:200px;"
 />

<g:select name='granted'
          id="granted"
          from="${user?.granted}"
          multiple="multiple"
          optionKey="id"
          style="height:200px;" 
 />
于 2013-08-27T19:30:18.310 回答
0

您授予的选择有这个from="[]"。您需要更改它,from以便它包含您已保存到数据库(或任何地方)的已授予项目的集合。

此外,您需要availableList在渲染视图(在您的控制器中)之前从您的授予项中过滤掉。

于 2013-08-16T21:31:33.973 回答