在phalcon框架中,我想填充相关的下拉类别列表。但是我在代码中遇到了一些问题:
#1。在选择类别列表->子类别列表上显示:未定义(未使用值填充选项)
#2。如果数据库没有数据console.log显示:未定义的变量:我的控制器中的resData
#3。如果选择值为“ 0”的类别,则不会再次禁用子类别列表
我在代码中做错了什么?
[Module.php]
subsubsection
[CONTROLLER]
use Phalcon\Mvc\View;
use Phalcon\Mvc\View\Engine\Volt;
$di->setShared('view', function () use ($config){
$view = new View();
$view->setViewsDir(APP_PATH . $config->appB->viewsDir);
# Register Volt Template
$view->registerEngines(array(
".volt" => function($view, $di) use ($config) {
$volt = new Volt($view, $di);
$volt->setOptions(
array(
'compiledPath' => APP_PATH . $config->appB->cacheDir,
'compiledExtension' => '.php',
'compiledSeparator' => '_',
'compileAlways' => true,
'autoescape' => false,
'stat' => true
)
);
$compiler = $volt->getCompiler();
$compiler->addFunction('strtotime','strtotime');
return $volt;
}
));
return $view;
});
[输入电压]
public function entryAction()
{
$formAction = 'backend/index/insert';
$this->view->setVar('action',$formAction);
$this->view->product_title = '';
$this->view->product_price = '';
$this->view->product_keyword = '';
$this->view->product_image = '';
$this->view->product_desc = '';
$category = Categories::find();
$this->view->setVar('categories',$category);
$this->view->pick("index/entry");
}
public function getSubcategoryAction()
{
$id = $this->request->getPost('id');
$data = Subcat::findBycategory_id($id);
$resData = array();
foreach($data as $result)
{
$resData[] = array('id' => $result->id, 'category_id' => $result->category_id, 'subcategory' => $result->subcategory_name);
}
echo(json_encode($resData));
//$this->view->setVar('subcategory',$resData);
}
[JQUERY]
Category:<select name="category" id="category">
<option value="0">Choose Category ...</option>
{% for category in categories %}
<option value="{{category.id}}">{{category.categoryname}}</option>
{% endfor %}
</select><br/>
sub-Category:<select name="subcategory" id="subcategory" disabled="disabled"><option value="0">Choose Sub-Category ...</option></select>
<br/>
Products:<select name="products" id="products" disabled="disabled"><option value="0">Choose a Product ...</option></select>
<br/>
答案 0 :(得分:2)
请注意,在控制器的第一行中,您正在禁用视图,因此不会处理Volt。在您现在工作时,jQuery仅接收JSON结果,因此您要附加JSON而不是Volt。 您必须选择一个路径:要么使用Volt,在这种情况下,您必须在两个操作中都删除第1行并使用参数处理视图,或者继续向jQuery发送JSON数据,并正确设置以处理JSON响应(检查此answer)
在您的情况下,getSubcategoryAction()
如下所示:
public function getSubcategoryAction()
{
//$this->view->disable(); //Replaced by:
$this->view->setRenderLevel(
View::LEVEL_ACTION_VIEW
);
$id = $this->request->getPost('id');
$data = Subcat::findBycategory_id($id);
foreach($data as $result)
{
$resData[] = array('id' => $result->id, 'category_id' => $result->category_id, 'subcategory' => $result->subcategory_name);
}
$this->view->setVar('categories', $resData);
}
这是假设您已在自己的DI中将Volt设置为渲染引擎,并且Volt模板对应于../app/views/index/getSubcategory.phtml,即:
<?php
use Phalcon\Mvc\View;
use Phalcon\Mvc\View\Engine\Volt;
// Register Volt as a service
$di->set(
'voltService',
function ($view, $di) {
$volt = new Volt($view, $di);
$volt->setOptions(
[
'compiledPath' => '../app/compiled-templates/',
'compiledExtension' => '.compiled',
]
);
return $volt;
}
);
// Register Volt as template engine
$di->set(
'view',
function () {
$view = new View();
$view->setViewsDir('../app/views/');
$view->registerEngines(
[
'.volt' => 'voltService',
]
);
return $view;
}
);
伏特注册代码是从以下位置复制的:https://docs.phalconphp.com/en/3.4/volt根据您的App结构修改目录。
答案 1 :(得分:0)
按照此操作,只更新您的代码...
[JQuery]
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title></title>
<script src="Scripts/jquery-3.3.1.min.js"></script>
</head>
<body>
<form method="post">
<span name="a">Check it</span> <input type="checkbox" id="id_exam_choices_1" />
<span name="a">Check it</span> <input type="checkbox" id="id_exam_choices_2" />
<span name="a">Check it</span> <input type="checkbox" id="id_exam_choices_3" />
</form>
</body>
</html>