![](/img/trans.png)
[英]convert the JSON string to a JavaScript object then create a HTML table
[英]Convert JSON string to object and display as HTML table
我有一个带有onchange event
的dropdown select option
,它将通过AJAX
将所选值发送到我的控制器,然后根据MySQL数据库中的选定值查询数据并将其作为JSON
对象返回给我的刀片。
我的问题是来自mysql数据库的JSON
数据当前在字符串中作为单行。 如何将其转换回普通的JSON
对象,并在新窗口中将基于列名的每个值显示为HTML
表。
假设是这样的过程
用于将JSON插入MySQL数据库的控制器代码
$sqlquery = DB::connection('sqlsrv')
->table('Stocks')
->join('StockTransactions', 'Stocks.Id', '=', 'StockTransactions.StockId')
->select(
'TransactionDate',
'DocumentCode',
'DocType',
'StockName',
DB::raw("FORMAT(SUM(UOMQty), 'N') AS TotalUOMQty"),
DB::raw("FORMAT(SUM(UOMPrice), 'N') AS TotalUOMPrice"),
DB::raw("FORMAT(CurrentBalance, 'N') AS CurrentBalance"),
DB::raw("FORMAT(((SUM(UOMPrice) / SUM(UOMQty)) * CurrentBalance), 'N') AS Total")
)
->where([
['UOMQty', '>', '0'],
['DocumentCode', 'NOT LIKE', 'SA%'],
['DocumentCode', 'NOT LIKE', 'SIN%']
])
->WhereBetween('TransactionDate', ['01-01-2000', $dateto])
->groupBy('StockName', 'TransactionDate', 'CurrentBalance', 'DocumentCode', 'DocType')
->orderBy('TransactionDate', 'ASC')
->get();
$array = json_decode(json_encode($sqlquery), true);
$json = json_encode($array);
DB::connection('mysql')
->table('records')
->insert(['record' => $json, 'date_from' => "2000-01-01", 'date_to' => "$dateto", 'date_inquiry' => "$getCurrentDateTimeTo"]);
用于从MySQL数据库获取JSON的Controllor代码
public function getmysqldata(Request $request)
{
$request_data = $request->all();
$dateInquiry = $request_data['dateInquiry'];
$result = DB::connection('mysql')
->table('records')
->where([
'date_inquiry' => "$dateInquiry"
])
->get();
return response()->json($result);
}
Index.blade.php
Record Available :
<select name="recordavailable" id="recordavailable">
@if(!empty($inquiry_date))
<option value="">Please select records</option>
@foreach ($inquiry_date as $date_inquiry)
<option value="{{ $date_inquiry->date_inquiry}}">{{ $date_inquiry->date_inquiry}}</option>
@endforeach
@else
option value="">No records</option>
@endif
</select>
脚本
$(document).on("change", 'select#recordavailable', function(e)
{
var dateInquiry = $(this).val();
$.ajax
({
type: "GET",
data: 'dateInquiry='+dateInquiry,
url: 'getmysqldata',
dataType: 'JSON',
success: function(data)
{
var randomnumber = Math.floor((Math.random()*100)+1);
var winPrint = window.open('',"",'PopUp'+randomnumber+', "scrollbars=1,menubar=0,resizable=1"');
winPrint.document.write('<title>Display Record of '+dateInquiry+'</title><style>table{ border-collapse: collapse;border-spacing: 0;'+
'width: 100%;border: 1px solid #ddd;}table, td, th{border: 1px solid black;text-align:center;padding: 5px;}#stockname {text-align: left;}'+
'h3 {text-align:center;}#qty, #cost, #balance, #total {text-align: right;}</style><h3>'+dateInquiry+'</h3>');
winPrint.document.write(JSON.stringify(data));
}
});
});
安慰
产量
输出我希望它是
您需要将记录表中的记录列转换为数组,这将在适当的json中将其强制转换为输出。
https://laravel.com/docs/5.8/eloquent-mutators#array-and-json-casting
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.