All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.wicketstuff.datatables.DataTables Maven / Gradle / Ivy

There is a newer version: 10.3.0
Show newest version
package org.wicketstuff.datatables;

import de.agilecoders.wicket.jquery.util.Strings2;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.behavior.Behavior;
import org.apache.wicket.extensions.markup.html.repeater.data.grid.DataGridView;
import org.apache.wicket.extensions.markup.html.repeater.data.table.DataTable;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.markup.head.IHeaderResponse;
import org.apache.wicket.markup.head.OnDomReadyHeaderItem;
import org.apache.wicket.markup.repeater.data.EmptyDataProvider;
import org.apache.wicket.markup.repeater.data.IDataProvider;
import org.wicketstuff.datatables.options.Options;

import java.util.List;

import static de.agilecoders.wicket.jquery.JQuery.$;

/**
 *
 */
public class DataTables extends DataTable {

    public static final int ZERO_ROWS_PER_PAGE = Integer.MIN_VALUE;

    private final Options options;
    private final long rowsPerPage;

   /**
     * Constructor that should be used for virtual scrolling
     *
     * @param id The component id
     * @param columns The table columns
     */
    public DataTables(final String id, final List> columns) {
        this(id, columns, new EmptyDataProvider(), ZERO_ROWS_PER_PAGE);
    }

    public DataTables(final String id, final List> iColumns, final IDataProvider dataProvider, final long rowsPerPage) {
        super(id, iColumns, dataProvider, rowsPerPage == ZERO_ROWS_PER_PAGE ? 1 : rowsPerPage);
        this.rowsPerPage = rowsPerPage;

        setOutputMarkupId(true);
        this.options = new Options();
        
    }

    @Override
    protected void onInitialize() {
        super.onInitialize();

        add(newDataTablesBehavior());
    }

    protected Behavior newDataTablesBehavior() {
        return new DataTablesBehavior(options);
    }

    public Options getOptions() {
        return options;
    }

    public void repaint(AjaxRequestTarget target) {
        target.appendJavaScript(String.format("window['%s'].api().ajax.reload();", getJsHandle()));
    }

    @Override
    public void renderHead(IHeaderResponse response) {
        super.renderHead(response);

        if (isEnabledInHierarchy()) {
            String dataTableFn = $(this).chain("dataTable", getOptions()).get();
            String setup = String.format("window['%s'] = %s", getJsHandle(), dataTableFn);
            response.render(OnDomReadyHeaderItem.forScript(setup));
        }
    }

    /**
     * @return An identifier that could be used to lookup the DataTable after initialization
     */
    public String getJsHandle() {

        // WicketStuff_DataTables_markupId
        return "WS_DT_" + Strings2.escapeMarkupId(getMarkupId());
    }

    @Override
    protected DataGridView newDataGridView(final String id, final List> iColumns, final IDataProvider dataProvider) {
        return new DataGridView(id, iColumns, dataProvider) {
            @Override
            public long getItemsPerPage() {
                return rowsPerPage;
            }
        };
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy