java - Is there a way to set a validation and edit a long value in an editable Vaadin 8 Grid -


i have vaadin 8 grid set 1 column editable. have food.calories long (yes in case int keep in mind example , specific use case requires long):

binder<food> binder = foodgrid.geteditor().getbinder(); textfield caloriestextfield = new textfield(); binder.forfield(caloriestextfield)         .withvalidator(customcaloryvalidator::isvalidvalue, "must valid positive amount")         .withconverter(new stringtocaloriesconverter("must integer"))         .bind(food::getcalories, food::setcalories);  // line fails error because types not match. foodgrid.addcolumn(food::getcalories, new numberrenderer(mynumberformat))         .seteditorcomponent(new textfield(), food::setcalories); 

unfortunately doesn't work , has following error:

inferred type 'c' type parameter 'c' not within bound; should implement 'com.vaadin.data.hasvalue'

i looked everywhere , couldn't find example of beyond simple edits. demo sampler did have more complex example using slider couldn't figure out how extrapolate example...

i understand error, it's trying map long string. can't find way add converter addcolumn make work...

firstly main issue binder did not specify generic type, needed be:

binder<food> binder = foodgrid.geteditor().getbinder(); 

and not:

binder binder = foodgrid.geteditor().getbinder(); 

that being said there several other gotchas. first when forfield() need keep track of binding able set later column. wasn't clear @ me. need to:

binder.binding<food, long> caloriesbinder = binder.forfield(caloriestextfield)         .withvalidator(customcaloryvalidator::isvalidvalue, "must valid positive amount")         .withconverter(new stringtocaloriesconverter("must integer"))         .bind(food::getcalories, food::setcalories); 

i'm not 100% sure on caloriesbinder because code different , example, need binding. take binding , do:

foodgrid.getcolumn("calories").seteditorbinding(caloriesbinding); 

this allows correct editor work. in documentation example simple missed that.

the next step extremely important depending on you're displaying, add renderer otherwise can run weird issues. example if you're using long store currency need convert display currency amount. if you're using date want format it. need add renderer. way find how without compilation errors (mismatched types) was:

((grid.column<food, long>)foodgrid.getcolumn("calories")).setrenderer(new caloriesrenderer()); 

just completeness need enable editor with:

foodgrid.geteditor().setenabled(true); 

lastly, if table part of bigger bean need call foodgrid.setitems(), cannot rely on binder.readbean() because cannot accept list. example if instead of food bean meal consisted of number of ingredients, not binder.readbean(meal) nor binder.readbean(meal.getingredients) though can binder.readbean(meal) rest of form. way make work do:

binder.readbean(meal); foodgrid.setitems(meal.getingredients); 

Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -