在 Eloquent 查询或 Yajra 数据表中格式化数字
Formatting number in Eloquent query or Yarja Datatables
我正在写一个可搜索的table,其中包括面积和人口。这是基本查询:
public function getCountryData()
{
$co = DB::table('countries')->leftJoin('country_detail','country_detail.country_id','=','countries.id')->addSelect(['countries.id','countries.name','country_detail.capital','country_detail.area','country_detail.iso3','country_detail.population','country_detail.currencyName','country_detail.phone','country_detail.continent'])->get();
return Datatables::of($co)
->addColumn('action', function($co){
$btn = '<div style="float:right">
<a href="'. route('country.edit',$co->id) .' " class="btn btn-outline-secondary btn-xs" title="edit" style="margin-right:.5em">'.getEditIcon().'</a><a href="'. route('country.show', $co->id) .'" class="btn btn-outline-secondary btn-xs" title="images" style="margin-right:.5em">'.getBinoculars().'</a>';
return $btn;
}) ->rawColumns(['action'])
->make(true);
}
在我看来这一切都很好,除了 population 字段,例如 returns 类似 29121286,当然我想将其格式化为 29,121,286。
这可以在查询中完成还是在 Datatables 本身中完成?
感谢 Laracasts 上的 Navok,这里就是答案。
首先,您需要一个 javascript 函数来将字符串转换为格式正确的数字。
function formatNumber(num) {
return num.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, ',')
}
我已将它添加到一个名为 misc.js 的文件中(这样我就可以拥有其他东西),我从 layouts.app 调用它,因此它在系统范围内可用。
可以在 https://blog.abelotech.com/posts/number-currency-formatting-javascript/ 找到。
然后在您页面上的数据表声明中添加
createdRow: function (row, data, dataIndex) {
if (data.population !== undefined) {
// 4 here is the cell number, it starts from 0 where this number should appear
$(row).find('td:eq(4)').html(formatNumber(data.population));
}
},
我希望这是有价值的。
我建议使用 php 的 number_format
函数来格式化您的号码,而不是使用 javascript 在客户端格式化号码。
return Datatables::editColumn('population', function($item) {
return number_format($item->population);
});
有关格式化数字的更多帮助,请参阅 https://www.php.net/manual/en/function.number-format.php
我正在写一个可搜索的table,其中包括面积和人口。这是基本查询:
public function getCountryData()
{
$co = DB::table('countries')->leftJoin('country_detail','country_detail.country_id','=','countries.id')->addSelect(['countries.id','countries.name','country_detail.capital','country_detail.area','country_detail.iso3','country_detail.population','country_detail.currencyName','country_detail.phone','country_detail.continent'])->get();
return Datatables::of($co)
->addColumn('action', function($co){
$btn = '<div style="float:right">
<a href="'. route('country.edit',$co->id) .' " class="btn btn-outline-secondary btn-xs" title="edit" style="margin-right:.5em">'.getEditIcon().'</a><a href="'. route('country.show', $co->id) .'" class="btn btn-outline-secondary btn-xs" title="images" style="margin-right:.5em">'.getBinoculars().'</a>';
return $btn;
}) ->rawColumns(['action'])
->make(true);
}
在我看来这一切都很好,除了 population 字段,例如 returns 类似 29121286,当然我想将其格式化为 29,121,286。
这可以在查询中完成还是在 Datatables 本身中完成?
感谢 Laracasts 上的 Navok,这里就是答案。
首先,您需要一个 javascript 函数来将字符串转换为格式正确的数字。
function formatNumber(num) {
return num.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, ',')
}
我已将它添加到一个名为 misc.js 的文件中(这样我就可以拥有其他东西),我从 layouts.app 调用它,因此它在系统范围内可用。
可以在 https://blog.abelotech.com/posts/number-currency-formatting-javascript/ 找到。
然后在您页面上的数据表声明中添加
createdRow: function (row, data, dataIndex) {
if (data.population !== undefined) {
// 4 here is the cell number, it starts from 0 where this number should appear
$(row).find('td:eq(4)').html(formatNumber(data.population));
}
},
我希望这是有价值的。
我建议使用 php 的 number_format
函数来格式化您的号码,而不是使用 javascript 在客户端格式化号码。
return Datatables::editColumn('population', function($item) {
return number_format($item->population);
});
有关格式化数字的更多帮助,请参阅 https://www.php.net/manual/en/function.number-format.php