Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
215 views
in Technique[技术] by (71.8m points)

java - ZK How to update ListBox ListModel using selected item

I'm making a web file browser using ZK Components and find a block. Is there any way to update the ListBox model using the selected item of the listbox? The use case is when traversing the files and folder, the user click the folder, and the list is refreshed with the content of the selected folder. The selection event is triggered and for regular file, it handle well, but not the folder.

My Code: myfilesvm.zul

<zk>
    <window apply="org.zkoss.bind.BindComposer"
        viewModel="@id('vm') @init('com.my.zk.mvvm.MyFilesViewModel')">
        <hlayout>
            <listbox vflex="true" hflex="1" model="@load(vm.files)"
                id="fileBrowser" selectedItem="@bind(vm.selectedFile)">
                <auxhead>
                    <auxheader colspan="3">File List</auxheader>
                    <auxheader colspan="3">
                        <hlayout>
                            <!-- breadcrumb, implemented later -->
                        </hlayout>
                    </auxheader>
                </auxhead>
                <listhead>
                    <listheader label="Name" />
                    <listheader label="Size" />
                    <listheader label="Modified" />
                </listhead>
                <template name="model" var="file">
                    <listitem>
                        <listcell label="@load(file.name)" />
                        <listcell label="@load(file.length())" />
                        <listcell label="@load(file.lastModified())" />
                    </listitem>
                </template>
            </listbox>
        </hlayout>
        <separator />
    </window>
</zk>

MyFilesViewModel.java

package com.my.zk.mvvm;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zkoss.bind.annotation.Init;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zul.Filedownload;
import org.zkoss.zul.ListModel;
import org.zkoss.zul.ListModelList;

import com.my.zk.FileCrumbManager;

public class MyFilesViewModel  {



    private static Logger log = LoggerFactory.getLogger(MyFilesViewModel.class);
//  AuthenticationService authService = new AuthenticationServiceImpl();
//  UserCredential cre = authService.getUserCredential();
    String homeFolder = "D:\path\home";

    ListModel<File> files = new ListModelList<File>(Arrays.asList(FileCrumbManager.populateList(new File(homeFolder))));
    File selectedFile;

    @Init
    public void init() { // Initialize

    }

    public ListModel<File> getFiles() {
        return files;
    }

    @NotifyChange({ "selectedFile" })
    public void setFiles(ListModel<File> files) {
        this.files = files;
    }

    public File getSelectedFile() {
        return selectedFile;
    }

    public void pilihFile() {
        if (getSelectedFile().isDirectory()) {
            log.info("File is a directory");
            this.files = new ListModelList<File>(
                    Arrays.asList(FileCrumbManager.populateList(new File(getSelectedFile().getAbsolutePath()))));
        } else {
            try {
                Filedownload.save(getSelectedFile(), null);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                log.error(e.getMessage());
            }
        }
    }

    public void setSelectedFile(File selectedFile) {
        this.selectedFile = selectedFile;
        pilihFile();
    }

}

Appreciate for any help. Thank you.

question from:https://stackoverflow.com/questions/65932203/zk-how-to-update-listbox-listmodel-using-selected-item

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The correct way to refresh ListModelList in pilihFile() is:

this.files.clear();
this.files.addAll(Arrays.asList(FileCrumbManager.populateList(new File(getSelectedFile().getAbsolutePath()))));

Because Listbox is model-driven rendering, you should control the rendering by manipulating the model object. When you call methods of ListModelList, it will notify Listbox to render into a browser. If you replace this.files with a new object, ZK doesn't know it. That's why your browser doesn't have the update.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...