Skip to content

Commit

Permalink
Merge pull request #368 from lehvolk/master
Browse files Browse the repository at this point in the history
Non-ajax Select2 component
  • Loading branch information
martin-g committed Nov 23, 2014
2 parents ba010d9 + eec9172 commit 79781d8
Show file tree
Hide file tree
Showing 9 changed files with 315 additions and 99 deletions.
Expand Up @@ -43,6 +43,28 @@ <h1>Single-Select <small>Ajax result loading. Paging with infinite scrolling. Mi
</div>
</div>
</section>

<section>
<div class="page-header">
<h1>Single-Select <small>Non-ajax result loading.</small></h1>
</div>
<div class="row">
<div class="span12">
<p>
Country currently selected: <strong><span
wicket:id="country-non-ajax"></span> </strong>
</p>
<form wicket:id="single-non-ajax">
<input wicket:id="country-non-ajax" type="hidden"
style="width: 300px;" />
<div class="form-actions">
<button type="submit" class="btn">Submit</button>
</div>
</form>
</div>
</div>
</section>

<section>
<div class="page-header">
<h1>Multi-Select <small>Ajax result loading. Paging with infinite scrolling. Minimum input length. Drag &amp; Drop</small></h1>
Expand All @@ -63,6 +85,26 @@ <h1>Multi-Select <small>Ajax result loading. Paging with infinite scrolling. Min
</div>
</div>
</section>
<section>
<div class="page-header">
<h1>Multi-Select <small>Non-ajax result loading.</small></h1>
</div>
<div class="row">
<div class="span12">
<p>
Countries currently selected: <strong><span
wicket:id="countries-non-ajax"></span> </strong>
</p>
<form wicket:id="multi-non-ajax">
<input wicket:id="countries-non-ajax"
type="hidden" style="width: 300px;" />
<div class="form-actions">
<button type="submit" class="btn">Submit</button>
</div>
</form>
</div>
</div>
</section>
</div>
</body>
</html>
Expand Up @@ -12,65 +12,86 @@
*/
package org.wicketstuff.select2;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.model.PropertyModel;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
* Example page.
*
*
* @author igor
*
*/
public class HomePage extends WebPage {

private static final long serialVersionUID = 1L;

private static final int PAGE_SIZE = 10;
@SuppressWarnings("unused")
private Country country = Country.US;
@SuppressWarnings("unused")
private List<Country> countries = new ArrayList<Country>(Arrays.asList(new Country[] { Country.US, Country.CA }));
private List<Country> countries = new ArrayList<>(Arrays.asList(new Country[] { Country.US, Country.CA }));

public HomePage() {

// single-select example

add(new Label("country", new PropertyModel<Object>(this, "country")));
add(new Label("country", new PropertyModel<>(this, "country")));

Form<?> form = new Form<Void>("single");
add(form);

Select2Choice<Country> country = new Select2Choice<Country>("country", new PropertyModel<Country>(this, "country"),
Select2Choice<Country> country = new Select2Choice<>("country", new PropertyModel<Country>(this, "country"),
new CountriesProvider());
country.getSettings().setMinimumInputLength(1);
form.add(country);

add(new Label("country-non-ajax", new PropertyModel<>(this, "country")));

Form<?> nonAjaxform = new Form<Void>("single-non-ajax");
add(nonAjaxform);
Select2Choice<Country> nonAjaxCountry = new Select2Choice<>("country-non-ajax",
new PropertyModel<Country>(this, "country"),
Arrays.asList(Country.values()), new CountriesRender());
nonAjaxCountry.getSettings().setMinimumInputLength(1);
nonAjaxform.add(nonAjaxCountry);

// multi-select example

add(new Label("countries", new PropertyModel<Object>(this, "countries")));
add(new Label("countries", new PropertyModel<>(this, "countries")));

Form<?> multi = new Form<Void>("multi");
add(multi);

Select2MultiChoice<Country> countries = new Select2MultiChoice<Country>("countries", new PropertyModel<Collection<Country>>(this,
"countries"), new CountriesProvider());
Select2MultiChoice<Country> countries = new Select2MultiChoice<>("countries",
new PropertyModel<Collection<Country>>(this,
"countries"), new CountriesProvider());
countries.getSettings().setMinimumInputLength(1);
countries.add(new DragAndDropBehavior());
multi.add(countries);

add(new Label("countries-non-ajax", new PropertyModel<>(this, "countries")));
Form<?> nonAjaxMulti = new Form<Void>("multi-non-ajax");
add(nonAjaxMulti);

countries = new Select2MultiChoice<>("countries-non-ajax", new PropertyModel<Collection<Country>>(this,
"countries"), Arrays.asList(Country.values()), new CountriesRender());
countries.getSettings().setMinimumInputLength(1);
countries.add(new DragAndDropBehavior());
nonAjaxMulti.add(countries);
}

/**
* Queries {@code pageSize} worth of countries from the {@link Country}
* enum, starting with {@code page * pageSize} offset. Countries are matched
* on their {@code displayName} containing {@code term}
*
*
* @param term
* search term
* @param page
Expand All @@ -81,7 +102,7 @@ public HomePage() {
*/
private static List<Country> queryMatches(String term, int page, int pageSize) {

List<Country> result = new ArrayList<Country>();
List<Country> result = new ArrayList<>();

term = term.toUpperCase();

Expand All @@ -107,11 +128,12 @@ private static List<Country> queryMatches(String term, int page, int pageSize) {
* {@link Country} based choice provider for Select2 components.
* Demonstrates integration between Select2 components and a domain object
* (in this case an enum).
*
*
* @author igor
*
*
*/
public class CountriesProvider extends TextChoiceProvider<Country> {

private static final long serialVersionUID = 1L;

@Override
Expand All @@ -132,12 +154,18 @@ public void query(String term, int page, Response<Country> response) {

@Override
public Collection<Country> toChoices(Collection<String> ids) {
ArrayList<Country> countries = new ArrayList<Country>();
ArrayList<Country> countries = new ArrayList<>();
for (String id : ids) {
countries.add(Country.valueOf(id));
}
return countries;
}
}

private class CountriesRender extends ChoiceRenderer<Country> {

public CountriesRender() {
super("displayName");
}
}
}
Expand Up @@ -18,8 +18,9 @@
* Application object
*/
public class WicketApplication extends WebApplication {

@Override
public Class<HomePage> getHomePage() {
return HomePage.class;
return HomePage.class;
}
}

0 comments on commit 79781d8

Please sign in to comment.