2

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.

Curtis Snowden
  • 407
  • 1
  • 4
  • 20
  • 1
    The well meaning struts2 jQuery plugin obfuscates a simple JavaScript library. You can use the UI library backing the plugin with less headache than the plugin proposes to remedy, save for those few who have vowed to do web programming while avoiding learning JavaScript. – Quaternion Mar 08 '16 at 05:26
  • I have no problem implementing javascript myself. In fact, I already have done just that and manually achieved the same end result. But it feels like a crude workaround, and I can't see anything that indicates this is a bug in the plugin. I really want to know where the problem is. Am I doing something wrong with my setup, or does the value tag for a struts-jquery multi-select just not work? The plugin has been really finicky, but I've never before encountered something I couldn't eventually figure out. – Curtis Snowden Mar 09 '16 at 01:33
  • 1
    Using these tags is a hack... If using JS looks like a hack, re-factoring is probably in order. JavaScript can be very readable and from a maintenance point of view, JS is much more accessible than these tags. From a value point of view time spent learning any JS library is time better spent. Well I did my part and said "this way lies madness!" but if you insist this page has everything worked out, although I've clearly pointed out I'm not a fan, I am impressed by the spectacular documentation supporting it: http://struts.jgeppert.com/struts2-jquery-showcase/index.action – Quaternion Mar 09 '16 at 04:18
  • @CurtisSnowden Although your workaround isn't direct answer to your question, I would still suggest to you to post it as an answer. It might help someone someday. :) I'll upvote it if you decide to post it. – Aleksandr M Mar 09 '16 at 21:06
  • Sure, I can do that. I wasn't sure if it would break a rule or something so played it safe. – Curtis Snowden Mar 09 '16 at 21:53

3 Answers3

1

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.

Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
1

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.

Community
  • 1
  • 1
Curtis Snowden
  • 407
  • 1
  • 4
  • 20
0

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]);
     ..........
     ..........
  }
});
Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
Guna Sekaran
  • 553
  • 4
  • 5