简体   繁体   English

页面刷新后如何在grails中保留下拉选择?

[英]How to retain dropdown selection in grails after page refresh?

I know that there are already some questions about dropdown selection retaining but please don't close this one. 我知道关于下拉菜单选择保留已经存在一些问题,但是请不要关闭此问题。

I have a login page with language dropdown list. 我有一个带有语言下拉列表的登录页面。 User chooses a language and the page is refreshed with the new language. 用户选择一种语言,页面就会用新的语言刷新。 The selection, of course, disappears and the lang parameter isn't moved to the whole application. 当然,选择消失了,并且lang参数没有移到整个应用程序中。 Home page opens with the default language, not with the one user wants. 主页以默认语言打开,而不是一个用户想要的语言。

Here is my code: 这是我的代码:

.gsp file .gsp文件

    <g:select style="width: 60px; background-color: transparent; border: 0px;" 
    onchange="goToPage(this.value)" name="lang"              
    from="${LangCodeBook.list()}" value="" noSelection="['':'...']">
    </g:select>


    <script type="text/javascript">

       function goToPage(lang){
   if(lang=='GER'){ 
           window.location.href = "?lang=de_DE";
   }
   else if(lang=='RUS'){ 
           window.location.href = "?lang=ru_RU";
   }
   else if(lang=='ENG'){ 
           window.location.href = "?lang=en_GB";
   }
     }
   </script>

On the other side is MyEventListener.groovy and he saves into an object all the params (username, password, lang). 另一面是MyEventListener.groovy,他将所有参数(用户名,密码和lang)保存到对象中。

        def paramObject=RequestContextHolder.currentRequestAttributes().getParams()

The problem is that the lang param stays blank. 问题是lang参数保持空白。

Please help. 请帮忙。 Thanks. 谢谢。

Just give the select control an id , read the lang value from the query string & set it as the selected value of the select control onload as shown below. 只需给select控件一个id ,从查询字符串中读取lang值,并将其设置为select控件onload的选定值,如下所示。

document.getElementById('lang').value= getParameterByName("lang");

<g:select style="width: 60px; background-color: transparent; border: 0px;" 
    onchange="goToPage(this.value)" name="lang" id="lang" 
    from="${LangCodeBook.list()}" value="" noSelection="['':'...']">
    </g:select>


    <script type="text/javascript">
       document.getElementById('lang').value= getParameterByName("lang");
       function goToPage(lang){
       if(lang=='GER'){ 
           window.location.href = "?lang=de_DE";
       }
       else if(lang=='RUS'){ 
           window.location.href = "?lang=ru_RU";
       }
       else if(lang=='ENG'){ 
           window.location.href = "?lang=en_GB";
       }
     }
     function getParameterByName(name) {
     name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
     var regexS = "[\\?&]" + name + "=([^&#]*)";
     var regex = new RegExp(regexS);
     var results = regex.exec(window.location.search);
     if (results == null) return "";
     else return decodeURIComponent(results[1].replace(/\+/g, " "));
     }
   </script>

very simple 很简单

Use Ajax and Session varible in grails controller ... 在grails控制器中使用Ajax和Session变量...

ajax function jquery on change function on the drop down box 下拉框上的ajax函数jquery on change function

<g:javascript>
        function onChange(){
        //id = $this.value;
        $.ajax({ 
        context: $(this),
        url:"${resource()}"+"/yourLanagueController/yourSelectionHolderAction",                             type:"POST",
        data:{"dropdownboxid":id}
        });
        }
        </g:javascript>

your controller and session variables 您的控制器和会话变量

on login page some where make you session varible to null; 在登录页面上,使会话可变为null的某些位置;

`def session.selectedlanagueid = null`

def yourSelectionHolderAction(params){

 //always hold one ID only so when u
//set session varible make to null first then add
            session.selectedlanagueid = null 
        if(params.id)
                 session.selectedlanagueid = id;

        render 'test' // or '' // or any valid json render result
    }

ALLOHA !!! 阿罗哈!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM