gpt4 book ai didi

javascript - 使用 ajax 在同一页面中显示特定类别的产品 - ajax、laravel

转载 作者:行者123 更新时间:2023-12-03 02:30:06 26 4
gpt4 key购买 nike

我的任务遇到一些问题。我不太擅长 JavaScript。请帮忙。

我将类别显示为按钮,并且我希望在按下按钮时显示分配给每个类别的产品,并且产品也以按钮的形式显示。在下一个任务中,我必须确保按下产品按钮时,该产品会添加到列表中,而且无需刷新页面。

Controller :

public function categories($id)
{
$table = Table::find($id);
$categories = Category::all();
return view('products', compact('table', 'categories'));
}
public function products($id)
{
$products = Products::find($id);
$response = '<button class="btn btn-dark product-button">{{$product->name}}</button>';
return response()->json([
'response' => $response
]);
}

路线:

Route::get('/masa/{id}','HomeController@categories')->name('table');
Route::get('/produse', 'HomeController@produse')->name('products');

Blade

@extends('layouts.app')
@section('content')
<div class="container-fluid">
<div class="row title">
<h2 style="text-align: center; margin: auto;">{{$table->name}}</h2>
</div>
<div class="row">
<div class="col-sm-4 col-xs-4 col-md-4 col-lg-4 product-table">
<div class="row">
<button class="btn btn-dark product-button">
{{$table->name}}
</button>
</div>
</div>
<div class="col-sm-8 col-xs-8 col-md-8 col-lg-8 product-table">
<div class="row">
{{--Categorii--}}
@foreach($categories as $category)
<button class="btn btn-dark product-button" id="cat-{{$category->id}}">{{$category->name}}</button>
@endforeach
</div>
<hr><br>
<div class="row" id="products">
<button class="btn btn-dark product-button">Product Button</button>
</div>
</div>
</div>
</div>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/js/bootstrap.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/twbs-pagination/1.3.1/jquery.twbsPagination.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/1000hz-bootstrap-validator/0.11.5/validator.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/js/toastr.min.js"></script>
<link href="//cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/css/toastr.min.css" rel="stylesheet">
{{--<script src="{{asset('/js/script.js')}}"></script>--}}
<script type="text/javascript">
$('select[cat-{{$category->id}}]').on('click', function () {
$.ajax(
url: "{{route('products')}}",
type: 'post',
data: {
'category': '{{$category->name}}',
"_token": "{{ csrf_token() }}"
},
success: function (data) {
$('.product-table').after(data.response);
}
});
});
</script>
@endsection

最佳答案

您的数据库结构应该是,

迁移

类别

    /**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('categories');
}

产品

/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->integer('category_id')->unsigned();
$table->string('name');
$table->timestamps();

$table->foreign('category_id')->references('id')->on('categories');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('products');
}

类别模型

class Category extends Model
{
//
public function products()
{
return $this->hasMany('App\Product');
}
}

产品型号

class Product extends Model
{
//
public function category()
{
return $this->belongsTo('App\Category');
}
}

现在,您应该拥有如下所示的 Controller 文件:

DropDownController

class DropDownController extends Controller
{
//

public function index(){

$categories = Category::all();
$products = Product::all();

return view('dropdown.dropdown', compact('categories','products'));
}

public function getProducts($id){
if($id!=0){
$products = Category::find($id)->products()->select('id', 'name')->get()->toArray();
}else{
$products = Product::all()->toArray();
}
return response()->json($products);
}
}

你的路线应该是这样的,

路线

Route::get('/dropdown','DropDownController@index');
Route::get('category/{id}/products', 'DropDownController@getProducts');

此后,在您想要显示类别的关联产品的编辑表单中,进行如下操作。

<div class="col-md-12">
<div class="col-md-6">
<label>Category</label>
<select class="form-control" name = "category">
<option value="0">Please Select Category</option>
@foreach($categories as $category)
<option value="{{$category->id}}">{{$category->name}}</option>
@endforeach
</select>
</div>
<div class="col-md-6">
<label>Products</label>
<select class="form-control" name = "product">
<option value="0">Please Select Product</option>
@foreach($products as $product)
<option value="{{$product->id}}">{{$product->name}}</option>
@endforeach

</select>
</div>
</div>

你的ajax脚本就像,

AJAX

<script>
jQuery(document).ready(function ($) {
$('select[name=category]').on('change', function () {
var selected = $(this).find(":selected").attr('value');
$.ajax({
url: base_url + '/category/'+selected+'/products/',
type: 'GET',
dataType: 'json',

}).done(function (data) {

var select = $('select[name=product]');
select.empty();
select.append('<option value="0" >Please Select Product</option>');
$.each(data,function(key, value) {
select.append('<option value=' + key.id + '>' + value.name + '</option>');
});
console.log("success");
})
});
});
</script>

这是相当大的,但很容易理解它是如何工作的。希望您能理解。

关于javascript - 使用 ajax 在同一页面中显示特定类别的产品 - ajax、laravel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48766800/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com