Laravel-admin select 下拉框 options 请求 API 如何传递参数?
前端样式
我需要根据时间段去动态获取select下拉框的值展示。
后台代码:
$form->select('ad_id', '位置')
->options(route('apiLists'),['start_time' => $start_time, 'end_time' => $end_time])
->rules('required');
路由
Route::get('/lists', 'ApiController@lists')->name('apiLists');
我这样传递参数给api,参数接收不到。下面我把api接收参数发出来
use App\Models\Ad;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Request;
class ApiController extends Controller
{
//
/**
* @return array
*/
public function lists()
{
$start_time = Request::get('start_time', 0);
$end_time = Request::get('end_time', 0);
}
}
我看了一下options方法源码
/**
* Load options from remote.
*
* @param string $url
* @param array $parameters
* @param array $options
*
* @return $this
*/
protected function loadRemoteOptions($url, $parameters = [], $options = [])
{
$ajaxOptions = [
'url' => $url.'?'.http_build_query($parameters),
];
$configs = array_merge([
'allowClear' => true,
'placeholder' => [
'id' => '',
'text' => trans('admin.choose'),
],
], $this->config);
$configs = json_encode($configs);
$configs = substr($configs, 1, strlen($configs) - 2);
$ajaxOptions = json_encode(array_merge($ajaxOptions, $options));
$this->script = <<<EOT
$.ajax($ajaxOptions).done(function(data) {
$("{$this->getElementClassSelector()}").each(function(index, element) {
$(element).select2({
data: data,
$configs
});
var value = $(element).data('value') + '';
if (value) {
value = value.split(',');
$(element).select2('val', value);
}
});
});
EOT;
dd($this->script);
return $this;
}
打印了一下源码:
"""
$.ajax({"url":"http:\/\/127.0.0.1:8000\/api\/lists?start_time=2020-06-05&end_time=2020-06-05"}).done(function(data) {
$(".ad_id").each(function(index, element) {
$(element).select2({
data: data,
"allowClear":true,"placeholder":{"id":"","text":"\u9009\u62e9"}
});
var value = $(element).data('value') + '';
if (value) {
value = value.split(',');
$(element).select2('val', value);
}
});
});
"""
我看请求也是对的。我单一在浏览器请求也是对的
推荐文章: