英文:
why my laravel only compute the quantity on the same page and not all the selected item?
问题
以下是翻译好的内容:
selected item before doing the searching
我正在制作一个类似电子商务系统的系统。系统工作正常。但是在我使用页面顶部的搜索栏进行搜索并根据搜索结果选择项目后,它不会计算我在使用搜索栏进行搜索之前选择的项目。总结一下,我猜测它只有在项目在同一页时才计算选定的项目。在这里,我将页面长度限制为200。
result before doing the search
这是成功计算的结果。
这是我使用搜索栏时发生的情况。
正如你所看到的,它没有计算之前的两个项目。
我将提供代码。
web_item.blade
@extends('frontend.layouts.app')
@section('content')
<link rel="stylesheet" href="{{ asset('css/tablestyle.css') }}" />
<link rel="stylesheet" href="https://cdn.datatables.net/1.11.3/css/jquery.dataTables.min.css">
{!! Form::open(['route' => 'frontend.webitem.confirm', 'method' => 'POST']) !!}
{!! Form::submit('Confirm', ['class' => 'btn btn-primary', 'style' => 'float:right;']) !!}
<br>
<table id="webitem-table" class="table table-bordered">
<thead>
<tr>
<th>Part No</th>
<th>Description</th>
<th>Supp Part</th>
<th>RK Qty</th>
<th>Dealer Price</th>
<th>Workshop</th>
<th>End User Price</th>
<th>Quantity</th>
<th>Customer Price</th>
<th>Part Search</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
{!! Form::close() !!}
<script>
$(document).ready(function () {
$('#webitem-table').DataTable({
processing: true,
ajax: '{!! route('frontend.webitem.webitemtable') !!}',
columns: [
{data: 'part_no', name: 'part_no', searchable: true},
{data: 'description', name: 'description', searchable: true},
{data: 'supp_part', name: 'supp_part'},
{data: 'rk_qty', name: 'rk_qty'},
{data: 'dealer_price', name: 'dealer_price'},
{data: 'workshop', name: 'workshop'},
{data: 'enduser_price', name: 'enduser_price'},
{
data: 'quantity',
name: 'quantity',
orderable: false,
searchable: false,
render: function (data, type, row) {
return '<input type="number" min="0" name="quantity[' + row.id + ']" value="0">';
}
},
{
data: 'user_price',
name: 'user_price',
orderable: false,
searchable: false,
render: function (data, type, row) {
return '<input type="number" min="0" step="0.01" name="user_price[' + row.id + ']" value="' + data + '';
}
},
{data: 'part_search', name: 'part_search', visible: false, searchable: true}
],
lengthMenu: [100, 200, 500, 1000], // Add this option to show 200, 500, 1000 and All
pageLength: 100, // Set default page length to 200
responsive: true, // Enable responsive design
});
});
</script>
@endsection
web_confirm.blade
@extends('frontend.layouts.app')
@section('content')
<div class="container">
<h1>Selected Items</h1>
<table class="table table-bordered">
<thead>
<tr>
<th>Part No</th>
<th>Description</th>
<th>Supp Part</th>
<th>Customer Price</th>
<th>Quantity</th>
<th>Total Price</th>
</tr>
</thead>
<tbody>
@foreach ($selectedItems as $selectedItem)
<tr>
<td>{{ $selectedItem['item']->part_no }}</td>
<td>{{ $selectedItem['item']->description }}</td>
<td>{{ $selectedItem['item']->supp_part }}</td>
<td>{{ $selectedItem['userPrice'] > 0 ? number_format((float) $selectedItem['userPrice'], 2, '.', '') : 'N/A' }}</td>
<td>{{ $selectedItem['quantity'] }}</td>
<td>{{ number_format((float) $selectedItem['itemTotal'], 2, '.', '') }}</td>
</tr>
@endforeach
</tbody>
</table>
<h2>Total Price: RM {{ $total }}</h2>
<button type="submit" class="btn btn-primary">Confirm Order</button>
</div>
@endsection
这是名为ItemController.php的控制器
<?php
namespace App\Http\Controllers\Frontend\webitem;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Access\Webitems;
use App\Models\Access\WebTest;
use Yajra\Datatables\Facades\Datatables;
use App\Models\Order;
class ItemController extends Controller
{
public function index()
{
return view('frontend.webitem.web_item');
}
public function webitemtable(Request $request)
{
$search = $request->input('search.value'); // 从DataTables获取搜索输入
$query = Webitems::query();
// 根据'part_no'和'description'列应用搜索过滤器
if ($search) {
$query->where('part_no', 'LIKE', "%$search%")
->orWhere('description', 'LIKE', "%$search%");
}
// 获取过滤后的项目
$items = $query->get();
return Datatables::of($items)
->addColumn('checkbox', function ($item) {
return '<input type="checkbox" data-id="' . $item->id . '" name="selected[]" value="' . $item->id . '" data-group="checkbox-group">';
})
->addColumn('action', function ($item) {
return '<input type="number" min="0" name="quantity[' . $item->id . ']" value="0">';
})
->addColumn('user_price', function ($item) {
return '<input type="number" min="0" step="0.01" name="user_price[' . $item->id . ']" value="0">';
})
->rawColumns(['checkbox', 'action', 'user_price'])
->make(true);
}
public function confirm(Request $request)
{
$selectedItems = [];
$total = 0;
$items = Webitems::all();
foreach ($items as $item) {
$quantity = $request->input('quantity.' . $item->id, 0);
$userPrice = $request->input('user_price.' . $item->id, 0);
if ($quantity > 0) {
$selectedItems[] = [
'item' => $item,
'quantity' => $quantity,
'userPrice' => $userPrice,
'itemTotal' => $userPrice * $quantity,
];
$total += $userPrice * $quantity;
}
}
$total = number_format((float) $total, 2, '.', ''); // 将总价格式化为2位小数
return view('frontend.webitem.web_confirm')->with([
'selectedItems' => $selectedItems,
'total' => $total,
'request' => $request,
]);
}
}
这是路由
Route::group(['prefix' => 'webitem', 'namespace' => 'webitem', 'middleware' => 'auth'], function () {
Route::get('index', 'ItemController@index')->name('webitem.index');
Route::get('webitemtable', 'ItemController@webitemtable')->name('webitem.webitemtable');
Route::get('index_test', 'ItemController@index_test')->name('webitem.index_test');
Route::get('getDataTable', 'ItemController@getDataTable')->name('webitem.getDataTable');
Route::post('confirm', 'ItemController@confirm')->name('webitem.confirm');
Route::post('/order/confirm', 'ItemController@confirmOrder')->name('order.confirm');
Route::post('/order/confirm', 'OrderController@confirmOrder')->name('order.confirm');
Route::post('/submit-order', 'OrderController@submitOrder')->name('submit.order');
Route::post('/frontend/webitem/process', 'Frontend\webitem\ItemController@confirm')->name('frontend.webitem.process');
英文:
selected item before doing the searching
I make an e-commerce like system. the system work fine. but after i use the search bar at the top of the page, and selected the item based on the result of the search, it wont compute the item i selected before i make a search using the search bar.In conclusion, i guess it only counted the selected item if the item are in the same page. here I limit the page length to 200.
result before doing the search
this was successfully computed.
this is what happen when i use the search bar.
as you can see, it doent count the previous 2 item.
i will provide the code.
web_item.blade
@extends('frontend.layouts.app')
@section('content')
<link rel="stylesheet" href="{{ asset('css/tablestyle.css') }}" />
<link rel="stylesheet" href="https://cdn.datatables.net/1.11.3/css/jquery.dataTables.min.css">
{!! Form::open(['route' => 'frontend.webitem.confirm', 'method' => 'POST']) !!}
{!! Form::submit('Confirm', ['class' => 'btn btn-primary', 'style' => 'float:right;']) !!}
<br>
<table id="webitem-table" class="table table-bordered">
<thead>
<tr>
<th>Part No</th>
<th>Description</th>
<th>Supp Part</th>
<th>RK Qty</th>
<th>Dealer Price</th>
<th>Workshop</th>
<th>End User Price</th>
<th>Quantity</th>
<th>Customer Price</th>
<th>Part Search</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
{!! Form::close() !!}
<script>
$(document).ready(function () {
$('#webitem-table').DataTable({
processing: true,
ajax: '{!! route('frontend.webitem.webitemtable') !!}',
columns: [
{data: 'part_no', name: 'part_no', searchable: true},
{data: 'description', name: 'description', searchable: true},
{data: 'supp_part', name: 'supp_part'},
{data: 'rk_qty', name: 'rk_qty'},
{data: 'dealer_price', name: 'dealer_price'},
{data: 'workshop', name: 'workshop'},
{data: 'enduser_price', name: 'enduser_price'},
{
data: 'quantity',
name: 'quantity',
orderable: false,
searchable: false,
render: function (data, type, row) {
return '<input type="number" min="0" name="quantity[' + row.id + ']" value="0">';
}
},
{
data: 'user_price',
name: 'user_price',
orderable: false,
searchable: false,
render: function (data, type, row) {
return '<input type="number" min="0" step="0.01" name="user_price[' + row.id + ']" value="' + data + '';
}
},
{data: 'part_search', name: 'part_search', visible: false, searchable: true}
],
lengthMenu: [100, 200, 500, 1000], // Add this option to show 200, 500, 1000 and All
pageLength: 100, // Set default page length to 200
responsive: true, // Enable responsive design
});
});
</script>
@endsection
web_confirm.blade
@extends('frontend.layouts.app')
@section('content')
<div class="container">
<h1>Selected Items</h1>
<table class="table table-bordered">
<thead>
<tr>
<th>Part No</th>
<th>Description</th>
<th>Supp Part</th>
<th>Customer Price</th>
<th>Quantity</th>
<th>Total Price</th>
</tr>
</thead>
<tbody>
@foreach ($selectedItems as $selectedItem)
<tr>
<td>{{ $selectedItem['item']->part_no }}</td>
<td>{{ $selectedItem['item']->description }}</td>
<td>{{ $selectedItem['item']->supp_part }}</td>
<td>{{ $selectedItem['userPrice'] > 0 ? number_format((float) $selectedItem['userPrice'], 2, '.', '') : 'N/A' }}</td>
<td>{{ $selectedItem['quantity'] }}</td>
<td>{{ number_format((float) $selectedItem['itemTotal'], 2, '.', '') }}</td>
</tr>
@endforeach
</tbody>
</table>
<h2>Total Price: RM {{ $total }}</h2>
<button type="submit" class="btn btn-primary">Confirm Order</button>
</div>
@endsection
the is the controller named itemController.php
<?php
namespace App\Http\Controllers\Frontend\webitem;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Access\Webitems;
use App\Models\Access\WebTest;
use Yajra\Datatables\Facades\Datatables;
use App\Models\Order;
class ItemController extends Controller
{
public function index()
{
return view('frontend.webitem.web_item');
}
public function webitemtable(Request $request)
{
$search = $request->input('search.value'); // Get the search input from DataTables
$query = Webitems::query();
// Apply search filters based on 'part_no' and 'description' columns
if ($search) {
$query->where('part_no', 'LIKE', "%$search%")
->orWhere('description', 'LIKE', "%$search%");
}
// Fetch filtered items
$items = $query->get();
return Datatables::of($items)
->addColumn('checkbox', function ($item) {
return '<input type="checkbox" data-id="' . $item->id . '" name="selected[]" value="' . $item->id . '" data-group="checkbox-group">';
})
->addColumn('action', function ($item) {
return '<input type="number" min="0" name="quantity[' . $item->id . ']" value="0">';
})
->addColumn('user_price', function ($item) {
return '<input type="number" min="0" step="0.01" name="user_price[' . $item->id . ']" value="0">';
})
->rawColumns(['checkbox', 'action', 'user_price'])
->make(true);
}
public function confirm(Request $request)
{
$selectedItems = [];
$total = 0;
$items = Webitems::all();
foreach ($items as $item) {
$quantity = $request->input('quantity.' . $item->id, 0);
$userPrice = $request->input('user_price.' . $item->id, 0);
if ($quantity > 0) {
$selectedItems[] = [
'item' => $item,
'quantity' => $quantity,
'userPrice' => $userPrice,
'itemTotal' => $userPrice * $quantity,
];
$total += $userPrice * $quantity;
}
}
$total = number_format((float) $total, 2, '.', ''); // format the total to 2 decimal places
return view('frontend.webitem.web_confirm')->with([
'selectedItems' => $selectedItems,
'total' => $total,
'request' => $request,
]);
}
}
this is the route
Route::group(['prefix' => 'webitem', 'namespace' => 'webitem', 'middleware' => 'auth'], function () {
Route::get('index', 'ItemController@index')->name('webitem.index');
Route::get('webitemtable', 'ItemController@webitemtable')->name('webitem.webitemtable');
Route::get('index_test', 'ItemController@index_test')->name('webitem.index_test');
Route::get('getDataTable', 'ItemController@getDataTable')->name('webitem.getDataTable');
Route::post('confirm', 'ItemController@confirm')->name('webitem.confirm');
Route::post('/order/confirm', 'ItemController@confirmOrder')->name('order.confirm');
Route::post('/order/confirm', 'OrderController@confirmOrder')->name('order.confirm');
Route::post('/submit-order', 'OrderController@submitOrder')->name('submit.order');
Route::post('/frontend/webitem/process', 'Frontend\webitem\ItemController@confirm')->name('frontend.webitem.process');
on the routes there might be more than needed because ive tried a few things, so just ignore the extra routes and provide with you suggestion
答案1
得分: 0
我认为你需要使用会话(session)来保存所有选择的项目。因为根据你的代码,我可以说每次进入选定项目页面时,只会显示最新选择的项目。所以你需要实现“会话”来保留先前的选择。
英文:
I think you have to use session to save all the selected items. Because seeing at your code I can say that every time you go to selected items page it will reflect only latest selected items. So you need to implement Session
to retain your previous selections.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论