1) According to the API doc for t:commandSortHeader immediate defaults to
true.
2) In the example source code (got the source from Apache's SVN) they
manually invert the sort ordering, although, I agree, I thought it would be
done automatically as well.

AFAIK when you click on the commandSortHeader it should pass the columnName
attribute to the bean property specified in the t:dataTable sortColumn
attribute. In the example from the tomahawk source, it certainly looks as if
this is what happens. In that example, the coder certainly manually does the
inversion for subsequent sort requests for the same column. Here is some of
the sample code from the tomahawk SVN...

/*
 * Copyright 2004 The Apache Software Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.myfaces.examples.listexample;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * DOCUMENT ME!
 * @author Thomas Spiegl (latest modification by $Author: mmarinschek $)
 * @version $Revision: 395700 $ $Date: 2006-04-20 17:23:03 -0400 (Thu, 20
Apr 2006) $
 */
public class SimpleSortableCarList extends SortableList
{
    private List _cars;

    public SimpleSortableCarList()
    {
        super("type");

        _cars = new ArrayList();
        _cars.add(new SimpleCar(1, "car A", "red"));
        _cars.add(new SimpleCar(1, "car B", "blue"));
        _cars.add(new SimpleCar(1, "car C", "green"));
        _cars.add(new SimpleCar(1, "car D", "yellow"));
        _cars.add(new SimpleCar(1, "car E", "orange"));
    }

    public List getCars()
    {
        sort(getSort(), isAscending());
        return _cars;
    }

    protected boolean isDefaultAscending(String sortColumn)
    {
        return true;
    }

    protected void sort(final String column, final boolean ascending)
    {
        Comparator comparator = new Comparator()
        {
            public int compare(Object o1, Object o2)
            {
                SimpleCar c1 = (SimpleCar)o1;
                SimpleCar c2 = (SimpleCar)o2;
                if (column == null)
                {
                    return 0;
                }
                if (column.equals("type"))
                {
                    return ascending ? c1.getType().compareTo(c2.getType())
: c2.getType().compareTo(c1.getType());
                }
                else if (column.equals("color"))
                {
                    return ascending ?
c1.getColor().compareTo(c2.getColor()) :
c2.getColor().compareTo(c1.getColor());
                }
                else return 0;
            }
        };
        Collections.sort(_cars, comparator);
    }
}

/*
 * Copyright 2004 The Apache Software Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.myfaces.examples.listexample;



/**
 * Convenient base class for sortable lists.
 * @author Thomas Spiegl (latest modification by $Author: matzew $)
 * @version $Revision: 167718 $ $Date: 2005-03-24 11:47:11 -0500 (Thu, 24
Mar 2005) $
 */
public abstract class SortableList
{
    private String _sort;
    private boolean _ascending;

    protected SortableList(String defaultSortColumn)
    {
        _sort = defaultSortColumn;
        _ascending = isDefaultAscending(defaultSortColumn);
    }

    /**
     * Sort the list.
     */
    protected abstract void sort(String column, boolean ascending);

    /**
     * Is the default sort direction for the given column "ascending" ?
     */
    protected abstract boolean isDefaultAscending(String sortColumn);


    public void sort(String sortColumn)
    {
        if (sortColumn == null)
        {
            throw new IllegalArgumentException("Argument sortColumn must not
be null.");
        }

        if (_sort.equals(sortColumn))
        {
            //current sort equals new sortColumn -> reverse sort order
            _ascending = !_ascending;
        }
        else
        {
            //sort new column in default direction
            _sort = sortColumn;
            _ascending = isDefaultAscending(_sort);
        }

        sort(_sort, _ascending);
    }

    public String getSort()
    {
        return _sort;
    }

    public void setSort(String sort)
    {
        _sort = sort;
    }

    public boolean isAscending()
    {
        return _ascending;
    }

    public void setAscending(boolean ascending)
    {
        _ascending = ascending;
    }
}

<%@ page session="false" contentType="text/html;charset=utf-8"%>
<%@ taglib uri="http://java.sun.com/jsf/html"; prefix="h"%>
<%@ taglib uri="http://java.sun.com/jsf/core"; prefix="f"%>
<%@ taglib uri="http://myfaces.apache.org/tomahawk"; prefix="t"%>
<html>

<!--
/*
 * Copyright 2004 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
//-->

<[EMAIL PROTECTED] file="inc/head.inc" %>

<body>

<f:view>

    <f:loadBundle
basename="org.apache.myfaces.examples.resource.example_messages"
var="example_messages"/>

    <t:dataTable styleClass="standardTable"
            headerClass="standardTable_SortHeader"
            footerClass="standardTable_Footer"
            rowClasses="standardTable_Row1,standardTable_Row2"
            var="car"
            value="#{list.cars}"
            sortColumn="#{list.sort}"
            sortAscending="#{list.ascending}"
            preserveDataModel="true"
            preserveSort="true">

        <f:facet name="header">
            <h:outputText value="(header table)"  />
        </f:facet>
        <f:facet name="footer">
            <h:outputText value="(footer table)"  />
        </f:facet>

        <h:column>
            <f:facet name="header">
                <t:commandSortHeader columnName="type" arrow="true">
                    <h:outputText
value="#{example_messages['sort_cartype']}" />
                </t:commandSortHeader>
            </f:facet>
            <h:outputText value="#{car.type}" />
            <f:facet name="footer">
                <h:outputText id="ftr1" value="(footer col1)"  />
            </f:facet>
        </h:column>

        <h:column>
            <f:facet name="header">
                <t:commandSortHeader columnName="color" arrow="true">
                    <h:outputText
value="#{example_messages['sort_carcolor']}" />
                </t:commandSortHeader>
            </f:facet>
            <h:outputText value="#{car.color}" />
            <f:facet name="footer">
                <h:outputText id="ftr2" value="(footer col2)"  />
            </f:facet>
        </h:column>

    </t:dataTable>

</f:view>

<[EMAIL PROTECTED] file="inc/page_footer.jsp" %>

</body>

</html>

I'm still lost. I've been working for two days on this and it's starting to
become irksome.
--
View this message in context: 
http://www.nabble.com/Sortable-DataTable-in-Tomahawk...-t1643510.html#a4453169
Sent from the MyFaces - Users forum at Nabble.com.

Reply via email to