Populating Drop down with the values from database in play frame work

后端 未结 2 1566
自闭症患者
自闭症患者 2020-12-22 11:44

I am new to play frame work and i am finding it bit difficult. i am retrieving list of client names from data base and populating it to a dropdown ,here is my client.java c

相关标签:
2条回答
  • 2020-12-22 11:53

    Play framework provides template helper library which gives functionality to build select dropdown with options and selected value. It's pretty simple to use once understood properly.

    @helper.select() method in view takes various parameters related to a input field of select type. 1st parameter is form field, since here we don't have any form we can create a temporary form and create a new field inside with name Consultant[as this will the value of name attribute of select field]. 2nd parameter will be map of options in which key and value, corresponds to value of option tag and text enclosed in option tag respectively.

    Controller code

    package controllers;
    
    import models.Client;
    
    import play.mvc.*;
    import views.html.*;
    
    public class Application extends Controller {
    
    
        public static Result index(){
    
            return ok(index.render(Client.getClientDetails()));
        }
    
    }
    

    Model Code

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.*;
    
    import play.db.ebean.Model;
    public class Client extends Model {
    
        /**
         * 
         */
        private static final long serialVersionUID = -1932214701504374792L;
        public static String ClientName;
        public static HashMap<String, String> Clientdetail= new HashMap<String, String>();
        public static HashMap<String, String> getClientDetails() {
            try {
                Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
                Connection conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433","SUMEET","sumeet");
                Statement sta = conn.createStatement();
                String Sql = "select * from client";
                ResultSet rs = sta.executeQuery(Sql);
                while (rs.next()) {
                    ClientName = rs.getString("ClientName");
                    Clientdetail.put(ClientName,ClientName);
                   }
    
            } catch (InstantiationException | IllegalAccessException
                    | ClassNotFoundException |SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return(Clientdetail);
    
        }
    
    }
    

    View Code : index.scala.html

    @(ClientDetails: java.util.HashMap[String, String])
    
    @import helper._
    
    @main("ADMS") {
    
       <center>
        <form id="select">
       <a>CONSULTANT</a>
       @select(Form.form()("Consultant"),           
           options(ClientDetails),
           'value -> "clientName1"[any value that should be selected by default])
         <table border="1">
           <tr>
           <td width=50px>Client</td>
           <td width=50px>Project</td>
           <td width=50px>Task</td>
           <td width=50px>Date</td>
           <td width=50px>Consultant</td>
           <td width=50px>Role</td>
           <td width=80px>Is Billable</td>
           </tr>
           <tr>
           <td>@ClientDetails</td>
           </tr>
           </table>
          </form>
       </center>
    }
    
    0 讨论(0)
  • 2020-12-22 11:58

    Yeah, unfortunately Play's templates system is Scala based so you'll have to map over the your @ClientDetails:

    @ClientDetails.map { clientName =>
      <option>@clientName</option>
    }
    

    If you return more complex objects it could look more like this:

    @ClientDetails.map { client =>
      <option value="@client.id">@client.name</option>
    }
    

    I hope this helps!

    0 讨论(0)
提交回复
热议问题