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

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

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

@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.

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

@extends(&#39;frontend.layouts.app&#39;)
@section(&#39;content&#39;)
&lt;link rel=&quot;stylesheet&quot; href=&quot;{{ asset(&#39;css/tablestyle.css&#39;) }}&quot; /&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.datatables.net/1.11.3/css/jquery.dataTables.min.css&quot;&gt;
{!! Form::open([&#39;route&#39; =&gt; &#39;frontend.webitem.confirm&#39;, &#39;method&#39; =&gt; &#39;POST&#39;]) !!}
{!! Form::submit(&#39;Confirm&#39;, [&#39;class&#39; =&gt; &#39;btn btn-primary&#39;, &#39;style&#39; =&gt; &#39;float:right;&#39;]) !!}
&lt;br&gt;
&lt;table id=&quot;webitem-table&quot; class=&quot;table table-bordered&quot;&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Part No&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;th&gt;Supp Part&lt;/th&gt;
&lt;th&gt;RK Qty&lt;/th&gt;
&lt;th&gt;Dealer Price&lt;/th&gt;
&lt;th&gt;Workshop&lt;/th&gt;
&lt;th&gt;End User Price&lt;/th&gt;
&lt;th&gt;Quantity&lt;/th&gt;
&lt;th&gt;Customer Price&lt;/th&gt;
&lt;th&gt;Part Search&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;/tbody&gt;
&lt;/table&gt;
{!! Form::close() !!}
&lt;script&gt;
$(document).ready(function () {
$(&#39;#webitem-table&#39;).DataTable({
processing: true,
ajax: &#39;{!! route(&#39;frontend.webitem.webitemtable&#39;) !!}&#39;,
columns: [
{data: &#39;part_no&#39;, name: &#39;part_no&#39;, searchable: true},
{data: &#39;description&#39;, name: &#39;description&#39;, searchable: true},
{data: &#39;supp_part&#39;, name: &#39;supp_part&#39;},
{data: &#39;rk_qty&#39;, name: &#39;rk_qty&#39;},
{data: &#39;dealer_price&#39;, name: &#39;dealer_price&#39;},
{data: &#39;workshop&#39;, name: &#39;workshop&#39;},
{data: &#39;enduser_price&#39;, name: &#39;enduser_price&#39;},
{
data: &#39;quantity&#39;, 
name: &#39;quantity&#39;, 
orderable: false, 
searchable: false, 
render: function (data, type, row) {
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;;
}
},
{
data: &#39;user_price&#39;,
name: &#39;user_price&#39;,
orderable: false,
searchable: false,
render: function (data, type, row) {
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;;
}
},
{data: &#39;part_search&#39;, name: &#39;part_search&#39;, 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
});
});
&lt;/script&gt;
@endsection

web_confirm.blade

@extends(&#39;frontend.layouts.app&#39;)
@section(&#39;content&#39;)
&lt;div class=&quot;container&quot;&gt;
&lt;h1&gt;Selected Items&lt;/h1&gt;
&lt;table class=&quot;table table-bordered&quot;&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Part No&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;th&gt;Supp Part&lt;/th&gt;
&lt;th&gt;Customer Price&lt;/th&gt;
&lt;th&gt;Quantity&lt;/th&gt;
&lt;th&gt;Total Price&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
@foreach ($selectedItems as $selectedItem)
&lt;tr&gt;
&lt;td&gt;{{ $selectedItem[&#39;item&#39;]-&gt;part_no }}&lt;/td&gt;
&lt;td&gt;{{ $selectedItem[&#39;item&#39;]-&gt;description }}&lt;/td&gt;
&lt;td&gt;{{ $selectedItem[&#39;item&#39;]-&gt;supp_part }}&lt;/td&gt;
&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;
&lt;td&gt;{{ $selectedItem[&#39;quantity&#39;] }}&lt;/td&gt;
&lt;td&gt;{{ number_format((float) $selectedItem[&#39;itemTotal&#39;], 2, &#39;.&#39;, &#39;&#39;) }}&lt;/td&gt;
&lt;/tr&gt;
@endforeach
&lt;/tbody&gt;
&lt;/table&gt;
&lt;h2&gt;Total Price: RM {{ $total }}&lt;/h2&gt;
&lt;button type=&quot;submit&quot; class=&quot;btn btn-primary&quot;&gt;Confirm Order&lt;/button&gt;
&lt;/div&gt;
@endsection

the is the controller named itemController.php

&lt;?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(&#39;frontend.webitem.web_item&#39;);
}
public function webitemtable(Request $request)
{
$search = $request-&gt;input(&#39;search.value&#39;); // Get the search input from DataTables
$query = Webitems::query();
// Apply search filters based on &#39;part_no&#39; and &#39;description&#39; columns
if ($search) {
$query-&gt;where(&#39;part_no&#39;, &#39;LIKE&#39;, &quot;%$search%&quot;)
-&gt;orWhere(&#39;description&#39;, &#39;LIKE&#39;, &quot;%$search%&quot;);
}
// Fetch filtered items
$items = $query-&gt;get();
return Datatables::of($items)
-&gt;addColumn(&#39;checkbox&#39;, function ($item) {
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;;
})
-&gt;addColumn(&#39;action&#39;, function ($item) {
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;;
})
-&gt;addColumn(&#39;user_price&#39;, function ($item) {
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;;
})
-&gt;rawColumns([&#39;checkbox&#39;, &#39;action&#39;, &#39;user_price&#39;])
-&gt;make(true);
}
public function confirm(Request $request)
{
$selectedItems = [];
$total = 0;
$items = Webitems::all();
foreach ($items as $item) {
$quantity = $request-&gt;input(&#39;quantity.&#39; . $item-&gt;id, 0);
$userPrice = $request-&gt;input(&#39;user_price.&#39; . $item-&gt;id, 0);
if ($quantity &gt; 0) {
$selectedItems[] = [
&#39;item&#39; =&gt; $item,
&#39;quantity&#39; =&gt; $quantity,
&#39;userPrice&#39; =&gt; $userPrice,
&#39;itemTotal&#39; =&gt; $userPrice * $quantity,
];
$total += $userPrice * $quantity;
}
}
$total = number_format((float) $total, 2, &#39;.&#39;, &#39;&#39;); // format the total to 2 decimal places
return view(&#39;frontend.webitem.web_confirm&#39;)-&gt;with([
&#39;selectedItems&#39; =&gt; $selectedItems,
&#39;total&#39; =&gt; $total,
&#39;request&#39; =&gt; $request,
]);
}
}

this is the route

      Route::group([&#39;prefix&#39; =&gt; &#39;webitem&#39;, &#39;namespace&#39; =&gt; &#39;webitem&#39;, &#39;middleware&#39; =&gt; &#39;auth&#39;], function () {
Route::get(&#39;index&#39;, &#39;ItemController@index&#39;)-&gt;name(&#39;webitem.index&#39;);
Route::get(&#39;webitemtable&#39;, &#39;ItemController@webitemtable&#39;)-&gt;name(&#39;webitem.webitemtable&#39;);
Route::get(&#39;index_test&#39;, &#39;ItemController@index_test&#39;)-&gt;name(&#39;webitem.index_test&#39;);
Route::get(&#39;getDataTable&#39;, &#39;ItemController@getDataTable&#39;)-&gt;name(&#39;webitem.getDataTable&#39;);
Route::post(&#39;confirm&#39;, &#39;ItemController@confirm&#39;)-&gt;name(&#39;webitem.confirm&#39;);
Route::post(&#39;/order/confirm&#39;, &#39;ItemController@confirmOrder&#39;)-&gt;name(&#39;order.confirm&#39;);
Route::post(&#39;/order/confirm&#39;, &#39;OrderController@confirmOrder&#39;)-&gt;name(&#39;order.confirm&#39;);
Route::post(&#39;/submit-order&#39;, &#39;OrderController@submitOrder&#39;)-&gt;name(&#39;submit.order&#39;);
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:

确定