【问题标题】:Spring mvc 3 Jqgrid :How to load data to a select Type from a list of objectsSpring mvc 3 Jqgrid:如何将数据加载到对象列表中的选择类型
【发布时间】:2013-04-18 12:23:10
【问题描述】:

我有 2 个类, User.java 和 Bank.java 。 每个用户都有对银行的引用。当我添加新用户时,我必须从银行列表中选择一家银行。 所以我的问题是如何将对象列表(来自数据库)加载到 jqGrid 中的选择表单中,并且知道这个选定的数据将作为整数(引用银行表的外键)在数据库中注册,可以你告诉我这是怎么回事。

@Entity
public class Utilisateur {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long idUser;
.
    .
@Autowired
@ManyToOne
private Agence agence;

@ManyToOne
private Banque banque;

这是我的控制器

@RequestMapping(value = "/getall", method = RequestMethod.GET)
public @ResponseBody
CustomUserResponse getAll() {

    System.out.println("affichage");

    List<Utilisateur> users = utilisateurDao.getAllUsers();

List<Banque> banks=utilisateurDao.getAllBanks();


    CustomUserResponse response = new CustomUserResponse();
    response.setRows(users);

    response.setRecords(String.valueOf(users.size()));

    response.setPage("1");

    response.setTotal("10");

    return response;
}


    @RequestMapping(value = "/add", method = RequestMethod.POST)
public @ResponseBody
CustomGenericResponse add(

@RequestParam("nom") String nom, @RequestParam("prenom") String prenom,
        @RequestParam("nomAcces") String nomAcces,
        @RequestParam("typeAcces") TypeAcces typeAcces,
        @RequestParam("motPasse") String motPasse,
        @RequestParam("banque") Banque banque

) {

    logger.debug("Received request to add a new user");

    // Construct our new user object. Take note the id is not required.
    // Assign the values from the parameters
    Utilisateur user = new Utilisateur();

    user.setNom(nom);
    user.setPrenom(prenom);
    user.setNomAcces(nomAcces);
    user.setTypeAcces(typeAcces);
    user.setMotPasse(motPasse);
    user.setBanque(banque);
    // Do custom validation here or in your service

    // Call service to add
    Boolean success = utilisateurDao.save(user);

谢谢。

【问题讨论】:

    标签: json hibernate spring-mvc jqgrid


    【解决方案1】:

    如果您查看有关

    的文档

    http://www.trirand.com/jqgridwiki/doku.php?id=wiki:predefined_formatter

    它们描述了如何生成将在编辑表单中使用的选择事件。

    例如:(作为您在银行列的 colModel 中设置的一部分

    formatter:'select', editoptions:{value:"1:One;2:Two"} 
    

    银行的 ID 和名称将是您要传递的值。您也可以让网格查询您的控制器来构建此列表。

    【讨论】:

    • 谢谢马克,我试过这个:{name:'banque',index:'banque',width:100, editable: true,formatter "select",edittype:"select",editoptions: {dataUrl:"/ConsultationArchive/med/crud/listBanks"}},如 "/ConsultationArchive/med/crud/listBanks" 返回银行列表,在 colModel 中返​​回 [Object object],在 editform 中返回空选择表单,问候。
    • trirand.com/jqgridwiki/doku.php?id=wiki:common_rules 将向您展示如何构建选择列表的 3 种方法中的每一种。
    • 很高兴我能帮上忙,如果这能帮助您找到解决方案,请标记为正确。
    • 嗨,我终于找到了解决方案:)
    【解决方案2】:

    我找到了解决方案并且它有效, 要生成选择表单,我必须插入一个有效的 html (

    @RequestMapping(value = "/listBanks")
    public @ResponseBody
    String listBanques(String idbanq) {
    
        String select = null;
        String selectfinal = null;
        List<Banque> banks = utilisateurDao.getAllBanks();
    
        for (Banque b : banks) {
    
            select = select + "<option value=" + b.getIdBanque() + ">"
                    + b.getNom() + "</option>"
        }
        selectfinal = "<select>" + select + "</select>";
    
        return selectfinal;
    
    }
    

    在 ColModel 中:

    ${name:'idBanque',index:'banque',width:100, editable: true,edittype:"select",editoptions:{dataUrl:"/ConsultationArchive/med/crud/listBanks"}},
    

    感谢您的帮助,:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-20
      • 2021-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多