selectpicker多选功能实现

1.前台设置

需要设置selectpicker为multiple,见代码


	
			        


控件传给后台的值是诸如‘id1,id2,id3’这样的形式;

前台界面图片

selectpicker多选功能实现_第1张图片

2.传给后台的controller层代码如下:

@RequestMapping("queryTestSamplePage")
    public String queryTestSamplePage(@RequestParam(defaultValue = "1", required = false) int pageNo,
            @RequestParam(defaultValue = "15", required = false) int pageSize, @RequestParam(defaultValue = "0", required = false) int recordOrder,
            TestSampleSearchVO testSampleSearchVO, HttpServletRequest request, Model model) {
        PageBean pageBean = new PageBean();
        pageBean.setPageNo(pageNo);
        pageBean.setPageSize(pageSize);
        pageBean.setOrderBy("ts.create_time");
        Map map = new HashMap();
        map.put("projectId", request.getSession().getAttribute("projectId"));
        
        String originalSupplierStr=testSampleSearchVO.getvSupplier();
        String originalContStr=testSampleSearchVO.getvControllerType();
        map.put("testSampleSearchVO", testSampleService.modifySuppAndCont(testSampleSearchVO));
        pageBean.setParams(map);
        pageBean.setResultList(testSampleService.findTestSamplePage(pageBean));
        
        testSampleSearchVO.setvSupplier(originalSupplierStr);
        testSampleSearchVO.setvControllerType(originalContStr);
        
        model.addAttribute("testSampleSearchVO", testSampleSearchVO);
        
        model.addAttribute("recordOrder", recordOrder);
        model.addAttribute("params", pageBean.getParams());
        model.addAttribute("pageBean", pageBean);
        model.addAttribute("controllerList", testSampleControllerService.findControllerList());
        model.addAttribute("supplierList", testSampleSupplierService.findSupplierList());
        
        request.getSession().setAttribute("selectedItem", "testSample_sample");

        return ControllerConstants.TESTSAMPLE + File.separator + "testSample.jsp";
    }
注意:

1)其中TestSampleSearchVO有对vControllerType的定义:(注意在controller层对此字段处理后,会去调用最终的mybatis处的查询,但是查完之后又恢复成原来的originalContStr,这是因为需要把此参数传给前台,以使得在前台可以保持勾选原来选择的东西)

private String vControllerType;
2)modifySuppAndCont函数是主要的处理函数,对拼成最后的字符串有决定性作用,代码如下:

public TestSampleSearchVO modifySuppAndCont(TestSampleSearchVO testSampleSearchVO) {
        List testContList=testSampleControllerDao.getAllEntity();;
        List testSuppList=testSampleSupplierDao.getAllEntity();;
        StringBuffer strBuff=new StringBuffer();
        ......此处省略了一些代码
        testSampleSearchVO.setvSupplier(strBuff.toString());
        
        
        StringBuffer strContBuff=new StringBuffer();
        if(testSampleSearchVO.getvControllerType()!=null){
            if(!testSampleSearchVO.getvControllerType().equals("")){
                String[] contrStrList=testSampleSearchVO.getvControllerType().split(",");
                strContBuff.append("(");
                    for(String k:contrStrList){
                        strContBuff.append("\'");
                        strContBuff.append(k);
                        strContBuff.append("\'");
                        strContBuff.append(",");
                                
                    }
                    strContBuff.append("''");
                    strContBuff.append(")");
                    
            }else{
                strContBuff.append("(");
                for(TestSampleControllerModel tm:testContList){
                    strContBuff.append("\'");
                    strContBuff.append(tm.getId());
                    strContBuff.append("\'");
                    strContBuff.append(",");
                }
                strContBuff.append("''");
                strContBuff.append(")");
            }
        }else{
            strContBuff.append("(");
            for(TestSampleControllerModel tm:testContList){
                strContBuff.append("\'");
                strContBuff.append(tm.getId());
                strContBuff.append("\'");
                strContBuff.append(",");
            }
            strContBuff.append("''");
            strContBuff.append(")");
        }
        testSampleSearchVO.setvControllerType(strContBuff.toString());
        return testSampleSearchVO;
    }
3)追溯到最终的mybatis查询语句代码



4)可以看一下查询时后台拼成的sql(拷贝自myeclipse的console)

select count(1) FROM test_sample ts LEFT JOIN test_sample_controller tsc ON ts.controller_type=tsc.id LEFT JOIN test_sample_supplier tss ON ts.supplier=tss.id LEFT JOIN system_users su ON ts.creator=su.id LEFT JOIN system_domain_int sdi ON ts.sample_status=sdi.domain_key AND sdi.domain_type='TEST_SAMPLE_STATUS' WHERE ts.controller_type IN ('30ba86d1ca23478b8b0855fbaff53b8a','7e555d4afd1349259ccf2ac755abee6a','852df69237db401eab51c102612b4082','') AND ts.supplier IN ('b57e824f80a14eb4a85cfcbc81bfefa5','c3899b11ec39497aa41515660f0b09f7','') AND ts.project_id=? 

Parameters: 4eb4503406724520a811c9d1fc8fee8c(String)




你可能感兴趣的:(selectpicker多选功能实现)