Curtis Snowden
Curtis Snowden

Reputation: 473

Struts2 Jquery Multiple Select Value

I'm having a hell of a time with something that should be extremely simple. I'm using the Struts2 Jquery plugin, and I have an <sj:select> on my page. It's set to multiple. I have an array of values that I want to be pre-selected, but nothing is being set. If I give it a single item in the value tag, that single item is selected. If I pass an ArrayList of Integers, or a Collection of Integer (still an instance of ArrayList) or a comma-delimited string ("100,101,102") to the value, nothing is selected. I've tried passing a simple array of Integers (Integer[]) and that will at least select the first item in the array, but that's it. All others are apparently discarded. I cannot understand what I'm doing wrong, or what I can do to make this work.

<sj:select 
    href="%{myStrutsJSONAction}"
    id="itemSelect"
    formIds="editForm"
    reloadTopics="reloadItem"
    name="selectedString"
    list="availableItems"
    listKey="id"
    listValue="description"
    multiple="true"
    cssClass="form-control"
    value="selectedIds"
    />

This was originally a regular Struts2 select (<s:select>) and that worked perfectly fine with an ArrayList of Integers passed to the value tag. I've changed to the Jquery plugin version because I need the data to be reloaded based on the value of a separate list.

The reloadTopics is working perfectly fine, the data is being repopulated, and when the form submits, all of my selections are correctly passed back to the server. But nothing is initially selected when the page loads.

Upvotes: 2

Views: 1215

Answers (3)

Guna Sekaran
Guna Sekaran

Reputation: 563

Split String variable using jquery in jsp

$(document).ready(function() {

  var subAns = $("#submitedAnswer").val();

  var toSplit = subAns.split(",");
     for (var i = 0; i < toSplit.length; i++) {
      console.log(toSplit[i]);
     ..........
     ..........
  }
});

Upvotes: 0

Curtis Snowden
Curtis Snowden

Reputation: 473

The correct answer to the question. I'm just posting the workaround in case anyone has the same issue.

The quickest and easiest solution is to put your values into a comma-delimited string that will be sent to the page.

public String strutsActionToLoadPage() {
    List<Integer> selectedItemIds = new ArrayList<Integer>();
    /*
     * Prepare your data for the page
     */

    /*
     * Set up the comma-delimited string
     */
    this.selectedItemString = "";
    for (int y = 0; y < (selectedItemIds.size()-1); y++) {
        this.selectedItemString += selectedItemIds.get(y) + ",";
    }
    if (!selectedItemIds.isEmpty()) {
        this.selectedItemString += selectedItemIds.get(selectedItemIds.size()-1);
    }
    return SUCCESS;
}

public String getSelectedItemString() {
    return this.selectedItemString;
}
public void setSeletedItemString(String selectedItemString) {
    this.selectedItemString = selectedItemString;
}

Then, using javascript, convert the string to array and set the val() of the <sj:select> element when the document is ready.

$(document).ready(function() {
     var data = '<s:property value="selectedItemString" />';

     //Make an array, splitting on the commas
     var dataArray = data.split(",");

     // Set the value
     $("#itemSelect").val(dataArray);
});

This question helped me out with that.

Less than ideal, but functional.

Upvotes: 1

Aleksandr M
Aleksandr M

Reputation: 24396

Looking at the source code of the plugin it seems that pre-selecting multiple values at once is not supported right now.

You can register a new issue in the GitHub repo of this plugin.

As for the workaround, you can probably do it with some additional JavaScript code.

Upvotes: 1

Related Questions