我想创建一个包含少数城市的区域。所以我决定使用jQuery Select2
这是我的创建形式多重选择
<div class="form-group {{ $errors->has('cities') ? 'has-error' : '' }}">
<label>Tentukan Kota</label>
<select name="cities[]" class="city form-control" data-placeholder="Pilih Kota" style="width: 100%;" multiple="multiple">
@foreach($cities as $city)
<option value="{{ $city->id }}">{{ $city->name }}</option>
@endforeach
</select>
</div>
我可以像文档一样进行多重选择。
这是我的控制器处理显示创建表单
public function zone_create()
{
$cities = City::where('zone_id', null)->get();
return view('backend.admin.pricings.zone_create', compact('cities'));
}
关系是一个区域有很多城市。
class Zone extends Model
{
protected $fillable = [
'name',
];
public function cities(){
return $this->hasMany(City::class);
}
}
该城市属于区域
class City extends Model
{
protected $fillable = [
'zone_id',
'name',
];
public function zone(){
return $this->belongsTo(Zone::class);
}
}
这是我的编辑方法
public function edit($id)
{
$zone = Zone::find($id);
$cities = City::all();
return view('backend.admin.pricings.zone_edit', compact('zone', 'cities'));
}
到目前为止,这是我的编辑表格
<div class="form-group {{ $errors->has('cities') ? 'has-error' : '' }}">
<label>Tentukan Kota</label>
<select name="cities[]" class="city form-control" data-placeholder="Pilih Kota" style="width: 100%;" multiple="multiple">
//load option from cities table
//set selected the city belongs to zone
//the other city which don't belong to zone still here for option
</select>
</div>
但是我如何填充我的编辑表单(多选)与城市属于区域?
答案 0 :(得分:2)
在我看来就像这样
<select name="cities[]" class="city form-control" data-placeholder="Pilih Kota" style="width: 100%;" multiple="multiple">
@foreach($cities as $city)
@if(in_array($city->id, $zoneCityIds))
<option value="{{ $city->id }}" selected="true">{{ $city->name }}</option>
@else
<option value="{{ $city->id }}">{{ $city->name }}</option>
@endif
@endforeach
</select>
在我的控制器中像这样
public function zone_edit($id)
{
$zoneCityIds = [];
$zone = Zone::find($id);
$cities = City::all();
foreach($zone->cities as $zoneCity)
{
$zoneCityIds[] = $zoneCity->id;
}
return view('backend.admin.pricings.zone_edit', compact('zone', 'cities', 'zoneCityIds'));
}
实际上它是关于选项标签selected="true"
答案 1 :(得分:0)
在您的编辑功能中,您可以获取该特定区域的所有城市
$zone_cities = implode(',',$zone->cities->lists('name')->toArray());
并在您的视图中传递$zone_cities
。
return view('backend.admin.pricings.zone_edit', compact('zone', 'cities','zone_cities'));
您必须在视图中使用表单模型绑定
阅读laravel集体表格模型绑定[此处]。QByteArray
您将以这样的方式打开表单
您的编辑表单将是这样的
{!! Form::label('cities','Cities: ') !!}
{!! Form::text('cities',$zone_cities,['class'=>'input','id'=>'cities','aria-required'=>'true','data-seperator'=>',']) !!}
答案 2 :(得分:0)
对于多个值,只需使用
Object.prototype.fullMatch = function(obj){
if (typeof this !== typeof obj) return false;
if (this == null && obj != null || this != null && obj == null) return false;
var this_keys = [];
var obj_keys = [];
for (var key in this) if (this.hasOwnProperty(key)) this_keys.push(key);
for (var key in obj) if (obj.hasOwnProperty(key)) obj_keys.push(key);
if (this_keys.length !== obj_keys.length){
this_keys = null;
obj_keys = null;
return false;
}
var full_match = true;
for (var key in this){
if (this.hasOwnProperty(key) && obj.hasOwnProperty(key)){
var this_value = this[key];
var obj_value = obj[key];
if (typeof this_value !== typeof obj_value || ("object" === typeof this_value && !this_value.fullMatch(obj_value)) || "object" !== typeof this_value && this_value !== obj_value){
full_match = false;
break;
}
}
}
return full_match;
};