On Mon, Jul 28, 2008 at 3:59 PM, nanotech <[EMAIL PROTECTED]> wrote:

>
> Hi All,
>
> I have a Save Form where user should be able to add/save new records and
> should be able
> to view feedback if there are any errors.
>
> In short, When user loads up the page he sees a form and under it a
> DataTable
> which holds the deafault search results.
>
> If he tries to add a record and Add is "Successful" then DataTable gets
> updated via Ajax.
> In case of "Failure" errors are reported on feedback panel.
>
> Here is how I have implemented it:
>
> ===================
> SaveContactPage.html
> ===================
>
> <div wicket:id="saveUserFeedback">[Feedback-error messages etc.]</div>
> <form wicket:id="saveUserForm">
>        <table>
>                <tr>
>                        <td>First Name:</td>
>                        <td><input type="text" wicket:id="firstName" /></td>
>                </tr>
>                <tr>
>                        <td>Last Name:</td>
>                        <td><input type="text" wicket:id="lastName" /></td>
>                </tr>
>                <tr>
>                        <td><input type="submit"
> wicket:id="saveContactButton" /></td>
>                </tr>
>
>        </table>
> </form>
> <!--  Updatable container for search/add results  -->
> <div wicket:id="resultsContainer">
>        <table wicket:id="addResultsTable">[Results for add/search
> Contacts]</table>
> </div>
> =====================
> SaveContactPage.java
> ======================
> public class SaveContactPage extends WebPage
> {
>        // Hold reference to provider..
>        AddContactDataProvider provider;
>        // Hold refernce to FeedbackPanel...
>        FeedbackPanel saveUserFeedback;
>        //Hold refernece to updatable dataTable's container..
>        WebMarkupContainer resultsContainer;
>
>        public SaveContactPage()
>        {
>                add(saveUserFeedback = new
> FeedbackPanel("saveUserFeedback"));
>                saveUserFeedback.setOutputMarkupId(true);
>
>                SaveUserForm userForm = new SaveUserForm("saveUserForm");
>                add(userForm);
>
>                // Get a new provider.
>                provider = new AddContactDataProvider((ContactInfoPOJO)
> userForm.getModelObject());
>
>                // Get all the columns.
>                List<IColumn> columns = createColumns();
>
>                resultsContainer = new
> WebMarkupContainer("resultsContainer");
>                resultsContainer.add(new DataTable("addResultsTable",
> columns,
>                                provider, 10));
>
>        }
>
>        private List<IColumn> createColumns()
>        {
>                List<IColumn> columns = new ArrayList();
>                columns.add(new PropertyColumn(this.getModel(),
> "firstName"));
>                columns.add(new PropertyColumn(this.getModel(),
> "lastName"));
>        }
>
> // Begin Save USER Form.
> private class SaveUserForm extends StatelessForm
> {
>        public SaveUserForm(String id)
>        {
>                super(id);
>                // Set Form's model
>                setModel(new CompoundPropertyModel(new ContactInfoPOJO()));
>                add(new TextField("firstName"));
>                add(new TextField("lastName"));
>                add(new AjaxFallbackButton("saveContactButton", this)
>                {
>                        @Override
>                        protected void onSubmit(AjaxRequestTarget target,
> Form form)
>                        {
>                                // TODO 1: Is this Correct approach
>                                // (i.e. calling the provider
>                                // again to refresh the container)
>                                // On Submit....call the provider again?
>
>                                provider = new
> AddContactDataProvider(form.getModelObject());
>
>                                //Refresh the dataTable
>                                target.addComponent(resultsContainer);
>
>                                // In case of Failure...Display the error
> messages.
>
>                        }
>
>                        protected void onError(AjaxRequestTarget target,
> Form form)
>                        {
>                                // TODO 3: Show Errors...if any
>                                // But Error happned in DataProvider...
>                                // How do I get hold of Error Object Here...
>                                target.addComponent(saveUserFeedback);
>                        }
>
>                });
>
>        }
> }
> }
>
>
> ======================
> AddContactDataProvider.java
> ======================
>
> public class AddContactDataProvider extends SortableDataProvider
> {
>
>    private ContactInfoPOJO contactPojo;
>    private ContactManager contactManager;
>
>    public AddContactDataProvider(ContactInfoPOJO contactPojo)
>    {
>        this.contactPojo = contactPojo;
>        contactManager = new JDBCContactManager();
>    }
>
>    public Iterator iterator(int first, int count)
>    {
>        // Save the Contact...
>        contactManager.saveContact(contactPojo);
>
>        //TODO 2::: HOW? WHERE???
>        //If Save FAILED then GET ERROR MESSAGE
>
>
>        // Get the saved list back....
>        List contacts = contactManager.getUsers();
>        if (first > 0) {
>                contacts = contacts.subList(first, first + count);
>        }
>        return contacts.iterator();
>    }
>
>    public IModel model(Object object)
>    {
>         return new ContactModel((ContactInfoPOJO)object);
>    }
>
>    public int size()
>    {
>        return contactManager.getContacts().size();
>    }
>
> }
>
>
> Questions:
> 1. Is calling dataprovider again the correct approach for updating the
> datatable? (See TODO 1 above) Can someone please suggest an alternate
> approach?
> 2. How do I pass the Error Object to Page's Panel from DataProvider in case
> of Unsuccessful  save?(see TODo: 2 above)
> 3. Will onError be called if I somehow plugin the error message to feedback
> Panel (See TODO: 3)
>
>
>
> Thanks for reading the post.
> --
> View this message in context:
> http://www.nabble.com/Best-way-to-implement-DataTable-updatable-via-Ajax-tp18698367p18698367.html
> Sent from the Wicket - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>


-- 
Ryan Gravener
http://twitter.com/ryangravener

Reply via email to