vuejs / Discussion

Vue.js discussion
167 stars 17 forks source link

V-model issue for Number type values #466

Open Anshul-Batra opened 9 years ago

Anshul-Batra commented 9 years ago

Hi,

Refer to below jsFiddle: http://jsfiddle.net/ha04broe/6/

I am facing an issue with v-model. When I bind an input field to say rows.data using v-model like below

<input type="number" v-model="rows.data">

then on changing the input field (type something and blur out) it will automatically create "data" property in "rows" object and assign value of input to "data". Currently if we type 23 in input field then it will assign rows.data="23". As we are expecting it as number so on API side we are parsing it to int. But if we type some thing and then clears out field the it sending data like below: rows.data="" which when parsing to int says Nan which is obvious but our API code is breaking because of this.

If v-model is creating an object property when not in existence then why it is not deleting the property when data is cleared out.

We want that when there is no data in a property then that property should not go to backend.

so instead of- "rows":{ "data" : "" }... it should send- "rows":{}

Though we can do this using onchange even but ours is a big project. There are more than 100 fields in one page. We don't want to implement onchange for almost all fields.

So if there is any solution in Vuejs please let us know

young-steveo commented 9 years ago

FIFY: http://jsfiddle.net/ha04broe/7/

Anshul-Batra commented 9 years ago

Hi,

Zero will not work.. As user is not setting any value and in return user is getting zero for that field. That would be kind of inconsistency.. nIs there a way to delete that property?

@yyx990803 .. Evan could please help me on this..

Thanks, Anshul

young-steveo commented 9 years ago

@Anshul-Batra I'm confused... if the property were undefined, your parseInt would be NaN anyway.

Anshul-Batra commented 9 years ago

In that case, we will not process that property which is not available..

Centaur commented 8 years ago

The v-model type of input[type=number] should be Number not String, though a two-way filter might help.