grails params坚持多选下拉列表的下拉列表选择

时间:2016-04-25 11:59:31

标签: javascript grails gsp

我有一个g:选择下拉列表,我用它来填充另一个g中显示的内容:选择grails中的下拉列表。我在第一个下拉列表中通过javascript onchange调用填充第二个下拉列表。然后,这将操纵哪些元素g:选择显示,哪些是隐藏的。

共有3个下拉列表全部设置为显示:none,直到从第一个下拉列表中进行选择。

然后使用第二个下拉列表确定从表单导出的参数值。

我的问题在于当第二个下拉值改变时,params会停留。当我第一次选择并导出值时,有时第二次从不同的下拉列表中导出值时,我得到正确的导出。然后在第三次导出前一个值而不是新值。

我已经尝试在每次选择更改时清除元素值,并且不知道如何在此阶段重置参数。我将在下面发布我的代码。任何帮助表示赞赏:

g:选择gsp代码:

<g:form>
<table>
    <tbody>

    <tr class="prop3">
        <td>
            <g:select id="target" name="componentchoice"
                      from="${['Export by Name', 'Export by Age', 'Export by Identifier']}"
                      noSelection="${['null': 'Please Choose...']}"
                      onchange="JavaScript:changeList()"/>
        </td>
        <td id="nameComponent" style="display: none">
            <g:select name="name"
                      from="${User.list().findAll {
                          it.name
                      }*.name.sort().unique()}"
                      noSelection="${['null': 'Please Choose...']}"
                      onchange="getElementValues()"/>
        </td>
        <td id="ageComponent" style="display: none">
            <g:select name="age"
                      from="${UserAge.list(sort: 'age')}"
                      noSelection="${['null': 'Please Choose...']}"
                      onchange="getElementValues()"/>
        </td>
        <td id="identifierComponent" style="display: none">
            <g:select name="identifier"
                      from="${Identifier.list().findAll {
                          it.identifier
                      }*.identifier.sort().unique()}"
                      noSelection="${['null': 'Please Choose...']}"
                      onchange="getElementValues()"/>
        </td>
    </tr>

    <tr class="prop3">
        <td valign="top" class="name">
            <g:actionSubmit value="Export" action="exportChoice"/>
        </td>
    </tr>

    </tbody>
</table>

Javascript代码:

<script type="text/javascript">

function getElementValues() {
    var visName = document.getElementsByName('name')[0].value;
    var visAge = document.getElementsByName('age')[0].value;
    var visID = document.getElementsByName('identifier')[0].value;
    console.log(visName);
    console.log(visAge);
    console.log(visID);
    visName.value = null;
    visAge.value = null;
    visID.value = null;
}
function changeList() {
    var choice = document.getElementById('target').value;
    var visName = document.getElementById('nameComponent');
    var visAge = document.getElementById('ageComponent');
    var visID = document.getElementById('identifierComponent');
    console.log('Change list is called');

    if (choice == 'Export by Name') {
        visName.style.display = "block";
        visID.style.display = "none";
        visAge.style.display = "none";
    }
    if (choice == 'Export by Age') {
        visAge.style.display = "block";
        visName.style.display = "none";
        visID.style.display = "none";
    }
    if (choice == 'Export by Identifier') {
        visID.style.display = "block";
        visAge.style.display = "none";
        visName.style.display = "none";
    }
}

</script>

控制器代码:

def exportChoice = { 
def choice = null 
if (params.name && params.name != 'null') {
  choice = User.findAllByName(params.name) 
} else if (params.age && params.age != 'null') {
  choice = UserAge.findAllByAge(params.age)  
} else if (params.identifier && params.identifier != 'null') {
  choice = Identifier.findAllByIdentifier(params.networkIdentifier) 
}

if (!choice) { 
  flash.message = "Unable to find Choice - Try refreshing if problem persists"
  redirect action: 'index'
} else {
  try {
    log.info "Writing Report to output Stream"
    def workbook = new ExcelExport(choice).workbook 
    if (params.name && params.name != 'null') { 
      response.setHeader("Content-disposition", "attachment; filename='${params.name}.xls'")
    } else if (params.age && params.age != 'null') {
      response.setHeader("Content-disposition", "attachment; filename='${params.age}.xls'")
    } else if (params.identifier && params.identifier != 'null') {
      response.setHeader("Content-disposition", "attachment; filename='${params.identifier}.xls'")
    }
    response.contentType = "application/vnd.ms-excel"
    workbook.write(response.outputStream)
    log.info "Flushing Stream"
    response.outputStream.flush()

  } catch (Exception e) {
    def errorMessage = "Error While Creating Excel (${e.message})"
    log.error errorMessage, GrailsUtil.sanitize(e)
    flash.message = errorMessage
    redirect action: 'index'
  }
}

}

0 个答案:

没有答案