0

How to sort on dynamic listheader with dynamic data

asked 2014-02-11 10:48:10 +0800

DarkFoo gravatar image DarkFoo
1 1

updated 2014-02-11 10:51:19 +0800

Hello guys,

I want to create a table with a dynamic header and dynamic content. I wrote:

        <window>
            <listbox model="@bind(vm.usersListModel)" vflex="1" sizedByContent="true">

                <listhead children="@bind(vm.headerListModel)" hflex="1">
                    <template name="children" var="item">
                        <listheader label="@load(item)" sort="auto"/>
                    </template>
                </listhead>

                <template name="model" var="line">
                    <listitem children="@bind(line)">   
                        <template name="children" var="cell">
                            <listcell label="@load(cell)"/>
                        </template>
                    </listitem>
                </template>
            </listbox>              
        </window>

and all I need is a sort for the columns. But with a ListModelList or ListModelArray the error "cannot be cast to java.lang.Comparable" comes. Do I need to write a comparator? How?

delete flag offensive retag edit

Comments

Ah okay... but how do I get the index/column to my compareTo method? I would use a ListModelArray.

DarkFoo ( 2014-02-11 12:30:26 +0800 )edit

Sorry guys, but I think the problem is that I don't know the actual header label. <listheader label="@load(item)" sort="auto(cell)"/> does not work, because there is no method called "cell" In my I use an ArrayList<List<String>>.

DarkFoo ( 2014-02-12 08:54:35 +0800 )edit

2 Answers

Sort by ยป oldest newest most voted
5

answered 2014-02-11 18:34:59 +0800

sitansu gravatar image sitansu
2254 13
http://java91.blogspot.in...

Here's demo try it:

package com.swain;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.zkoss.bind.annotation.AfterCompose;
import org.zkoss.zul.ListModel;
import org.zkoss.zul.ListModelList;

public class TestViewModel {
    private Set<Person> selectedCode;

    public class Person {
        private String _name;

        public Person(String name) {
            _name = name;
        }

        public String getName() {
            return _name;
        }
    }

    private ListModelList<Person> model;

    @AfterCompose
    public void init() {
        List<Person> persons = new ArrayList<Person>();
        persons.add(new Person("aaa"));
        persons.add(new Person("bbbbb"));
        persons.add(new Person("zzz"));
        persons.add(new Person("ZZZZZ"));
        persons.add(new Person("aaa"));

        model = new ListModelList<Person>(persons);
        model.setMultiple(true);
    }

    public ListModel<Person> getModel() {
        return model;
    }

    public Set<Person> getSelectedCode() {
        return selectedCode;
    }

    public void setSelectedCode(Set<Person> selectedCode) {
        this.selectedCode = selectedCode;
    }
}

Here's zul:

<?page title="new page title" contentType="text/html;charset=UTF-8"?>
<zk>
<window title="new page title" border="normal" >
<label value="You are using: ${desktop.webApp.version}"/>
<hlayout>
    <window title="Listbox Default Auto-sorting" border="normal" hflex="1">
        <listbox>
            <listhead>
                <listheader label="Name (insensitive)" sort="auto" />
            </listhead>
            <listitem>
                <listcell label="aaa"/>
            </listitem>   
            <listitem>
                <listcell label="bbbbb"/>
            </listitem>   
            <listitem>
                <listcell label="zzz"/>
            </listitem>   
            <listitem>
                <listcell label="ZZZZZ"/>
            </listitem>   
        </listbox>  
    </window>
    <window title="Listbox Field Auto-sorting" border="normal" hflex="1"
        apply="org.zkoss.bind.BindComposer"
        viewModel="@id('vm') @init('com.swain.TestViewModel')">
        <listbox model="@load(vm.model)" multiple="true" sizedByContent="true" span="true" checkmark="true" height="400px" selectedItems="@bind(vm.selectedCode)">
            <listhead>
            <listheader></listheader>
                <listheader label="Name (insensitive)" sort="auto(LOWER(name))"/>
                <listheader label="Name (sensitive)" sort="auto(name)" />
            </listhead>
            <template name="model" var="person">
                <listitem>
                <listcell />
                    <listcell label="@load(person.name)"/>
                    <listcell label="@load(person.name)"/>            
                </listitem>
            </template>
        </listbox>
    </window>
</hlayout>
</window>
</zk>
link publish delete flag offensive edit

Comments

1

nice example sitansu and even I learned again from it.

chillworld ( 2014-02-11 18:46:43 +0800 )edit
0

answered 2014-02-11 11:09:12 +0800

chillworld gravatar image chillworld flag of Belgium
5367 4 9
https://github.com/chillw...

updated 2014-02-11 12:47:19 +0800

Or you write a comparator or you need to do this :
edit: You don't have to know the index, you compare 2 objects with each other.(I did fill in a comparator on the Id of an object, you just need to implement on what you want to compare.)

public class Item implements Comparable<Item> {
    @Override
    public int compareTo(Item item){
        if(item.getId() < this.getId()) {
           return -1;
        } else if (item.getId() == this.getId()) {
           return 0;
        } else {
           return 1;
        }
    }

}

see here at stackoverflow for more info

link publish delete flag offensive edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
1 follower

RSS

Stats

Asked: 2014-02-11 10:48:10 +0800

Seen: 85 times

Last updated: Feb 11 '14

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More