如何将 CheckboxsetField 的结果格式设置为 SilverStripe 中的逗号分隔字符串?
How can I get the format the results of a CheckboxsetField as a comma separated string in SilverStripe?
我正在尝试为数据列表创建一些过滤器。我希望用户能够从标签列表中 select 一个或多个过滤器,然后根据这些过滤器吐出一个对象列表。使用此代码根据正在发送的 URL 参数获取数据一切都很好...
public function index(SS_HTTPRequest $request)
{
// ...
if($tagsParam = $request->getVar('tags')) {
$articles = new ArrayList();
$tagids = explode(",", $tagsParam);
foreach($tagids AS $tagid) {
$tag = Category::get()->byID($tagid);
$articleitems = $tag->getManyManyComponents('Articles')->sort('Date DESC');
foreach($articleitems AS $articleitem) {
$articles->push($articleitem);
}
}
}
$data = array (
'Articles' => $articles
);
if($request->isAjax()) {
return $this->customise($data)->renderWith('ListItems');
}
return $data;
}
该代码适用于 URL,例如 mysite.com/?tags=1,2,3
我的问题是尝试根据使用 CheckboxSetField 构建的过滤器生成 URL。这是我的代码...
public function ArticlesSearchForm()
{
$tagsmap = $this->getTags()->map('ID', 'Title')->toArray();
$form = Form::create(
$this,
'ArticlesSearchForm',
FieldList::create(
CheckboxSetField::create('tags')
->setSource($tagsmap)
),
FieldList::create(
FormAction::create('doArticlesSearch','Search')
)
);
$form->setFormMethod('GET')
->setFormAction($this->Link())
->disableSecurityToken()
->loadDataFrom($this->request->getVars());
return $form;
}
当用户提交该表单时,生成的 URL 类似于 mysite.com?tags%5B1%5D=1&tags%5B2%5D=2&action_doArticlesSearch=搜索 显然,它将值作为数组传递。我怎样才能传递一个简单的逗号分隔列表?
与其尝试更改 CheckboxSetField
的 return,我建议您更改代码。鉴于您正在将逗号分隔的列表列表转换为已经存在的数组:
$tagids = explode(",", $tagsParam);
类似这样的,将跳过这一步:
public function index(SS_HTTPRequest $request)
{
// ...
if($tagsParam = $request->getVar('tags')) {
$articles = new ArrayList();
//This has a minor risk of going bad if $tagsParam is neither an
//array of a comma-separated list
$tagids = is_array($tags) ? $tagsParam : explode(",", $tagsParam);
我正在尝试为数据列表创建一些过滤器。我希望用户能够从标签列表中 select 一个或多个过滤器,然后根据这些过滤器吐出一个对象列表。使用此代码根据正在发送的 URL 参数获取数据一切都很好...
public function index(SS_HTTPRequest $request)
{
// ...
if($tagsParam = $request->getVar('tags')) {
$articles = new ArrayList();
$tagids = explode(",", $tagsParam);
foreach($tagids AS $tagid) {
$tag = Category::get()->byID($tagid);
$articleitems = $tag->getManyManyComponents('Articles')->sort('Date DESC');
foreach($articleitems AS $articleitem) {
$articles->push($articleitem);
}
}
}
$data = array (
'Articles' => $articles
);
if($request->isAjax()) {
return $this->customise($data)->renderWith('ListItems');
}
return $data;
}
该代码适用于 URL,例如 mysite.com/?tags=1,2,3
我的问题是尝试根据使用 CheckboxSetField 构建的过滤器生成 URL。这是我的代码...
public function ArticlesSearchForm()
{
$tagsmap = $this->getTags()->map('ID', 'Title')->toArray();
$form = Form::create(
$this,
'ArticlesSearchForm',
FieldList::create(
CheckboxSetField::create('tags')
->setSource($tagsmap)
),
FieldList::create(
FormAction::create('doArticlesSearch','Search')
)
);
$form->setFormMethod('GET')
->setFormAction($this->Link())
->disableSecurityToken()
->loadDataFrom($this->request->getVars());
return $form;
}
当用户提交该表单时,生成的 URL 类似于 mysite.com?tags%5B1%5D=1&tags%5B2%5D=2&action_doArticlesSearch=搜索 显然,它将值作为数组传递。我怎样才能传递一个简单的逗号分隔列表?
与其尝试更改 CheckboxSetField
的 return,我建议您更改代码。鉴于您正在将逗号分隔的列表列表转换为已经存在的数组:
$tagids = explode(",", $tagsParam);
类似这样的,将跳过这一步:
public function index(SS_HTTPRequest $request)
{
// ...
if($tagsParam = $request->getVar('tags')) {
$articles = new ArrayList();
//This has a minor risk of going bad if $tagsParam is neither an
//array of a comma-separated list
$tagids = is_array($tags) ? $tagsParam : explode(",", $tagsParam);