gpt4 book ai didi

javascript - Ajax 响应 Laravel 到对象

转载 作者:行者123 更新时间:2023-12-03 04:15:55 25 4
gpt4 key购买 nike

我创建了一个ajax请求来显示我的表 Eloquent 查询的结果,该查询依赖于选择框“poule”。

一切正常,但是当我通过从选择框中选择 poule_id 来运行 ajax 请求时,我需要显示 json 结果。我想将结果显示为表中的 foreach 循环($equipes as $equipe),因为如您所见,我显示了相关模型的值。

更新:

我的模型装备:

class Equipe extends Model
{

public function structure()
{
return $this->belongsTo('App\Structure');
}

我的模型 CompetEquipe(我用它来显示我的 foreach)

class CompetEquipe extends Model
{
public function equipe(){

return $this->belongsTo('App\Equipe' , 'equipe_id');

}

Like this i can access to the tables in relations in my foreach

<tr>
<td>
<a href="{!! route('club.show', $equipe->equipe->structure->id) !!}">{{$equipe->equipe->structure->nom_structure}}</a>
</td>
<td>
<a href="{!! route('equipe.show', $equipe->equipe->id) !!}">{{$equipe->equipe->lb_equipe}}</a>
</td>
<td>{!! Form::text('nb_bonus') !!}</td>
</tr>

实际上,通过这种方式,我只能显示equipe_id,但我想显示对象以访问相关的其他模型,并将结果显示为表中的foreach,如下所示:

@foreach($equipes as $equipe)
<tr>
<td>
<a href="{!! route('club.show', $equipe->equipe->structure->id) !!}">{{$equipe->equipe->structure->nom_structure}}</a>
</td>
<td>
<a href="{!! route('equipe.show', $equipe->equipe->id) !!}">{{$equipe->equipe->lb_equipe}}</a>
</td>
<td>{!! Form::text('nb_bonus') !!}</td>
</tr>
@endforeach

希望有人明白我想做什么。提前非常感谢 friend 们

here my JSON RESULT : {"equipes":[{"equipe_id":1,"poule_id":1}]}

我选择的过滤器搜索:

<select id="poule">
@foreach($select_poules as $select_poule)
<option value="{{$select_poule->id}}">{{$select_poule->lb_poule}}</option>
@endforeach
</select>

我的 table :

<table id="equipes" class="table table-striped">
<thead>
<tr>
<th>Club</th>
<th>Nom de l'équipe</th>
<th>Bonus(+/-)</th>
</tr>
</thead>
<tbody>
@foreach($equipes as $equipe)
<tr>
<td>
<a href="{!! route('club.show', $equipe->equipe->structure->id) !!}">{{$equipe->equipe->structure->nom_structure}}</a>
</td>
<td>
<a href="{!! route('equipe.show', $equipe->equipe->id) !!}">{{$equipe->equipe->lb_equipe}}</a>
</td>
<td>{!! Form::text('nb_bonus') !!}</td>
</tr>
@endforeach
</tbody>
</table>

我的 Controller :

public function searchEquipes(Request $request)
{
$equipes = [];

if($request->has('poule_id')){
$equipes = EquipePoule::where('poule_id',$request->poule_id)
->get();
}


return response()->json(['equipes' => $equipes]);
}

我的脚本:

<script>
$(document).on('change', '#poule', function() {

$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});

$.ajax({
type: 'GET',
dataType: "json",
url : '/licences/public/search/equipes',
data : {
poule_id : $('#poule').val()
},
success:function(data){

$('#equipes').empty();
for (var i = 0; i < data.equipes.length; i++) {
$('#equipes').append('<tr><td>'+data.equipes[i].equipe_id+'</td></‌​tr>')
}
},
timeout:10000
});
});
</script>

最佳答案

要将表的内容替换为 AJAX 请求的响应,您需要 jQuery 的 replaceWith 。您需要稍微更改 jQuery 成功函数。

success: function(data) {
//Build the row data as you wish to display it
var rowData = ""
$.each(team["equipes"][0], function(i, value) {
var rowData += $("#equipes").append("<td>"+value+"</td>");
})
$("#equipes").replaceWith("<tr>"+rowData+"</tr>

$("#equipes").append("</tr>");
}

这会将您的初始表格数据替换为您选择的数据。

关于javascript - Ajax 响应 Laravel 到对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44134661/

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