0

listbox with nonSelectabletags inplace new listitem

asked 2014-06-11 13:45:31 +0800

javiut gravatar image javiut flag of Venezuela, Bolivarian Republic of
90 1 5

i have a listbox like this

<listbox nonselectableTags="">
   <listitem>
    <listcell><textbox inplace='true'/>
    </listcell>
</listitem>
 </listbox>

every time i click on the textbox the listitem becomes selected this is my expected behavior but when i try to add a new Listitem by adding a new class to model

like this

listbox.getModel().add(new Student());

i have try to set selected the lastitem with

item.setSelected(true);
listbox.setSelectedItem(item);
listbox.setSelectedIndex(item.getIndex());

and seems to selected but when i think do the onAfterRender ZK selected the last listitem which was selected by hand.

my question is in this scenario how can i selected the last listitem added..

thanks a lot

delete flag offensive retag edit

Comments

mvvm or mvc?

chillworld ( 2014-06-11 13:55:15 +0800 )edit

is mvc style chillworld

javiut ( 2014-06-11 14:28:11 +0800 )edit

3 Answers

Sort by ยป oldest newest most voted
2

answered 2014-06-13 05:53:20 +0800

cor3000 gravatar image cor3000
4596 2 7
ZK Team

updated 2014-06-13 05:55:37 +0800

If you are using a listbox with a model (doesn't matter if MVC or MVVM) you need to control the selection via the model.

As shown in this example on zkfiddle.

So simply call model.clearSelection() and model.addToSelection(newValue);

If you retrieve the model from the listbox, you'd have to cast to org.zkoss.zul.ext.Selectable<E> in order to get access to the clearSelection and addToSelection methods.

Of course this implies that your model implements the Selectable interface which is the case anyway for all AbtractListModel implementations.

Robert

link publish delete flag offensive edit

Comments

very good!!!

mhj ( 2014-06-14 02:17:30 +0800 )edit
0

answered 2014-06-11 23:27:12 +0800

mhj gravatar image mhj flag of Brazil
806 1 7

Hello javiut, have you tried with listbox.selectItem((Listitem) listbox.getLastChild()); ?

link publish delete flag offensive edit
0

answered 2014-06-16 10:27:30 +0800

cyiannoulis gravatar image cyiannoulis
1191 10

As Robert said, if you are using a model to populate your listbox, you have to control the selections through this model. Here is a working MVC example:

<?page title="MVC Students" contentType="text/html;charset=UTF-8"?>
<zk>
<window apply="snippets.MvcStudentsController"
        width="100%" height="100%"
        xmlns:n="native">

    <n:h1>MVC Students</n:h1>

    <hlayout valign="middle" width="50%">
        <button id="addButton" label="Add Student" sclass="btn btn-primary" />
        <textbox id="txtCode" placeholder="Code" width="70px" sclass="form-control" />
        <textbox id="txtName" placeholder="Name" width="200px" sclass="form-control" />
        <intbox id="txtGrade" placeholder="Grade" width="70px" sclass="form-control" />
        <label hflex="1" />     
        <n:h3><n:span class="label label-default glyphicon glyphicon-user">
            <label id="lblCount" />
        </n:span></n:h3>
        <separator width="20px"></separator>
    </hlayout>

    <listbox id="studentsList" model="${$composer.students}" width="50%">
        <listhead>
            <listheader label="Code" width="70px" />
            <listheader label="Name" hflex="1" />
            <listheader label="Grade" width="80px" />
        </listhead>
        <template name="model">
            <listitem>
                <listcell><label value="${each.code}" /> </listcell>
                <listcell><label value="${each.name}" /> </listcell>
                <listcell><intbox inplace="true" value="${each.grade}" 
                    width="70px" sclass="txt-grade" /></listcell>
            </listitem>
        </template>
    </listbox>

    <n:h3><label id="lblSelection" /></n:h3>
</window>
</zk>

And the composer:

public class MvcStudentsController extends SelectorComposer<Component> {

    private static final long serialVersionUID = 1L;

    private ListModelList<Student> students = createData();

    @Wire private Listbox studentsList;
    @Wire private Label lblCount;
    @Wire private Label lblSelection;
    @Wire private Textbox txtCode;
    @Wire private Textbox txtName;
    @Wire private Intbox txtGrade;


    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);
        updateCount();
    }

    @Listen("onClick = #addButton")
    public void onClickAddButton() {
        if (txtCode.getValue().isEmpty() || txtName.getValue().isEmpty()) {
            Clients.showNotification("Please type a valid 'code' and 'name' ");
            return;
        }
        /*
         * Add a new student to the model
         */
        Student student = new Student(txtCode.getValue(), txtName.getValue(), txtGrade.getValue()); 
        students.add( student );
        /*
         * Update the selection
         */
        updateSelection(student);
        /*
         * Update the count label
         */
        updateCount();
    }

    /*
     * Set row selection when entering a grade field
     */
    @Listen("onFocus = .txt-grade")
    public void onFocusGrade(Event event) {
        Listitem listitem = (Listitem) event.getTarget().getParent().getParent();
        Student selectedStudent = (Student) listitem.getValue();
        updateSelection(selectedStudent);
    }

    /*
     * Update the selection status label every time a selection is made
     */
    @Listen("onSelect = #studentsList")
    public void onStudentSelected(Event event) {
        Listitem selectedItem = studentsList.getSelectedItem();
        Student selectedStudent = (Student) selectedItem.getValue();
        updateSelectionStatus(selectedStudent);
    }

    /*
     * Update the selections in the data model. 
     * Selections in the data model are reflected automaticaly 
     * to the listbox widget.
     */
    private void updateSelection(Student student) {
        List<Student> selections = new ArrayList<Student>();
        selections.add(student);
        students.setSelection(selections);
        updateSelectionStatus(student);
    }

    private void updateSelectionStatus(Student student) {
        lblSelection.setValue("Current selection: " + student.getName());
    }

    private void updateCount() {
        lblCount.setValue("" + students.getSize());
    }   

    public ListModelList<Student> createData() {

        List<Student> data = new ArrayList<Student>();
        data.add(new Student("1", "Costas", 10));
        data.add(new Student("2", "Nick", 20));
        data.add(new Student("3", "Zoe", 30));
        data.add(new Student("4", "Helen", 40));
        data.add(new Student("5", "Paul", 50));
        return new ListModelList<Student>(data);
    }

    public ListModel<Student> getStudents() {
        return students;
    }

}

Hope that helps

Costas

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
2 followers

RSS

Stats

Asked: 2014-06-11 13:45:31 +0800

Seen: 44 times

Last updated: Jun 16 '14

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