Orit
Active member
Hello
I'm trying to create a search box for searching for a node from the node list.
I've been using this thread as a guide:
I want to generate a node list in a dropdown menu, similar to the member list dropdown.
I'm having trouble displaying the results in the dropdown- all results are "undefined" so I understand I'm not rendering it correctly.
my code:
My\Addon\Pub\Controller\ForumFind.php (only the relevant function)
My\Addon\Pub\View\Find.php
my widget template:
This is what I get when trying to search:
The number of results is correct, but all results do not contain the title and the link.
What am I doing wrong?
Thanks!!
(I've been using the following link to view the results/errors when there were: http://localhost/hello/index.php?forumsearch/auto-complete&q=for&_xfRequestUri=/hello/index.php?members/admin.1/&_xfWithData=1&_xfToken=1717656091,46fa0ed547cbd68f7c62fbe96c461851&_xfResponseType=json Only I can access it of course. I used it to debugging)
(Please excuse any unnecessary/duplicate code. I have not yet done a cleanup...)
I'm trying to create a search box for searching for a node from the node list.
I've been using this thread as a guide:
I would like to create an autocomplete dropdown for things other than username (i.e. Forum Title). I noticed that the AutoComplete function only allows you to use username, since it is including the username and avatar URL. How would I go about doing this? Do I need to write my own AutoComplete function, or is there something I am overlooking? Thanks!
- LiquidPro
- Replies: 18
- Forum: XenForo development discussions
I want to generate a node list in a dropdown menu, similar to the member list dropdown.
I'm having trouble displaying the results in the dropdown- all results are "undefined" so I understand I'm not rendering it correctly.
my code:
My\Addon\Pub\Controller\ForumFind.php (only the relevant function)
PHP:
public function actionAutoComplete()
{
$q = ltrim($this->filter('q', 'str', ['no-trim']));
if ($q !== '' && utf8_strlen($q) >= 2)
{
/** @var \XF\Finder\Forum $forums */
$nodeRepo = $this->repository('XF:Node');
$forums = $nodeRepo->findNodesForList()
->where('node_type_id', 'Forum')
->where('title', 'LIKE', '%' . $q . '%')
->fetch();
$viewableForums = [];
foreach ($forums as $forum) {
if ($forum->canView()) {
$viewableForums[] = $forum;
}
}
}
else
{
$forums = [];
$viewableForums = [];
$q = '';
}
$results = $forums;
$viewParams = [
'q' => $q,
'forums' => $viewableForums, //$forums,
'results' => $results
];
return $this->view('Prog\Widgets:Find', '', $viewParams);
}
My\Addon\Pub\View\Find.php
PHP:
<?php
namespace My\Addon\Pub\View;
use XF\Mvc\View;
use XF\Mvc\Controller;
use XF\Mvc\Router;
use XF\App;
class Find extends View
{
/**
* @param string $link
* @param mixed $data
* @param array $parameters
*
* @return string
*/
public function buildLink($link, $data = null, array $parameters = [])
{
return \XF::app()->router()->buildLink($link, $data, $parameters);
}
public function renderJson()
{
$results = [];
foreach ($this->params['forums'] AS $forum)
{
$results[] = [
'node_id' => $forum->node_id,
'title' => $forum->title,
'url' => $this->buildLink('forums', $forum)
];
$test = $forum;
}
return [
'test' => $this->params['forums'],
'results' => $results,
'q' => $this->params['q']
];
}
}
my widget template:
HTML:
<xf:textboxrow name="forums" value="{$forums}" data-xf-init="auto-complete" label="{{ phrase('search_for_a_forum') }}"
data-single="1" data-acurl="{{ link('forumsearch/auto-complete') }}" />
This is what I get when trying to search:
The number of results is correct, but all results do not contain the title and the link.
What am I doing wrong?
Thanks!!
(I've been using the following link to view the results/errors when there were: http://localhost/hello/index.php?forumsearch/auto-complete&q=for&_xfRequestUri=/hello/index.php?members/admin.1/&_xfWithData=1&_xfToken=1717656091,46fa0ed547cbd68f7c62fbe96c461851&_xfResponseType=json Only I can access it of course. I used it to debugging)
(Please excuse any unnecessary/duplicate code. I have not yet done a cleanup...)
Last edited: