Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
145
Add a new Row to a Grid
posted

 Hi there

 

Is there any code sample, tips etc. for adding an updateable row to a Grid and then Submit?

We would like to have a grid with a list of objects and when the user selects "create new" another row will be added to the grid.

 

Thanks in advance,

Shai 

 

  • 120
    posted

    Hi, Shaikoren!

    I've written an example for test purposes and I'll explain the scenario. I'll devide in two parts - jsp and bean parts where to explain my steps.

    1. Jsp part

     //Comment: In jsp (see bellow) are added some buttons to change data source - "Add Data", "Remove Data" and  "Change Data". For each of them there is bound component and added ActionListener in the used bean.

    <h:commandButton value="Add Data" binding="#{beanName.addButton}"
            actionListener="#{beanName.onDataSourceChange}"/>
    <h:commandButton value="Remove Data" binding="#{beanName.removeButton}"
            actionListener="#{beanName.onDataSourceChange}"/>        
     <h:commandButton value="Change Data" binding="#{beanName.changeButton}"
            actionListener="#{beanName.onDataSourceChange}"/>

    <ig:gridView binding="#{beanName.grid}"
         dataSource="#{beanName.rows}" id="grid1"
          />

    2. Bean part. It is dynamyc grid with some additions. I send you a java file (.txt) for the bean and explain you the logic bellow.

    Note:thesent DynamicChangingBean extends BaseGridPage and it is not a problem. the BaseGridPage can be taken from NetAdvantageSamples 

    In Bean the following methods:

    //Three UICommand components presented for add, remove and change data; setXXX is getXXX methoods for button components available, for one compoenent is shown

    private UICommand addButton = null;

    public UICommand getAddButton() {

    return addButton;

    }

    public void setAddButton(UICommand addButton) {

    this.addButton = addButton;

    }

    //The following method is called when an button is press (actionListener method), set a field what kind of action should be executed

    public void onDataSourceChange(ActionEvent event)

    //The next method is called to prepare data source and to change(add, remove or change data) if it is needed, an action is set to be executed in the previous method and null the action

    private DataSource getDataSource()  

    //For dynamic grid realization

    public void setGrid(GridView grid) {

    this.grid = grid;

    // if the grid is empty, it probably means that it hasn't been initialized yet

    if (grid.getChildren().isEmpty()) {

    initGrid();

    }

    }

    private void clearGrid() {

    // clear the grid

    grid.setFooter(null);

    grid.setHeader(null);

    grid.getChildren().clear();

    grid.getTemplateItems().clear();

    grid.setColumnPositions(null);

    }

    private void initGrid() {

    FacesContext context = FacesContext.getCurrentInstance();

    // clear the grid

    clearGrid();

    // loop through the meta-data defining the columns in the dataModel and create a column in the grid for basic data type

    dataSource = getDataSource(); //here dataSource is created or changed

    if (dataSource != null) {

    ColumnMetaData[ columns = dataSource.getColumMetaData();

    for (int i = 0; i < columns.length; i++) {

    ColumnMetaData columnMetaData = columns[i];

    if (columnMetaData != null) {

    if (columnMetaData.getDatatype() != List.class) {

    // create a column

    Column columnName = createColumn(context,

    columnMetaData.getDisplayName(), "#{DATA_ROW."

    + columnMetaData.getName() + "}",

    columnMetaData.getName());

    grid.getTemplateItems().add(columnName); }}}}