启用选择时,datamodel必须实现。

时间:2014-04-25 10:48:55

标签: jsf primefaces

当勾选复选框并按下删除按钮时,我想从数据表中删除行..

这是数据表摘录:

<p:dataTable id="cartTable" lazy="true" scrollable="true"
             scrollHeight="115" selection="#{Cart_Check.selectedItems}"
             value="#{Cart_Check.cart}" var="cart" rowKey="#{cart.sheetno}"
             style="widht:100%;margin-top:10%;margin-left:1%;margin-right:30px ;box-shadow: 10px 10px 25px #888888;">

    <f:facet name="header">  
        Checkbox Based Selection  
    </f:facet>

    <p:column selectionMode="multiple" style="width:2%">
    </p:column>

    //Here the columns are metion
    <f:facet name="footer">
        <p:commandButton id="viewButton" value="Remove" />
    </f:facet>
</p:dataTable>

这是支持bean

public class checkcart {
        private int items;
        private ArrayList<User_Cart> cart;
        private ArrayList<User_Cart> selectedItems;

    public checkcart() {
        getvalues();
    }
//getter and setter 

    public void getvalues() {
        FacesContext context = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) context.getExternalContext()
                .getSession(false);
        System.out.println("Cart Request ::::" + session.getAttribute("regid"));

        try {
            Connection connection = BO_Connector.getConnection();
            String sql = "Select * from cart_orderinfo where usrregno=?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, (String) session.getAttribute("regid"));
            ResultSet rs = ps.executeQuery();
            cart = new ArrayList<>();
            while (rs.next()) {
                User_Cart user_cart = new User_Cart();
                user_cart.setSheetno(rs.getString("sheetno"));
                user_cart.setState_cd(rs.getString("state_cd"));
                user_cart.setDist_cd(rs.getString("dist_cd"));
                user_cart.setLicensetype(rs.getString("license_type"));
                user_cart.setFormat(rs.getString("sheet_format"));
                user_cart.setQuantity(rs.getInt("quantity"));
                cart.add(user_cart);
            }

        } catch (Exception ex) {
            System.out.println(ex);
        }

    }
}

当我运行此页面时,我收到以下错误 datamodel必须在启用选择时实现org.primefaces.model.selectabledatamodel。

但是当我删除复选框时,它们没有错误,但它没有复选框。

该怎么做以及如何解决以下错误..亲切帮助..

我想要这样的事情: http://www.primefaces.org/showcase/ui/datatableRowSelectionRadioCheckbox.jsf

2 个答案:

答案 0 :(得分:0)

您只需要定义ListDataModel,如下所示,

public class SD_User_Cart extends ListDataModel<User_Cart> implements SelectableDataModel<User_Cart> {

    public SD_User_Cart() {
    }

    public SD_User_Cart(List<User_Cart> data) {
        super(data);
    }

    @Override
    public User_Cart getRowData(String rowKey) {
        //In a real app, a more efficient way like a query by rowKey should be implemented to deal with huge data  

        List<User_Cart> rows = (List<User_Cart>) getWrappedData();

        for (User_Cart row : rows) {
            if (row.getCartId.toString().equals(rowKey)) {//CartId is the primary key of your User_Cart
                return row;
            }
        }

        return null;
    }

    @Override
    public Object getRowKey(User_Cart row) {
        return row.get.getCartId();
    }
}

更改您的购物车&#34;对象进入SD_User_Cart,如下所示,

private SD_User_Cart cart;

然后在p:datatable中定义选择,并添加如下所示的列

<p:column selectionMode="multiple" style="width:18px"/>

希望这会有所帮助:)

答案 1 :(得分:0)

您需要在后面的类private ArrayList<User_Cart> selectedItems;中定义public class checkcart数据成员,例如private User_Cart[] selectedItems;,并为其提供相同的setter和getter方法。 我也面临同样的问题。