刷新页面时 Cakephp 下拉列表未显示正确的数据
我的页面上有两个下拉列表,一个用于国家/地区,另一个用于城市。 当我最初加载页面时,我将国家/地区设置为从用户注册加载的默认国家/地区。然后我加载该国家/地区的城市。
问题是,现在当我更改国家/地区下拉列表并刷新页面时,我的国家/地区下拉列表所选值保持不变,但已更改,但城市正在从默认国家/地区加载。
我从控制器的视图中获得了正确的值,但视图没有将其设置正确。 我认为的代码如下
echo $this->Form->input('from_country_code',
array(
'options'=>$countries,
'id'=>'from_country_code',
'label' => __('Country',true),
'selected'=>$selectedCountryCode
)
);
其次,请解释一下我如何检测页面刷新并保留更改的下拉值并且不执行整个操作控制器代码。
任何帮助将不胜感激。
已更新 -- 这是我的控制器代码
function add() {
$currentUser = $this->Auth->user();
$countryMap = $this->requestAction('/countries/getList/');
$this->set('countries',$countryMap);
$this->set('selectedCountryCode',$currentUser['City']['countriesCode']);
$cityMap = $this->requestAction('/cities/getListByCountryCode/'.$currentUser['City']['countriesCode']);
$this->set('cities',$cityMap);
if(!empty($this->data)) {
if($this->Request->saveAll($this->data)) {
$this->Session->setFlash('The Request was successfully Posted');
$this->redirect(array('controller'=>'users','action'=>'requests'));
} else {
$this->Session->setFlash('The Request was not saved. Please try again');
}
}
}
请注意,我遇到的问题是在页面刷新时,国家/地区下拉列表的选定属性设置不正确。即使我刷新页面,我也检查了控制器中的值是否正确。
我在此变量 @selectedCountryCode 中设置所选国家/地区值。
Ive two dropdown lists one for country and other for cities on my page.
When i load the page initially i set the country to the default country loaded from the user registeration. Then i load the cities for that country.
The problem is that now when i change the country dropdown and refresh the page my country dropdown selected value remains the same which has changed but the cities are loading from the default country.
I'm getting the right value at the view from the controller but view is not setting it right.
The code in my view is as follows
echo $this->Form->input('from_country_code',
array(
'options'=>$countries,
'id'=>'from_country_code',
'label' => __('Country',true),
'selected'=>$selectedCountryCode
)
);
Secondly, please explain me that how can i detect that a page is refreshed and retain the changed drop down values and don't execute the whole action controller code.
Any help would be highly appreciated.
Updated --
Here its my controller code
function add() {
$currentUser = $this->Auth->user();
$countryMap = $this->requestAction('/countries/getList/');
$this->set('countries',$countryMap);
$this->set('selectedCountryCode',$currentUser['City']['countriesCode']);
$cityMap = $this->requestAction('/cities/getListByCountryCode/'.$currentUser['City']['countriesCode']);
$this->set('cities',$cityMap);
if(!empty($this->data)) {
if($this->Request->saveAll($this->data)) {
$this->Session->setFlash('The Request was successfully Posted');
$this->redirect(array('controller'=>'users','action'=>'requests'));
} else {
$this->Session->setFlash('The Request was not saved. Please try again');
}
}
}
Please note that the issue i'm having is that on page refresh the selected property of the country dropdown is not setting properly. Ive checked the value from controller its coming correctly even if i refresh the page.
Im setting the selected country value in this variable @selectedCountryCode.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定是否明白。您在同一页面上有两个下拉列表,您想要做的是根据第一个下拉列表中选定的元素更新第二个下拉列表的元素?因此用户必须点击刷新按钮才能更新第二个列表?
如果这就是您的意思,我认为这根本不是用户友好的,甚至不是在互联网上导航的标准方式。最重要的是,当您点击刷新按钮时,整个控制器操作会再次执行,这是绝对正常的。事实上,点击刷新按钮恰好意味着“请再次执行网页逻辑”。
如果您想要一种更用户友好的方式来更新第二个下拉列表,您应该搜索 Javascript 方式来执行此操作。例如,请参见: http://nuts-and-bolts-of-cakephp.com/2010/03/10/use-cakephp-jquery-to-build-dynamic-selects/
I'm not sure to understand. You have two dropdown lists on the same page and what you want to do is to update the elements of the second one according to the selected element in the first one ? And therefore the user will have to hit the refresh button to update the second list ?
If this is what you mean, I think this is not user friendly at all and even not a standard way to navigate on internet. On top of that, this is absolutely normal that the whole controller action is executed again when you hit the refresh button. In fact hitting the refresh button means exactly 'please execute the webpage logic again'.
If you want a more user friendly way to update the second dropdown list, you should search for a Javascript way to do it. See for instance: http://nuts-and-bolts-of-cakephp.com/2010/03/10/use-cakephp-jquery-to-build-dynamic-selects/
您不应该使用刷新来执行此操作,这会使控制器逻辑过于复杂。使用 javascript 根据国家/地区列表更改城市列表。您可能想使用 ajax 来获取城市列表。
You shouldn't use refresh to do that, that makes the controller logic too complicated. Use javascript to change the cities list based on the country list. You may want to use ajax to get the cities list.