[英]Displaying the value in a Laravel Nova Value metric
I need to show the sum of the price
column of model Foo
. 我需要显示模型
Foo
的price
列的总和。 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. 其中显示以下输出。
22
=> $22
22
=> $22
3120
=> $3.10k
3120
=> $3.10k
I need to just show $22
, $3120
without any formatting. 我只需要显示
$22
, $3120
而无需任何格式。 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. 从Nova
1.3.1
我们可以使用Numeral.js
格式来格式化Trend/Value
指标中的Trend/Value
。
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 从v1.2.0开始
The format happens in nova/resources/js/components/Metrics/Base/ValueMetric.vue
该格式发生在
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. 然后运行
npm run prod
以构建并运行php artisan nova:publish
命令以复制更新的文件。
Note - Your changes will get override when you update Nova version in future. 注意 -您的更改将在以后更新Nova版本时被覆盖。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.