Grails gsp 嵌套两个选择

发布于 2025-01-13 00:54:38 字数 751 浏览 7 评论 0原文

我需要将以下对象

companies = [
{ id: 1, code: "cod1", employessList : [{id:1, name:"name1"}, {id:2, name:"name2"}, ...]},
{ id: 2, code: "cod2", employessList : [{id:4, name:"name1"}, {id:5, name:"name2"}, ...]},
{ id: 3, code: "cod3", employessList : [{id:11, name:"name1"}, {id:12, name:"name2"}, ...]},
...
]

放入 gsp 中,两个 一个用于选择公司,另一个用于选择嵌套的员工。当我更换公司时,员工名单也会更新。

例如,

<div> 
   <g:select id="companiesSelect" name="companies" from="${companies}" 
   class="form-control"                           optionKey="id" 
   optionValue="code" />
<div> 

<div> 
  <!-- Div with nested elements -->
</div>

有人可以帮我解决这个问题吗?

i've the following object

companies = [
{ id: 1, code: "cod1", employessList : [{id:1, name:"name1"}, {id:2, name:"name2"}, ...]},
{ id: 2, code: "cod2", employessList : [{id:4, name:"name1"}, {id:5, name:"name2"}, ...]},
{ id: 3, code: "cod3", employessList : [{id:11, name:"name1"}, {id:12, name:"name2"}, ...]},
...
]

i need put in gsp, two <g:select> one to choose the companies and an another to select the employess nested. And when i change the company the employees list update.

for instance,

<div> 
   <g:select id="companiesSelect" name="companies" from="${companies}" 
   class="form-control"                           optionKey="id" 
   optionValue="code" />
<div> 

<div> 
  <!-- Div with nested elements -->
</div>

Could someone help me with this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦魇绽荼蘼 2025-01-20 00:54:38

这里有 4 个选项:

  1. 在服务器端渲染两个选择。

控制器操作:

def somePage() {
  def companies = [...]
  def employees = params.companyId ? companies.findResult{ it.id == params.companyId ? it.employessList : null } : []
  [ companies:companies, employees:employees ]
}

GSP:

<div> 
   <g:select id="companiesSelect" from="${companies}" onChange="refreshPage( this.value )" .../>
<div> 

<div> 
  <g:select id="employeesSelect" from="${employees}" .../>
</div>

其中 refreshPage( this.value ) 是一个 JS 函数,用于将公司 ID 发送到控制器操作。

  1. 将公司+员工的完整列表作为 JSON 加载到页面中,将该数据呈现为选择,并使用 onChange-JS 侦听器刷新第二个选择。

  2. 使用Ajax调用将基于公司ID的数据加载到员工选择中。

选项2和3需要大量JS,这里就不贴了。

  1. 使用像 React 这样的现代 JS 框架来进行渲染,以便服务器可以通过 REST 端点进行通信。

You have 4 options here:

  1. Render both selects on the server side.

Controller action:

def somePage() {
  def companies = [...]
  def employees = params.companyId ? companies.findResult{ it.id == params.companyId ? it.employessList : null } : []
  [ companies:companies, employees:employees ]
}

GSP:

<div> 
   <g:select id="companiesSelect" from="${companies}" onChange="refreshPage( this.value )" .../>
<div> 

<div> 
  <g:select id="employeesSelect" from="${employees}" .../>
</div>

where refreshPage( this.value ) is a JS-function to send the company id to controller action.

  1. Load the full list of companies+employees into the page as JSON, render that data as selects, and use the onChange-JS listener to refresh the 2nd select.

  2. Use Ajax call to load the data based on company id into employees select.

The options 2 and 3 require a lot of JS, so I won't post it here.

  1. Use a modern JS framework like react to do the rendering, so that the server would be communicated over REST endpoint.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文