30.52. <w:rowEditorPlug>

RowEditorPlug是dataGrid的一个插件,只用作w:dataGrid的子标签;用于增强dataGrid,使之具有行编辑的功能。

30.52.1. 构件信息

表 30.127. 构件信息

Component Type org.operamasks.faces.component.plugin.grid.impl.UIRowEditorPlug
Component Family org.operamasks.faces.component.plugin.grid.impl.UIRowEditorPlug
Component Class org.operamasks.faces.component.plugin.grid.impl.UIRowEditorPlug
Tag Class org.operamasks.faces.webapp.plugin.UIRowEditorPlugTag
Renderer Type org.operamasks.faces.component.plugin.grid.impl.UIRowEditorPlug
Renderer Class(AJAX) org.operamasks.faces.render.plugin.grid.RowEditorPlugRenderer

继承体系

+java.lang.Object

++javax.faces.component.UIComponent

+++javax.faces.component.UIComponentBase

++++org.operamasks.faces.component.plugin.grid.base.UIRowEditorPlugBase

+++++org.operamasks.faces.component.plugin.grid.impl.UIRowEditorPlug

30.52.2. 属性

表 30.128. 属性

binding 定义类 javax.faces.component.UIComponentBase
类型 javax.el.ValueExpression(javax.faces.component.UIComponent)
是否必须 延时求值
一个值表达式,用于把该组件链接到一个ManagedBean的某个属性。
cancelText 定义类 org.operamasks.faces.component.plugin.grid.impl.UIRowEditorPlug
类型 javax.el.ValueExpression(java.lang.String)
是否必须 延时求值
取消按钮的文字
id 定义类 javax.faces.component.UIComponentBase
类型 java.lang.String
是否必须 延时求值
这个组件的组件标识符。这个值在最近的命名容器类型的父组件范围内,必须是唯一的。
rendered 定义类 javax.faces.component.UIComponentBase
类型 boolean(boolean)
是否必须 延时求值
一个标志,指出该组件是否要在任何随后的form提交过程中被渲染或处理。 这个属性的缺省值是true。
saveText 定义类 org.operamasks.faces.component.plugin.grid.impl.UIRowEditorPlug
类型 javax.el.ValueExpression(java.lang.String)
是否必须 延时求值
保存按钮的文字

30.52.3. 示例

下面的例子演示了行编辑插件的用法,w:rowEditorPlug作为w:dataGrid的子标签存在

<w:form>
            <w:editDataGrid id="grid" paged="true" rows="10" clicksToEdit="2" toolBarPosition="top" height="400" width="800">
            	<w:rowEditorPlug saveText="保存"  cancelText="取消" />
                <w:outputColumn id="id" hidden="true" hidable="false" />
                <w:outputColumn id="name" width="150" align="center" sortable="true" header="公司">
                    <w:textField id="nameEidt" allowBlank="false" />
                </w:outputColumn>
                <w:outputColumn id="leadingProduct" header="主打产品">
                    <w:textField id="leadingProductEdit" />
                </w:outputColumn>
                <w:outputColumn id="address" width="150" header="地址">
                    <w:textField id="addressItem" />
                </w:outputColumn>
                <w:outputColumn id="phone" header="联系电话">
                    <w:textField id="phoneItem" />
                </w:outputColumn>
                <w:outputColumn id="homepage" width="250" header="公司主页">
                    <w:textField id="homepageEdit" />
                </w:outputColumn>

                <w:pagingToolbar displayInfo="true" displayMsg="显示 {0} - {1},共 {2} 条">
                    <w:separator />
                    <w:menu label="数据维护">
                        <w:commandMenuItem label="增加" image="../images/new.gif"
                            action="#{ux.datarepresent.datagrid.gridPlugins.rowEditorBean.insert}" />
                        <w:commandMenuItem label="增加(预填数据)" image="../images/new.gif"
                            action="#{ux.datarepresent.datagrid.gridPlugins.rowEditorBean.insertWithData}" />
                        <w:commandMenuItem label="删除" image="../images/delete.gif"
                            action="#{ux.datarepresent.datagrid.gridPlugins.rowEditorBean.remove}" />
                        <w:commandMenuItem label="保存" image="../images/save.gif"
                            action="#{ux.datarepresent.datagrid.gridPlugins.rowEditorBean.save}" />
                    </w:menu>
                    <w:separator />
                    <w:button id="add" value="增加" image="../images/new.gif" />
                    <w:button id="addWithData" value="增加(预填数据)" image="../images/new.gif" />
                    <w:button id="remove" value="删除" image="../images/delete.gif" />
                    <w:button id="save" value="保存" image="../images/save.gif" />
                </w:pagingToolbar>
            </w:editDataGrid>
        </w:form>

Managed Bean代码:

@ManagedBean(name = "ux.datarepresent.datagrid.gridPlugins.rowEditorBean", scope = ManagedBeanScope.REQUEST)
public class RowEditorBean {
    @ManagedProperty("#{companyService}")
    private CompanyService companyService;
    
    @Bind(id = "grid")
    private List<Company> getCompanys() {
        return companyService.findAll();
    }

    @Bind(id = "grid")
    private UIEditDataGrid grid;

    @Bind(id = "grid", attribute = "addedData")
    private Object addedData;
    @Bind(id = "grid", attribute = "modifiedData")
    private Object modifiedData;
    @Bind(id = "grid", attribute = "removedData")
    private Object removedData;
    @Bind(id = "grid", attribute = "bindBean")
    private Class bindBean = Company.class;

    @Action(id = "add")
    public void insert() {
        grid.insertRow(0);
    }

    @Action(id = "addWithData")
    public void insertWithData() {
        grid.insertRow(0, new Company(UUID.randomUUID().toString(),"Microsoft","Windows, Office","天河软件园","http://www.microsoft.com","800 810 1818",""));
    }

    @Action(id = "remove")
    public void remove() {
        grid.remove();
    }

    @Action(id = "save")
    public void save() {
        try {
            if (addedData != null) {
                add((Company[]) addedData);
            }
            if (modifiedData != null) {
                update((Company[]) modifiedData);
            }
            if (removedData != null) {
                remove((Company[]) removedData);
            }
            grid.commit();
            grid.reload();
        } catch (Exception e) {
            throw new FacesException(e);
        }
    }

    private void remove(Company[] companys) {
        for (Company company : companys) {
            companyService.remove(company);
        }
    }

    private void update(Company[] companys) {
        for (Company company : companys) {
            companyService.update(company);
        }
    }

    private void add(Company[] companys) {
        for (Company company : companys) {
            company.setId(UUID.randomUUID().toString());
            companyService.add(company);
        }
    }
}

Company和CompanyService分别是实体类和服务类,此处从略。