簡體   English   中英

啟用選擇時,必須實現數據模型。

[英]datamodel must implement when selection is enabled.?

我想在選中復選框並按下“刪除”按鈕時從數據表中刪除行。

這是datatable片段:

<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>

這是支持豆

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);
        }

    }
}

當我運行此頁面時,出現以下錯誤數據模型,當啟用選擇時,必須實現org.primefaces.model.selectabledatamodel。

但是當我刪除復選框時,它們沒有錯誤,但是沒有復選框。

該怎么辦以及如何解決以下錯誤..請幫助。

我想要這樣的東西: http : //www.primefaces.org/showcase/ui/datatableRowSelectionRadioCheckbox.jsf

您只需要定義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();
    }
}

如下所示,將“購物車”對象更改為SD_User_Cart,

private SD_User_Cart cart;

然后在p:datatable中定義選擇,並添加如下所示的列,

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

希望這可以幫助:)

您需要定義一個private ArrayList<User_Cart> selectedItems; 后台類public class checkcart數據成員,例如private User_Cart[] selectedItems; 並提供setter和getter方法,使其同樣起作用。 我也遇到過同樣的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM