为什么我的Laravel只在同一页上计算数量,而不是所有选定的项目?

huangapple go评论126阅读模式
英文:

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

这是成功计算的结果。

after doing the searching

这是我使用搜索栏时发生的情况。

result after doing the search

正如你所看到的,它没有计算之前的两个项目。

我将提供代码。

web_item.blade

  1. @extends('frontend.layouts.app')
  2. @section('content')
  3. <link rel="stylesheet" href="{{ asset('css/tablestyle.css') }}" />
  4. <link rel="stylesheet" href="https://cdn.datatables.net/1.11.3/css/jquery.dataTables.min.css">
  5. {!! Form::open(['route' => 'frontend.webitem.confirm', 'method' => 'POST']) !!}
  6. {!! Form::submit('Confirm', ['class' => 'btn btn-primary', 'style' => 'float:right;']) !!}
  7. <br>
  8. <table id="webitem-table" class="table table-bordered">
  9. <thead>
  10. <tr>
  11. <th>Part No</th>
  12. <th>Description</th>
  13. <th>Supp Part</th>
  14. <th>RK Qty</th>
  15. <th>Dealer Price</th>
  16. <th>Workshop</th>
  17. <th>End User Price</th>
  18. <th>Quantity</th>
  19. <th>Customer Price</th>
  20. <th>Part Search</th>
  21. </tr>
  22. </thead>
  23. <tbody>
  24. </tbody>
  25. </table>
  26. {!! Form::close() !!}
  27. <script>
  28. $(document).ready(function () {
  29. $('#webitem-table').DataTable({
  30. processing: true,
  31. ajax: '{!! route('frontend.webitem.webitemtable') !!}',
  32. columns: [
  33. {data: 'part_no', name: 'part_no', searchable: true},
  34. {data: 'description', name: 'description', searchable: true},
  35. {data: 'supp_part', name: 'supp_part'},
  36. {data: 'rk_qty', name: 'rk_qty'},
  37. {data: 'dealer_price', name: 'dealer_price'},
  38. {data: 'workshop', name: 'workshop'},
  39. {data: 'enduser_price', name: 'enduser_price'},
  40. {
  41. data: 'quantity',
  42. name: 'quantity',
  43. orderable: false,
  44. searchable: false,
  45. render: function (data, type, row) {
  46. return '<input type="number" min="0" name="quantity[' + row.id + ']" value="0">';
  47. }
  48. },
  49. {
  50. data: 'user_price',
  51. name: 'user_price',
  52. orderable: false,
  53. searchable: false,
  54. render: function (data, type, row) {
  55. return '<input type="number" min="0" step="0.01" name="user_price[' + row.id + ']" value="' + data + '';
  56. }
  57. },
  58. {data: 'part_search', name: 'part_search', visible: false, searchable: true}
  59. ],
  60. lengthMenu: [100, 200, 500, 1000], // Add this option to show 200, 500, 1000 and All
  61. pageLength: 100, // Set default page length to 200
  62. responsive: true, // Enable responsive design
  63. });
  64. });
  65. </script>
  66. @endsection

web_confirm.blade

  1. @extends('frontend.layouts.app')
  2. @section('content')
  3. <div class="container">
  4. <h1>Selected Items</h1>
  5. <table class="table table-bordered">
  6. <thead>
  7. <tr>
  8. <th>Part No</th>
  9. <th>Description</th>
  10. <th>Supp Part</th>
  11. <th>Customer Price</th>
  12. <th>Quantity</th>
  13. <th>Total Price</th>
  14. </tr>
  15. </thead>
  16. <tbody>
  17. @foreach ($selectedItems as $selectedItem)
  18. <tr>
  19. <td>{{ $selectedItem['item']->part_no }}</td>
  20. <td>{{ $selectedItem['item']->description }}</td>
  21. <td>{{ $selectedItem['item']->supp_part }}</td>
  22. <td>{{ $selectedItem['userPrice'] > 0 ? number_format((float) $selectedItem['userPrice'], 2, '.', '') : 'N/A' }}</td>
  23. <td>{{ $selectedItem['quantity'] }}</td>
  24. <td>{{ number_format((float) $selectedItem['itemTotal'], 2, '.', '') }}</td>
  25. </tr>
  26. @endforeach
  27. </tbody>
  28. </table>
  29. <h2>Total Price: RM {{ $total }}</h2>
  30. <button type="submit" class="btn btn-primary">Confirm Order</button>
  31. </div>
  32. @endsection

这是名为ItemController.php的控制器

  1. <?php
  2. namespace App\Http\Controllers\Frontend\webitem;
  3. use Illuminate\Http\Request;
  4. use App\Http\Controllers\Controller;
  5. use App\Models\Access\Webitems;
  6. use App\Models\Access\WebTest;
  7. use Yajra\Datatables\Facades\Datatables;
  8. use App\Models\Order;
  9. class ItemController extends Controller
  10. {
  11. public function index()
  12. {
  13. return view('frontend.webitem.web_item');
  14. }
  15. public function webitemtable(Request $request)
  16. {
  17. $search = $request->input('search.value'); // 从DataTables获取搜索输入
  18. $query = Webitems::query();
  19. // 根据'part_no'和'description'列应用搜索过滤器
  20. if ($search) {
  21. $query->where('part_no', 'LIKE', "%$search%")
  22. ->orWhere('description', 'LIKE', "%$search%");
  23. }
  24. // 获取过滤后的项目
  25. $items = $query->get();
  26. return Datatables::of($items)
  27. ->addColumn('checkbox', function ($item) {
  28. return '<input type="checkbox" data-id="' . $item->id . '" name="selected[]" value="' . $item->id . '" data-group="checkbox-group">';
  29. })
  30. ->addColumn('action', function ($item) {
  31. return '<input type="number" min="0" name="quantity[' . $item->id . ']" value="0">';
  32. })
  33. ->addColumn('user_price', function ($item) {
  34. return '<input type="number" min="0" step="0.01" name="user_price[' . $item->id . ']" value="0">';
  35. })
  36. ->rawColumns(['checkbox', 'action', 'user_price'])
  37. ->make(true);
  38. }
  39. public function confirm(Request $request)
  40. {
  41. $selectedItems = [];
  42. $total = 0;
  43. $items = Webitems::all();
  44. foreach ($items as $item) {
  45. $quantity = $request->input('quantity.' . $item->id, 0);
  46. $userPrice = $request->input('user_price.' . $item->id, 0);
  47. if ($quantity > 0) {
  48. $selectedItems[] = [
  49. 'item' => $item,
  50. 'quantity' => $quantity,
  51. 'userPrice' => $userPrice,
  52. 'itemTotal' => $userPrice * $quantity,
  53. ];
  54. $total += $userPrice * $quantity;
  55. }
  56. }
  57. $total = number_format((float) $total, 2, '.', ''); // 将总价格式化为2位小数
  58. return view('frontend.webitem.web_confirm')->with([
  59. 'selectedItems' => $selectedItems,
  60. 'total' => $total,
  61. 'request' => $request,
  62. ]);
  63. }
  64. }

这是路由

  1. Route::group(['prefix' => 'webitem', 'namespace' => 'webitem', 'middleware' => 'auth'], function () {
  2. Route::get('index', 'ItemController@index')->name('webitem.index');
  3. Route::get('webitemtable', 'ItemController@webitemtable')->name('webitem.webitemtable');
  4. Route::get('index_test', 'ItemController@index_test')->name('webitem.index_test');
  5. Route::get('getDataTable', 'ItemController@getDataTable')->name('webitem.getDataTable');
  6. Route::post('confirm', 'ItemController@confirm')->name('webitem.confirm');
  7. Route::post('/order/confirm', 'ItemController@confirmOrder')->name('order.confirm');
  8. Route::post('/order/confirm', 'OrderController@confirmOrder')->name('order.confirm');
  9. Route::post('/submit-order', 'OrderController@submitOrder')->name('submit.order');
  10. 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.

after doing the searching

this is what happen when i use the search bar.

result after doing the search

as you can see, it doent count the previous 2 item.

i will provide the code.

web_item.blade

  1. @extends(&#39;frontend.layouts.app&#39;)
  2. @section(&#39;content&#39;)
  3. &lt;link rel=&quot;stylesheet&quot; href=&quot;{{ asset(&#39;css/tablestyle.css&#39;) }}&quot; /&gt;
  4. &lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.datatables.net/1.11.3/css/jquery.dataTables.min.css&quot;&gt;
  5. {!! Form::open([&#39;route&#39; =&gt; &#39;frontend.webitem.confirm&#39;, &#39;method&#39; =&gt; &#39;POST&#39;]) !!}
  6. {!! Form::submit(&#39;Confirm&#39;, [&#39;class&#39; =&gt; &#39;btn btn-primary&#39;, &#39;style&#39; =&gt; &#39;float:right;&#39;]) !!}
  7. &lt;br&gt;
  8. &lt;table id=&quot;webitem-table&quot; class=&quot;table table-bordered&quot;&gt;
  9. &lt;thead&gt;
  10. &lt;tr&gt;
  11. &lt;th&gt;Part No&lt;/th&gt;
  12. &lt;th&gt;Description&lt;/th&gt;
  13. &lt;th&gt;Supp Part&lt;/th&gt;
  14. &lt;th&gt;RK Qty&lt;/th&gt;
  15. &lt;th&gt;Dealer Price&lt;/th&gt;
  16. &lt;th&gt;Workshop&lt;/th&gt;
  17. &lt;th&gt;End User Price&lt;/th&gt;
  18. &lt;th&gt;Quantity&lt;/th&gt;
  19. &lt;th&gt;Customer Price&lt;/th&gt;
  20. &lt;th&gt;Part Search&lt;/th&gt;
  21. &lt;/tr&gt;
  22. &lt;/thead&gt;
  23. &lt;tbody&gt;
  24. &lt;/tbody&gt;
  25. &lt;/table&gt;
  26. {!! Form::close() !!}
  27. &lt;script&gt;
  28. $(document).ready(function () {
  29. $(&#39;#webitem-table&#39;).DataTable({
  30. processing: true,
  31. ajax: &#39;{!! route(&#39;frontend.webitem.webitemtable&#39;) !!}&#39;,
  32. columns: [
  33. {data: &#39;part_no&#39;, name: &#39;part_no&#39;, searchable: true},
  34. {data: &#39;description&#39;, name: &#39;description&#39;, searchable: true},
  35. {data: &#39;supp_part&#39;, name: &#39;supp_part&#39;},
  36. {data: &#39;rk_qty&#39;, name: &#39;rk_qty&#39;},
  37. {data: &#39;dealer_price&#39;, name: &#39;dealer_price&#39;},
  38. {data: &#39;workshop&#39;, name: &#39;workshop&#39;},
  39. {data: &#39;enduser_price&#39;, name: &#39;enduser_price&#39;},
  40. {
  41. data: &#39;quantity&#39;,
  42. name: &#39;quantity&#39;,
  43. orderable: false,
  44. searchable: false,
  45. render: function (data, type, row) {
  46. return &#39;&lt;input type=&quot;number&quot; min=&quot;0&quot; name=&quot;quantity[&#39; + row.id + &#39;]&quot; value=&quot;0&quot;&gt;&#39;;
  47. }
  48. },
  49. {
  50. data: &#39;user_price&#39;,
  51. name: &#39;user_price&#39;,
  52. orderable: false,
  53. searchable: false,
  54. render: function (data, type, row) {
  55. return &#39;&lt;input type=&quot;number&quot; min=&quot;0&quot; step=&quot;0.01&quot; name=&quot;user_price[&#39; + row.id + &#39;]&quot; value=&quot;&#39; + data + &#39;&#39;;
  56. }
  57. },
  58. {data: &#39;part_search&#39;, name: &#39;part_search&#39;, visible: false, searchable: true}
  59. ],
  60. lengthMenu: [100, 200, 500, 1000], // Add this option to show 200, 500, 1000 and All
  61. pageLength: 100, // Set default page length to 200
  62. responsive: true, // Enable responsive design
  63. });
  64. });
  65. &lt;/script&gt;
  66. @endsection

web_confirm.blade

  1. @extends(&#39;frontend.layouts.app&#39;)
  2. @section(&#39;content&#39;)
  3. &lt;div class=&quot;container&quot;&gt;
  4. &lt;h1&gt;Selected Items&lt;/h1&gt;
  5. &lt;table class=&quot;table table-bordered&quot;&gt;
  6. &lt;thead&gt;
  7. &lt;tr&gt;
  8. &lt;th&gt;Part No&lt;/th&gt;
  9. &lt;th&gt;Description&lt;/th&gt;
  10. &lt;th&gt;Supp Part&lt;/th&gt;
  11. &lt;th&gt;Customer Price&lt;/th&gt;
  12. &lt;th&gt;Quantity&lt;/th&gt;
  13. &lt;th&gt;Total Price&lt;/th&gt;
  14. &lt;/tr&gt;
  15. &lt;/thead&gt;
  16. &lt;tbody&gt;
  17. @foreach ($selectedItems as $selectedItem)
  18. &lt;tr&gt;
  19. &lt;td&gt;{{ $selectedItem[&#39;item&#39;]-&gt;part_no }}&lt;/td&gt;
  20. &lt;td&gt;{{ $selectedItem[&#39;item&#39;]-&gt;description }}&lt;/td&gt;
  21. &lt;td&gt;{{ $selectedItem[&#39;item&#39;]-&gt;supp_part }}&lt;/td&gt;
  22. &lt;td&gt;{{ $selectedItem[&#39;userPrice&#39;] &gt; 0 ? number_format((float) $selectedItem[&#39;userPrice&#39;], 2, &#39;.&#39;, &#39;&#39;) : &#39;N/A&#39; }}&lt;/td&gt;
  23. &lt;td&gt;{{ $selectedItem[&#39;quantity&#39;] }}&lt;/td&gt;
  24. &lt;td&gt;{{ number_format((float) $selectedItem[&#39;itemTotal&#39;], 2, &#39;.&#39;, &#39;&#39;) }}&lt;/td&gt;
  25. &lt;/tr&gt;
  26. @endforeach
  27. &lt;/tbody&gt;
  28. &lt;/table&gt;
  29. &lt;h2&gt;Total Price: RM {{ $total }}&lt;/h2&gt;
  30. &lt;button type=&quot;submit&quot; class=&quot;btn btn-primary&quot;&gt;Confirm Order&lt;/button&gt;
  31. &lt;/div&gt;
  32. @endsection

the is the controller named itemController.php

  1. &lt;?php
  2. namespace App\Http\Controllers\Frontend\webitem;
  3. use Illuminate\Http\Request;
  4. use App\Http\Controllers\Controller;
  5. use App\Models\Access\Webitems;
  6. use App\Models\Access\WebTest;
  7. use Yajra\Datatables\Facades\Datatables;
  8. use App\Models\Order;
  9. class ItemController extends Controller
  10. {
  11. public function index()
  12. {
  13. return view(&#39;frontend.webitem.web_item&#39;);
  14. }
  15. public function webitemtable(Request $request)
  16. {
  17. $search = $request-&gt;input(&#39;search.value&#39;); // Get the search input from DataTables
  18. $query = Webitems::query();
  19. // Apply search filters based on &#39;part_no&#39; and &#39;description&#39; columns
  20. if ($search) {
  21. $query-&gt;where(&#39;part_no&#39;, &#39;LIKE&#39;, &quot;%$search%&quot;)
  22. -&gt;orWhere(&#39;description&#39;, &#39;LIKE&#39;, &quot;%$search%&quot;);
  23. }
  24. // Fetch filtered items
  25. $items = $query-&gt;get();
  26. return Datatables::of($items)
  27. -&gt;addColumn(&#39;checkbox&#39;, function ($item) {
  28. return &#39;&lt;input type=&quot;checkbox&quot; data-id=&quot;&#39; . $item-&gt;id . &#39;&quot; name=&quot;selected[]&quot; value=&quot;&#39; . $item-&gt;id . &#39;&quot; data-group=&quot;checkbox-group&quot;&gt;&#39;;
  29. })
  30. -&gt;addColumn(&#39;action&#39;, function ($item) {
  31. return &#39;&lt;input type=&quot;number&quot; min=&quot;0&quot; name=&quot;quantity[&#39; . $item-&gt;id . &#39;]&quot; value=&quot;0&quot;&gt;&#39;;
  32. })
  33. -&gt;addColumn(&#39;user_price&#39;, function ($item) {
  34. return &#39;&lt;input type=&quot;number&quot; min=&quot;0&quot; step=&quot;0.01&quot; name=&quot;user_price[&#39; . $item-&gt;id . &#39;]&quot; value=&quot;0&quot;&gt;&#39;;
  35. })
  36. -&gt;rawColumns([&#39;checkbox&#39;, &#39;action&#39;, &#39;user_price&#39;])
  37. -&gt;make(true);
  38. }
  39. public function confirm(Request $request)
  40. {
  41. $selectedItems = [];
  42. $total = 0;
  43. $items = Webitems::all();
  44. foreach ($items as $item) {
  45. $quantity = $request-&gt;input(&#39;quantity.&#39; . $item-&gt;id, 0);
  46. $userPrice = $request-&gt;input(&#39;user_price.&#39; . $item-&gt;id, 0);
  47. if ($quantity &gt; 0) {
  48. $selectedItems[] = [
  49. &#39;item&#39; =&gt; $item,
  50. &#39;quantity&#39; =&gt; $quantity,
  51. &#39;userPrice&#39; =&gt; $userPrice,
  52. &#39;itemTotal&#39; =&gt; $userPrice * $quantity,
  53. ];
  54. $total += $userPrice * $quantity;
  55. }
  56. }
  57. $total = number_format((float) $total, 2, &#39;.&#39;, &#39;&#39;); // format the total to 2 decimal places
  58. return view(&#39;frontend.webitem.web_confirm&#39;)-&gt;with([
  59. &#39;selectedItems&#39; =&gt; $selectedItems,
  60. &#39;total&#39; =&gt; $total,
  61. &#39;request&#39; =&gt; $request,
  62. ]);
  63. }
  64. }

this is the route

  1. Route::group([&#39;prefix&#39; =&gt; &#39;webitem&#39;, &#39;namespace&#39; =&gt; &#39;webitem&#39;, &#39;middleware&#39; =&gt; &#39;auth&#39;], function () {
  2. Route::get(&#39;index&#39;, &#39;ItemController@index&#39;)-&gt;name(&#39;webitem.index&#39;);
  3. Route::get(&#39;webitemtable&#39;, &#39;ItemController@webitemtable&#39;)-&gt;name(&#39;webitem.webitemtable&#39;);
  4. Route::get(&#39;index_test&#39;, &#39;ItemController@index_test&#39;)-&gt;name(&#39;webitem.index_test&#39;);
  5. Route::get(&#39;getDataTable&#39;, &#39;ItemController@getDataTable&#39;)-&gt;name(&#39;webitem.getDataTable&#39;);
  6. Route::post(&#39;confirm&#39;, &#39;ItemController@confirm&#39;)-&gt;name(&#39;webitem.confirm&#39;);
  7. Route::post(&#39;/order/confirm&#39;, &#39;ItemController@confirmOrder&#39;)-&gt;name(&#39;order.confirm&#39;);
  8. Route::post(&#39;/order/confirm&#39;, &#39;OrderController@confirmOrder&#39;)-&gt;name(&#39;order.confirm&#39;);
  9. Route::post(&#39;/submit-order&#39;, &#39;OrderController@submitOrder&#39;)-&gt;name(&#39;submit.order&#39;);
  10. Route::post(&#39;/frontend/webitem/process&#39;, &#39;Frontend\webitem\ItemController@confirm&#39;)-&gt;name(&#39;frontend.webitem.process&#39;);

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.

huangapple
  • 本文由 发表于 2023年8月9日 16:18:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76865831.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定