简体   繁体   中英

Displaying the value in a Laravel Nova Value metric

I need to show the sum of the price column of model Foo . Right now I can do it with this.

    public function calculate(Request $request)
    {

        return $this
            ->sum($request, Contribution::class, 'contribution_amount')
            ->dollars();
    }

Which show the following output.

  • For sum of 22 => $22
  • For sum of 3120 => $3.10k

I need to just show $22 , $3120 without any formatting. I tried to override the aggregate function but it still doesn't give me the correct output format.

protected function aggregate($request, $model, $function, $column = null, $dateColumn = null)
    {
        $query = $model instanceof Builder ? $model : (new $model)->newQuery();

        $column = $column ?? $query->getModel()->getQualifiedKeyName();

        $previousValue = with(clone $query)->whereBetween(
            $dateColumn ?? $query->getModel()->getCreatedAtColumn(), $this->previousRange($request->range)
        )->{$function}($column);

        return $this->result(
            with(clone $query)->whereBetween(
                $dateColumn ?? $query->getModel()->getCreatedAtColumn(), $this->currentRange($request->range)
            )->{$function}($column)
        )->previous($previousValue);
    }

Can anyone give a pointer here?

For future readers..

As of Nova 1.3.1 we can use Numeral.js formatting to format the values in the Trend/Value metric.

return $this
            ->result($val)
            ->dollars()
            ->format('0,0.00')

Above snippet will format the value to be displayed to two decimal places.

As of v1.2.0

The format happens in nova/resources/js/components/Metrics/Base/ValueMetric.vue

formattedValue() {
    if (!this.isNullValue) {
        const numeralValue = numeral(this.value)

        return numeralValue.value() > 1000
            ? this.prefix + numeralValue.format('(0.00a)')
            : this.prefix + this.value
    }

    return ''
},

It is not configurable.

Workaround

You can edit above mentioned file just to return non format value. Then run npm run prod to build & run php artisan nova:publish command to copy updated files.

Note - Your changes will get override when you update Nova version in future.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM