Skip to content

useCastAttributes ​

Resolves dynamic attribute patterns in action config strings. Supports three syntaxes for binding item values into labels, URLs, or expressions without writing custom Vue logic.

File: vue/src/js/hooks/useCastAttributes.js


Syntax Reference ​

PatternExampleResolves to
Dot-notation $key.sub$user.nameitem.user.name
Wildcard $items.*.title$items.*.titleJoined values of the title key from all items in the array
Eval $(expr)$$($price * 1.18)$Result of evaluated JS expression (item values substituted first)

Usage ​

js
import { useCastAttributes } from '@/hooks'

const { castAttribute, castObjectAttributes } = useCastAttributes()

const item = { id: 5, user: { name: 'Alice' }, price: 100 }

// Single attribute
castAttribute('$user.name', item)          // => 'Alice'
castAttribute('$( $price * 1.18 )$', item) // => 118

// Deep object (all string values in the object are cast)
castObjectAttributes({ label: 'Hello $user.name', value: '$id' }, item)
// => { label: 'Hello Alice', value: '5' }

Returns ​

NameSignatureDescription
matchAttribute(value) => BooleanTrue if value contains a dot-notation pattern
matchStandardAttribute(value) => BooleanTrue if value matches $key
matchEvalAttribute(value) => BooleanTrue if value matches $(...)$
matchAnyPattern(value) => BooleanTrue if any pattern matches
castAttribute(value, item) => anyCast a single value against an item
castStandardAttribute(value, item, options?) => anyCast a $key pattern; supports clearAsterisk option
castEvalAttribute(value, item) => anyEvaluate a $(expr)$ expression
castObjectAttribute(value, item, options?) => anyCast a single string value
castObjectAttributes(data, item) => anyRecursively cast all string values in an object or array

Where it is used ​

useItemActions calls castObjectAttributes(action, editingItem) before rendering each action button, allowing action labels and endpoint URLs to reference the current row item:

php
// In module config
'actions' => [
    [
        'type'     => 'blank',
        'label'    => 'View $name',
        'endpoint' => '/preview/:id',
    ]
]

See Also ​