0

Input invoice using inline listbox

asked 2014-06-16 11:27:57 +0800

onsir gravatar image onsir
132 2

I tried input data sales use listbox so, while i type Code in cell code, cell name will fill description automatically

my code like this

Invoice.zul <zk xmlns="&lt;a href=" http:="" www.zkoss.org="" 2005="" zul"="">http://www.zkoss.org/2005/zul" xmlns:h="http://www.w3.org/1999/xhtml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.zkoss.org/2005/zul http://www.zkoss.org/2005/zul/zul.xsd">

<window id="win" width="950px" border="normal" &gt;="" <tabbox="" id="tb" height="500px"> <tabs id="tabs"> <tab id="tbcreagte"/> </tabs> <tabpanels> <tabpanel> <panel id="panel" framable="true" height="450px"> <panelchildren>

<!-- Tested with ZK 6.0.2 -->
<div apply="org.zkoss.bind.BindComposer" viewModel="@id('vm') @init('zkexample.invoice.InvoiceVM')">
    <hlayout>
        <vlayout>
            <listbox id="listboxInvoice"  width="700px" model="@load(vm.penjualanModel)"
                      selectedIndex="@bind(vm.selectedIndex)"
                      onModelDataChange="@command('updateModelData')"
                      sizedByContent="true"
                      itemRenderer="zkexample.invoice.InplaceEditingInvoiceRenderer"
            >
            </listbox>
            <hlayout>
                <button label="add new" onClick="@command('addNew')" />
            </hlayout>
        </vlayout>
    </hlayout>
</div>        

</panelchildren>
       </panel>
    </tabpanel>

</tabpanels>

</tabbox>

</window> </zk>

InvoiceVM.java

public class InvoiceVM {
private ListModelList<Penjualan> _penjualanModel;
private int _selectedIndex = -1;
private Listbox listboxInvoice;

@WireVariable private CRUDPenjualanService CRUDPenjualanService;


public ListModel<Penjualan> getPenjualanModel () {
    if (_penjualanModel == null) {
        List<Penjualan> l = new ArrayList<Penjualan>();
        _penjualanModel = new ListModelList<Penjualan>(l);
    }
    return _penjualanModel;
}

public void setSelectedIndex (int selectedIndex) {
    _selectedIndex = selectedIndex;
}
public int getSelectedIndex () {
    return _selectedIndex;
}

//add new line
@Command
public void addNew() {
    if (_selectedIndex >= 0) {
        _penjualanModel.add(_selectedIndex+1, new Penjualan ("","",BigDecimal.ZERO,0,BigDecimal.ZERO,BigDecimal.ZERO));
    } else {
        _penjualanModel.add(new Penjualan ("","",BigDecimal.ZERO,0,BigDecimal.ZERO,BigDecimal.ZERO));
    }
}


    @AfterCompose
     public void initSetup(@ContextParam(ContextType.VIEW) Component view) {
    Selectors.wireComponents(view, this, false);
           CRUDPenjualanService = (CRUDPenjualanService) SpringUtil.getBean("CRUDPenjualanService");

            List<Barang> list=CRUDPenjualanService.getAll(Barang.class);
            //ok
            for(Barang b : list){
                System.out.println("barang = " + b.getNamaBarang());
            }

 }

}

InplaceEditingInvoiceRenderer.java

package zkexample.invoice; import java.math.BigDecimal; import org.zkoss.zk.ui.event.Event; import org.zkoss.zk.ui.event.Events; import org.zkoss.zk.ui.event.EventListener; import org.zkoss.zk.ui.event.InputEvent; import org.zkoss.zk.ui.select.annotation.WireVariable; import org.zkoss.zkplus.spring.SpringUtil; import org.zkoss.zul.Doublebox; import org.zkoss.zul.Intbox; import org.zkoss.zul.Label; import org.zkoss.zul.Listbox; import org.zkoss.zul.Listcell; import org.zkoss.zul.Listhead; import org.zkoss.zul.Listheader; import org.zkoss.zul.Listitem; import org.zkoss.zul.ListitemRenderer; import org.zkoss.zul.Textbox;

/** * tested with ZK 6.0.2 * @author ben


*/ public class InplaceEditingInvoiceRenderer implements ListitemRenderer<penjualan> {

@SuppressWarnings("unchecked")
public void render (Listitem listitem, Penjualan data, int index) {
    Listbox listbox = listitem.getListbox();
    if (index == 0 && listbox.getListhead() == null) {
        createListhead().setParent(listbox);
    }
    listitem.setValue(data);

    addTextboxCell(listitem, data.getKodeBarang())
        .addEventListener(Events.ON_CHANGE, getKodeBarangChangedListener(listbox, data, listitem));

    addLabelCell(listitem, data.getNamaBarang());
    Doublebox harga = addDoubleboxCell(listitem, data.getHarga());

    Intbox qty = addIntboxCell(listitem, data.getQty());
    qty.addEventListener(Events.ON_CHANGE, getQtyChangedListener(listbox, qty, data, listitem));

    Doublebox diskon = addDoubleboxCell(listitem, data.getDiskon());
    Doublebox totalHarga = addDoubleboxCell(listitem, data.getTotalHarga());
}

private Listhead createListhead () {

    Listhead lh = new Listhead();
    createListheader("Kode Barang", "kodeBarang").setParent(lh);
    createListheader("Nama Barang", "namaBarang").setParent(lh);
    createListheader("Harga", "harga").setParent(lh);
    createListheader("Qty", "qty").setParent(lh);
    createListheader("Diskon", "diskon").setParent(lh);
    createListheader("Total Harga", "totalHarga").setParent(lh);
    return lh;
}

private Listheader createListheader (String label, String fieldToCompare) {
    Listheader lhr = new Listheader(label);
    lhr.setSortAscending(new PersonComparator(true, fieldToCompare));
    lhr.setSortDescending(new PersonComparator(false, fieldToCompare));
    return lhr;
}

private Textbox addTextboxCell (Listitem listitem, String value) {
    Listcell lc = new Listcell();
    Textbox tbx = new Textbox(value);
    tbx.setParent(lc);
    lc.setParent(listitem);
    return tbx;
}

private Label addLabelCell (Listitem listitem, String value) {
    Listcell lc = new Listcell();
    Label lbl = new Label(value);
    lbl.setParent(lc);
    lc.setParent(listitem);
    return lbl;
}

private Doublebox addDoubleboxCell (Listitem listitem, BigDecimal value) {
    Listcell lc = new Listcell();
    Doublebox ibx = new Doublebox();
    if (value != null) {
        ibx.setValue(value.doubleValue()); //.setValue(value);
    }
    ibx.setParent(lc);
    lc.setParent(listitem);
    return ibx;
}

private Intbox addIntboxCell (Listitem listitem, Integer value) {
    Listcell lc = new Listcell();
    Intbox ibx = new Intbox();
    if (value != null) {
        ibx.setValue(value);
    }
    ibx.setParent(lc);
    lc.setParent(listitem);
    return ibx;
}

@SuppressWarnings("rawtypes")
private EventListener getKodeBarangChangedListener (final Listbox listbox, final Penjualan oldData, final Listitem listitem){{
    return new EventListener () {
        public void onEvent (Event event) {
            InputEvent ievent = (InputEvent)event;
            Barang barang=new Barang();
            Order order=new Order();

            //EROR here while CELL KodeBarang lost focus or changed
            barang=order.getBarang(ievent.getValue());
            System.out.println("kodeBarang = " + ievent.getValue() + " namanya = "+ barang.getNamaBarang());


        }
    };
}

}

Order.java

public class Order {

@WireVariable private CRUDPenjualanService CRUDPenjualanService;

public Barang getBarang(String code){
    CRUDPenjualanService = (CRUDPenjualanService) SpringUtil.getBean("CRUDPenjualanService");
    return (Barang)CRUDPenjualanService.getByCode(code);
}

}

Erorr org.springframework.orm.hibernate3.HibernateSystemException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here; nested exception is org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:690) at org.springframework.orm.hibernate3.HibernateExceptionTranslator.convertHibernateAccessException(HibernateExceptionTranslator.java:89) at org.springframework.orm.hibernate3.HibernateExceptionTranslator.translateExceptionIfPossible(HibernateExceptionTranslator.java:68) at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:58) at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213) at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:163) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202) at com.sun.proxy.$Proxy35.getByCode(Unknown Source) at zkexample.invoice.CRUDPenjualanServiceImpl.getByCode(CRUDPenjualanServiceImpl.java:26) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:318) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:196) at com.sun.proxy.$Proxy36.getByCode(Unknown Source) at zkexample.invoice.Order.getBarang(Order.java:20) at zkexample.invoice.InplaceEditingInvoiceRenderer$1.onEvent(InplaceEditingInvoiceRenderer.java:128) at org.zkoss.zk.ui.AbstractComponent.onEvent(AbstractComponent.java:2746) at org.zkoss.zk.ui.AbstractComponent.service(AbstractComponent.java:2717) at org.zkoss.zk.ui.AbstractComponent.service(AbstractComponent.java:2658) at org.zkoss.zk.ui.impl.EventProcessor.process(EventProcessor.java:136) at org.zkoss.zk.ui.impl.EventProcessingThreadImpl.process0(EventProcessingThreadImpl.java:536) at org.zkoss.zk.ui.impl.EventProcessingThreadImpl.run(EventProcessingThreadImpl.java:463)

Thanks,

delete flag offensive retag edit

10 Answers

Sort by ยป oldest newest most voted
1
link publish delete flag offensive edit
0

answered 2014-06-17 00:41:52 +0800

mhj gravatar image mhj flag of Brazil
806 1 7

Hello onsir, it seems your project is with a spring config problem. look at this links: link1

link2 maybe can helps you...

link publish delete flag offensive edit
0

answered 2014-06-17 02:26:00 +0800

onsir gravatar image onsir
132 2

thanks,mhj its work

link publish delete flag offensive edit
0

answered 2014-06-17 07:42:13 +0800

onsir gravatar image onsir
132 2

updated 2014-06-17 07:46:58 +0800

why the cell Nama Barang does not contain automatically, while cell Kode Barang is changed, i has modified like this

@SuppressWarnings("rawtypes")
private EventListener getKodeBarangChangedListener (final Listbox listbox, final Penjualan oldData, final Listitem listitem){//,final CRUDPenjualanService service) {
    return new EventListener () {
        public void onEvent (Event event) {
            InputEvent ievent = (InputEvent)event;
            Barang barang=new Barang();
            Order order=new Order();
            barang=order.getBarang(ievent.getValue());

            Events.postEvent(ModelDataChangeEvent
                .getModelDataChangeEvent(listbox,
                 new 
                          //sets the value for the cell Nama Barang
Penjualan(ievent.getValue(),  barang.getNamaBarang()   ,oldData.getHarga(),oldData.getQty(),oldData.getDiskon(),oldData.getTotalHarga()),listitem.getIndex()));

        }
    };
}

ModelDataChangeEvent.java

package zkexample.invoice;

import org.zkoss.zk.ui.Component; import org.zkoss.zk.ui.event.Event;

/** * tested with ZK 6.0.2 * @author ben


*/ public class ModelDataChangeEvent extends Event { private static final long serialVersionUID = 3645653880934243558L;

// the changed data, can be any Object
private final Object _data;
// the index to apply this change
private int _index;


public static ModelDataChangeEvent getModelDataChangeEvent (Component target, Object data, int index) {
    return new ModelDataChangeEvent("onModelDataChange", target, data, index);
}

public ModelDataChangeEvent (String kodeBarang, Component target, Object data, int index) {
     super(kodeBarang, target);
    _data = data;
    _index = index;
}
public Object getData () {
    return _data;
}
public int getIndex () {
    return _index;
}

}

thanks

link publish delete flag offensive edit
0

answered 2014-06-18 02:12:47 +0800

onsir gravatar image onsir
132 2

updated 2014-06-18 02:21:54 +0800

ok is solved , i change method updateModelData in InvoiceVM.java like this

@Command
public void updateModelData(@ContextParam(ContextType.TRIGGER_EVENT) ModelDataChangeEvent event) {
    Penjualan oldPenjualan = _penjualanModel.get(event.getIndex());;
    Penjualan newPenjualan = (Penjualan)event.getData();
    oldPenjualan.setKodeBarang(newPenjualan.getKodeBarang());
    Barang barang=new Barang();
    barang=CRUDPenjualanService.getByCode(newPenjualan.getKodeBarang());

    oldPenjualan.setNamaBarang(barang.getNamaBarang());
    oldPenjualan.setHarga(barang.getHarga());

    oldPenjualan.setQty(newPenjualan.getQty());
    oldPenjualan.setDiskon(newPenjualan.getDiskon());

    BigDecimal totalHarga=(newPenjualan.getHarga().multiply(new BigDecimal(newPenjualan.getQty())))
                          .subtract(newPenjualan.getDiskon());
    oldPenjualan.setTotalHarga(totalHarga);

}

and i made method calculate,

@Command
@NotifyChange("penjualanModel")
public void calculate() {
   List<Penjualan> l = new ArrayList<Penjualan>();
        for (Penjualan p : _penjualanModel.getInnerList()) {
            l.add(new Penjualan(p.getKodeBarang(),p.getNamaBarang(),p.getHarga(),p.getQty(),p.getDiskon(),p.getTotalHarga()));
        }
   _penjualanModel = new ListModelList<Penjualan>(l);
   _penjualanModel.add(new Penjualan ("","",BigDecimal.ZERO,0,BigDecimal.ZERO,BigDecimal.ZERO));
}

then, how it will calculate method on the process when a cell Kode Barang is changed ? thanks

link publish delete flag offensive edit
0

answered 2014-06-18 02:29:13 +0800

onsir gravatar image onsir
132 2

Hello, mr Senthilchettyin,

Thank you Sir, that I can code from your tutorials, but I modified a bit.

i like your tutorial, and like song Kumar sanu and Udit narayan too

link publish delete flag offensive edit
0

answered 2014-06-19 04:57:10 +0800

onsir gravatar image onsir
132 2

hello all, my problem unsolved have an idea ?

link publish delete flag offensive edit
0

answered 2014-06-21 04:09:32 +0800

onsir gravatar image onsir
132 2

hello, i tried with add code like this

made new class CalculateModelDataChangeEvent.java

package zkexample.invoice;

import org.zkoss.zk.ui.Component; import org.zkoss.zk.ui.event.Event;

/** * tested with ZK 6.0.2 * @author ben


*/ public class CalculateModelDataChangeEvent extends Event { private static final long serialVersionUID = 3645653880934243558L;

// the changed data, can be any Object
private final Object _data;
// the index to apply this change
private int _index;

/**
 * Get a ModelDataChangeEvent
 * @param target the target to post this event
 * @param data the changed data
 * @param index the index to apply this change
 * @return ModelDataChangeEvent
 */
public static CalculateModelDataChangeEvent getCalculateModelDataChangeEvent (Component target,Object data, int index) {
    return new CalculateModelDataChangeEvent("onCalculateModelDataChange", target,data,index);
}
/**
 * 
 * @param name event name, should starts with "on"
 * @param target
 * @param data
 * @param index
 */
//public ModelDataChangeEvent (String name, Component target, Object data, int index) {
public CalculateModelDataChangeEvent (String name, Component target, Object data, int index) {
    //super(name, target);
    super(name, target);
    _data = data;
    _index = index;
}
public Object getData () {
    return _data;
}
public int getIndex () {
    return _index;
}

}

then add code in

@SuppressWarnings("rawtypes")
private EventListener getKodeBarangChangedListener (final Listbox listbox, final Penjualan oldData, final Listitem listitem){//,final CRUDPenjualanService service) {
    return new EventListener () {
        public void onEvent (Event event) {
            InputEvent ievent = (InputEvent)event;
            System.out.println("ini diprose change kode barang");
            Events.postEvent(ModelDataChangeEvent.getModelDataChangeEvent(listbox,
                                                                          new Penjualan(ievent.getValue(),oldData.getNamaBarang(),oldData.getHarga(),oldData.getQty(),oldData.getDiskon(),oldData.getTotalHarga()),
                                                                          listitem.getIndex())
                             );
            Events.postEvent(CalculateModelDataChangeEvent.getCalculateModelDataChangeEvent(listbox,
                                                                                            null,// new Penjualan(ievent.getValue(),oldData.getNamaBarang(),oldData.getHarga(),oldData.getQty(),oldData.getDiskon(),oldData.getTotalHarga()),
                                                                                            listitem.getIndex())
                             );

        }
    };
}

then in invoice.zul change be

<listbox id="listboxInvoice"  width="700px" model="@load(vm.penjualanModel)"
                      selectedIndex="@bind(vm.selectedIndex)"
                      onModelDataChange="@command('updateModelData')"
                      onCalculateModelDataChange="@command('calculate')"

                      sizedByContent="true"
                      itemRenderer="zkexample.invoice.InplaceEditingInvoiceRenderer"
            >

then how to get focus in cell Qty ?

link publish delete flag offensive edit
0

answered 2014-06-24 02:30:53 +0800

onsir gravatar image onsir
132 2

ups, over 7 days, not yet solved

link publish delete flag offensive edit
0

answered 2014-06-26 02:35:01 +0800

onsir gravatar image onsir
132 2

I has try with add code like this

@SuppressWarnings("rawtypes")
private EventListener getKodeBarangChangedListener (final Listbox listbox, final Penjualan oldData, final Listitem listitem){//,final CRUDPenjualanService service) {
    return new EventListener () {
        public void onEvent (Event event) {
            InputEvent ievent = (InputEvent)event;
                       Events.postEvent(ModelDataChangeEvent.getModelDataChangeEvent(listbox,
                                                                          new Penjualan(ievent.getValue(),oldData.getNamaBarang(),oldData.getHarga(),oldData.getQty(),oldData.getDiskon(),oldData.getTotalHarga()),
                                                                          listitem.getIndex())
                             );
            Events.postEvent(CalculateModelDataChangeEvent.getCalculateModelDataChangeEvent(listbox,
                                                                                            null,// new Penjualan(ievent.getValue(),oldData.getNamaBarang(),oldData.getHarga(),oldData.getQty(),oldData.getDiskon(),oldData.getTotalHarga()),
                                                                                            listitem.getIndex())
                             );

            //add code 
            //for set focus to cell qty
            ((Listbox)listitem.getParent()).setSelectedIndex(listitem.getIndex());
            Listitem i=((Listbox)listitem.getParent()).getItemAtIndex(listitem.getIndex());
            //cell 3 is qty
            Listcell c=(Listcell) i.getChildren().get(3);
            c.focus();
        }
    };
}

but still not working

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-06-16 11:27:57 +0800

Seen: 38 times

Last updated: Jun 26 '14

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