How to dynamically set jquery multiselect selectedText parameter in javascript ..?

I have the following code. I want to check if each user in the table row has permission with the string “all” and set “ALL permissions are allowed” as selectedText of the corresponding selection list.

 <%!
 List permission = new ArrayList();
 Connection connection;
 PreparedStatement ps1,ps2;
 ResultSet rs1,rs2;
 String sql1,sql2;   
 // execute rs1
 %>
 <table>
  <tbody>
      <% 
         while (rs1.next()) {
          String user = rs1.getString('username');
      %>
      <tr> 
          <td class="username" width="200"><%=user%></td>
          <td class="perms" width="200">
             <%
                sql2 = "SELECT perms_name FROM perms WHERE username = ? ";
               // execute rs2              
                permission.clear();
                 while (rs2.next()) {
                    permission.add(rs2.getString("permission"));
                 }
            %>
            <select class="perms" name="perms" multiple="multiple">
                <% for(int i=0;i<permsList_folder.size();i++){%>
                    <option value="<%=permission.get(i)%>" disabled="disabled" selected="selected"><%=permission.get(i).toString().toUpperCase()%></option>
                <%}%>
            </select> 
          <td>
     </tr>
  </tbody>
 </table>
 <script>
      var hasAllPerms ="<%=permission.contains('all')%>";// this only handle last row results not for each row/user in table          
      $(".perms").multiselect({
            noneSelectedText: "No Permission",
            selectedText:hasAllPerms=="true"?"ALL Permissions Allowed":"# Permissions Allowed"
      });
 </script>
+3
source share
1 answer

There are several ways to achieve what you are trying to do.

In this case, it looks like you are using jsp, so you can just set the selected value and display the string on the server side without using javascript.

<select class="perms" name="perms" multiple="multiple">
   <% for(String permissionStr : permission) {
         String selectedStr="";
         String displayValue=permissionStr.toUpperCase();
         if("all".equals(permissionStr))
         {
            displayValue="ALL Permissions Allowed";
            selectedStr="selected='selected'";
         }
   %>
      <option value="<%=permissionStr%>" disabled="disabled" <%=selectedStr%> ><%=displayValue%></option>
   <%}%>
</select> 

If you want to install it using javascript / jquery, one way is to add an identifier to each select box, e.g.

<select class="perms" name="perms" id="perms<%=user%>" multiple="multiple">
   <% for(int i=0;i<permsList_folder.size();i++){%>
      <option value="<%=permission.get(i)%>" disabled="disabled" selected="selected"><%=permission.get(i).toString().toUpperCase()%></option>
   <%}%>
</select> 

id

$("#perms" + user).multiselect(...);

( java 1.5) 's' 'List' , , , .

<%!
List<String> permissions = new ArrayList<String>();
%>
+2

All Articles