Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Bind to nested reactive property#16267

Answeredbybrunnerh
SEUH asked this question inQ&A
Discussion options

I have an accessorKey "properties.amount" and i have an object "{properties: {amount: '123', ...}, ...}". Now i want to bind to the objects property. I haven't found any solution for this problem. Using $derived only the variable inside the component gets reactive but it doesn't bubble up to the parent. Any ideas? Because i don't want to write multiple components like the following for obvious reasons:

<script>import {Input}from'@/components/ui/input/index.js';// def.accessorKey = 'properties.amount'// cell.row.original = {properties: {amount: '123', ...}, ...}let {def, cell}=$props();let levels=def.accessorKey.split('.');</script><divclass="flex space-x-2">    <spanclass="max-w-[500px] truncate font-medium">        {#iflevels.length===1}            <Inputbind:value={cell.row.original[levels[0]]}/>        {:elseiflevels.length===2}            <Inputbind:value={cell.row.original[levels[0]][levels[1]]}/>        {:elseiflevels.length===3}            <Inputbind:value={cell.row.original[levels[0]][levels[1]][levels[2]]}/>        {:elseiflevels.length===4}            <Inputbind:value={cell.row.original[levels[0]][levels[1]][levels[2]][levels[3]]}/>        {/if}    </span></div>
You must be logged in to vote

If you make the binding interact with the source objectand the source object is deeply stateful, this is possible.

Example:

<script>let { def, cell }=$props();let accessor=$derived.by(()=> {constparts=def.accessorKey.split('.');constproperty=parts.at(-1);let o=cell.row.original;for (constkeyofparts.slice(0,-1))o= o[key];return {getvalue() {return o[property]; },setvalue(v) { o[property]= v; },}});</script><inputbind:value={accessor.value} />

Playground

In the case of shallow reactivity, thecell needs to be$bindable and the entire object has to be swapped out with an updated version, which is a bit more involved.

Replies: 1 comment 2 replies

Comment options

If you make the binding interact with the source objectand the source object is deeply stateful, this is possible.

Example:

<script>let { def, cell }=$props();let accessor=$derived.by(()=> {constparts=def.accessorKey.split('.');constproperty=parts.at(-1);let o=cell.row.original;for (constkeyofparts.slice(0,-1))o= o[key];return {getvalue() {return o[property]; },setvalue(v) { o[property]= v; },}});</script><inputbind:value={accessor.value} />

Playground

In the case of shallow reactivity, thecell needs to be$bindable and the entire object has to be swapped out with an updated version, which is a bit more involved.

You must be logged in to vote
2 replies
@SEUH
Comment options

This a great solution, i hadn't thought of abstracting behind a getter/setter. Thank you!

@gterras
Comment options

Really elegant solution indeed for a common problem, I wish there was a "recipies" part in the docs where one can browse useful tricks of this kind.

Answer selected bySEUH
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
3 participants
@SEUH@brunnerh@gterras

[8]ページ先頭

©2009-2025 Movatter.jp