如何在 octobercms 中使用路由来建立依赖关系?
How to use route for dependent relationship in octobercms?
我目前正在使用 ajax 数据请求处理前端表单。在我的代码中,我在 php 块中有这个,当 select-box 中的类别被 selected 显示子类别时,它会部分显示。
function onChangeCat()
{
$this['subs'] = Cat::whereHas('parent', function ($query) use($cats){
$query->where('cats','=', $cats );
})->pluck('cat_title', 'id');
我正在尝试将它连接到一条路线,以便当用户单击某个类别时,相关的子类别会显示在第二个 select 框中。
这是我的路由文件,其中#id of category select-box 作为参数
Route::get('ajax/{cats}' , function () {
//
return json_encode();
});
如何将 php 块和路由中的代码连接起来,以便只显示类别的相关子类别?
October CMS 有一个 $this->params()
方法,可以调用该方法从当前请求中获取 url 参数(参见 here)。您的代码应如下所示(未经测试):
Route::get('ajax/{cats}' , function () {
$results = Cat::whereHas('parent', function ($query) {
$query->where('cats', $this->param('cats'));
})->pluck('cat_title', 'id')->all();
return $results;
}
要将当前元素值传递给 Ajax Handler
,您需要给它 name - attribute
并添加 data-request="onChange"
处理程序。所有其他事情将由 October CMS Ajax Api
处理
<select name="country" data-request="onChange">
<option id="1">A</option>
<option id="2">B</option>
<option id="3">C</option>
<option id="4">D</option>
</select>
In your Ajax handler
function onChange() {
$id = post('country'); // as we name it `country` for select
// ^ - this will be your selected value [id]
return ['data' => 'some data'];
}
Further process data [ IF its needed ] Other wise you can just use data-request-update="calcresult: '#result'"
with returning Html Markup
<script>
function myFunction(data) {
console.log(data)
}
</script>
<select
name="first"
data-request="onChange"
data-request-success="myFunction(data)">
...
</select>
在 success-full request
这将调用 myFunction
与 return 数据在我们的例子中它将是 {'data':'some data'}
JSON 对象,无论你 return 来自 Ajax-Handler
.
如有疑问请评论。
我目前正在使用 ajax 数据请求处理前端表单。在我的代码中,我在 php 块中有这个,当 select-box 中的类别被 selected 显示子类别时,它会部分显示。
function onChangeCat()
{
$this['subs'] = Cat::whereHas('parent', function ($query) use($cats){
$query->where('cats','=', $cats );
})->pluck('cat_title', 'id');
我正在尝试将它连接到一条路线,以便当用户单击某个类别时,相关的子类别会显示在第二个 select 框中。
这是我的路由文件,其中#id of category select-box 作为参数
Route::get('ajax/{cats}' , function () {
//
return json_encode();
});
如何将 php 块和路由中的代码连接起来,以便只显示类别的相关子类别?
October CMS 有一个 $this->params()
方法,可以调用该方法从当前请求中获取 url 参数(参见 here)。您的代码应如下所示(未经测试):
Route::get('ajax/{cats}' , function () {
$results = Cat::whereHas('parent', function ($query) {
$query->where('cats', $this->param('cats'));
})->pluck('cat_title', 'id')->all();
return $results;
}
要将当前元素值传递给 Ajax Handler
,您需要给它 name - attribute
并添加 data-request="onChange"
处理程序。所有其他事情将由 October CMS Ajax Api
<select name="country" data-request="onChange">
<option id="1">A</option>
<option id="2">B</option>
<option id="3">C</option>
<option id="4">D</option>
</select>
In your Ajax handler
function onChange() {
$id = post('country'); // as we name it `country` for select
// ^ - this will be your selected value [id]
return ['data' => 'some data'];
}
Further process data [ IF its needed ] Other wise you can just use data-
request-update="calcresult: '#result'"
with returningHtml Markup
<script>
function myFunction(data) {
console.log(data)
}
</script>
<select
name="first"
data-request="onChange"
data-request-success="myFunction(data)">
...
</select>
在 success-full request
这将调用 myFunction
与 return 数据在我们的例子中它将是 {'data':'some data'}
JSON 对象,无论你 return 来自 Ajax-Handler
.
如有疑问请评论。