如何在cakephp中创建链选择表单
我的业务目录应用程序需要 3 个链接的选择框,我使用 cakephp 来构建此应用程序。
这些部分的层次结构和选择顺序如下:
1 - 业务组
2 - 业务类型
3 - 城市(包含在表客户中)
关系为:
客户 HABTM 业务类型
业务组有多种业务类型
业务类型有一个业务组,HABTM客户
我搜索了jquery插件,可以帮助有了这个,并找到了雷米·夏普(Remy Sharp)的一个,但它没有我所拥有的更复杂的关系。 http://remysharp.com/2007/09/18 /auto-populate-multiple-select-boxes/
我想象发生的是第一个选择框(业务组)被预先填充,一旦做出选择,事件侦听器就会发送一条消息来过滤第二个选择框,第三个框也一样。
我不知道如何根据事件监听器构建搜索操作。
有什么建议还是我离谱了?
一如既往,我来到井边寻求帮助。
非常感谢。 保罗
非常感谢尼克,我读过你的很多帖子,我非常感谢你的回复。
我已按照您的指示进行操作,但遇到了问题。我已经尽力解决它们,但还没有弄清楚。
这是我到目前为止所做的:
1)在business_type和business_directory中创建“链接”操作(将客户重命名为业务目录,这更合适。)
业务类型链接操作:
function chained($business_group_id) {
$business_types = $this->BusinessType->find('list', array(
'conditions' => array( 'BusinessType.business_group_id' => $business_group_id)
));
$this->set('business_types', $business_types);
}
业务目录链接操作:
function chained($business_type_id) {
$business_directories = $this->BusinessDirectory->bindModel(array( 'hasOne' => array('business_directories_business_types' )));
$business_directories = $this->BusinessDirectory->find('all', array(
'fields' => array( ' BusinessDirectory.city'),
'conditions' => array( 'business_directories_business_types.business_type_id' => $business_type_id)
));
$this->set('business_directories', $business_directories);
}
我确实发现对于 HABTM 关系,使用 find 'list' 不会创建连接查询,而 find 'all' 会创建连接查询。
2)然后我在业务目录和相应的视图中创建了一个搜索操作。
对于业务组,我创建了一个 getList 操作来填充搜索表单中的选项列表:
function getList() {
return $this->BusinessGroup->find('list');
}
在搜索视图中,我添加了链选择的 javascript:
<script type="text/javascript">
<!--
$(function () {
var group = $('#businessGoup');
var type = $('#businessType');
var city = $('#businessDirectoryCity');
type.selectChain({
target: city,
url: '../business_directories/chained/'+$(this).val(),
data: { ajax: true, anotherval: "anotherAction" }
});
group.selectChain({
target: type,
url: '../business_types/chained/'+$(this).val()
}).trigger('change');
});
//-->
</script>
以及表单:
create('business_directories', array('action'=>'/search_results')); ?> input('business_group_id', array( 'type' => 'select', 'id' => 'businessGoup', 'empty' => '-- Select Business Group --', 'multiple' => true, 'options' => $this->requestAction('/business_groups/getList' ), 'label' => 'Business Group')); ?> input('business_type.id', array( 'type' => 'select', 'id' => 'businessType', 'empty' => '-- Select Business Type --', 'multiple' => true, 'options' => 'none selected', 'label' => 'Business Type')); ?> input('business_directories.id', array( 'type' => 'select', 'id' => 'businessDirectoryCity', 'empty' => '-- Select City --', 'multiple' => true, 'options' => 'options', 'label' => 'City')); ?> end('Search'); ?>当我测试业务类型链函数时,/business_types /chained/1,一切正常。
但是当我测试搜索视图时,我收到了 javascript 警报错误。然后,当我检查 firebug 时,出现以下两个错误:
警告(2):缺少 BusinessTypesController::chained() 的参数 1 [APP\controllers\business_types_controller.php,第 71 行]
注意(8):未定义的变量:business_group_id [ APP\controllers\business_types_controller.php,第 73 行]
非常感谢任何额外的帮助。
谢谢,保罗
My business directory application calls for 3 chained select boxes, and I'm using cakephp to build this application.
The hierarchy and order of choices for the sections is this:
1 - business group
2 - business type
3 - city (included in table customer)
The relationships are:
customer HABTM business types
business groups have many business types
business types have one business group, HABTM customers
I have searched for jquery plugins that help with this, and found one by Remy Sharp, but it doesn't have the more complex relationships I have.
http://remysharp.com/2007/09/18/auto-populate-multiple-select-boxes/
What I imagine happening is the first selection box (business groups) is pre-populated and once a selection is made, an event listener send a message that filters the second selection box, and the same for the third.
What I don't know is how to structure the search action based on the event listener.
Any advice or am I way off base?
As always, I come to the well for help.
Much appreciated.
Paul
Thanks very much Nick, I've read many of your posts I really appreciate your response.
I've followed your instructions but have run into problems. I've tried my best to resolve them but haven't figured it out.
This is what I've done so far:
1) created 'chained' actions in both the business_type and business_directory (renamed customer to business directory, which is more appropriate.)
business type chained action:
function chained($business_group_id) {
$business_types = $this->BusinessType->find('list', array(
'conditions' => array( 'BusinessType.business_group_id' => $business_group_id)
));
$this->set('business_types', $business_types);
}
business directory chained action:
function chained($business_type_id) {
$business_directories = $this->BusinessDirectory->bindModel(array( 'hasOne' => array('business_directories_business_types' )));
$business_directories = $this->BusinessDirectory->find('all', array(
'fields' => array( ' BusinessDirectory.city'),
'conditions' => array( 'business_directories_business_types.business_type_id' => $business_type_id)
));
$this->set('business_directories', $business_directories);
}
I did find that with a HABTM relationship, using find 'list' didn't create the join query, whereas find 'all' did.
2) I then created a search action in the business directory and corresponding view.
For the business groups I created a getList action to populate the option list in the search form:
function getList() {
return $this->BusinessGroup->find('list');
}
In the search view, I've added the javascript for the chain select:
<script type="text/javascript">
<!--
$(function () {
var group = $('#businessGoup');
var type = $('#businessType');
var city = $('#businessDirectoryCity');
type.selectChain({
target: city,
url: '../business_directories/chained/'+$(this).val(),
data: { ajax: true, anotherval: "anotherAction" }
});
group.selectChain({
target: type,
url: '../business_types/chained/'+$(this).val()
}).trigger('change');
});
//-->
</script>
And the form:
create('business_directories', array('action'=>'/search_results')); ?>
input('business_group_id',
array( 'type' => 'select',
'id' => 'businessGoup',
'empty' => '-- Select Business Group --',
'multiple' => true,
'options' => $this->requestAction('/business_groups/getList' ),
'label' => 'Business Group'));
?>
input('business_type.id',
array( 'type' => 'select',
'id' => 'businessType',
'empty' => '-- Select Business Type --',
'multiple' => true,
'options' => 'none selected',
'label' => 'Business Type'));
?>
input('business_directories.id',
array( 'type' => 'select',
'id' => 'businessDirectoryCity',
'empty' => '-- Select City --',
'multiple' => true,
'options' => 'options',
'label' => 'City'));
?>
end('Search'); ?>
When I test the business type chain function, /business_types/chained/1, everything works.
But when I test the search view, I get a javascript alert error. Then when I check firebug, I get the following two errors:
Warning (2): Missing argument 1 for BusinessTypesController::chained() [APP\controllers\business_types_controller.php, line 71]
Notice (8): Undefined variable: business_group_id [APP\controllers\business_types_controller.php, line 73]
Any additional help with this is very much appreciated.
Thanks, Paul
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要的是在控制器中有 2 个操作(business_type 和 customer)。
每个动作都应该是这样的。在这种情况下,对于业务类型
,当然您还需要查看该操作,该操作将以链式选择的正确格式格式化值。
对于业务组,您需要直接显示所有值,因此不需要 ajax。
客户控制器的操作类似,但您需要选择所有相关客户的城市。
然后,通过链式选择,您需要设置适当的元素并设置需要调用的适当操作。
IE:
What you need is to have 2 actions in the controllers (business_type and customer).
each action should look like this. In that case for the business type
of course you need also view for that action which will format the values in the proper format for the chained select.
For Business group you need to show all values directly so no ajax is needed.
The Customer controller's action is similar, but you need to select cities of all related customers.
Then with the chained select you need to set the proper elements and set the proper actions which need to be called.
i.e.: