使用参数动态创建宏组件的 id

发布于 2024-12-11 23:52:56 字数 2132 浏览 0 评论 0原文

我正在尝试创建两个执行完全相同功能的网格,而不必重复网格代码两次。所以,我决定使用宏组件。但是,我不确定如何动态创建宏组件中组件的 id。该代码执行以下操作:

第一个网格(西部地区)有两行,其中有两个文本框。如果我将“hello”添加到该网格中的第一个文本框,那么第二个文本框的值也设置为“hello”。

第二个网格(中心区域)有两行,有两个文本框。如果我将“world”添加到该网格中的第一个文本框,则第二个文本框的值也设置为“world”

  • 现在相同,即“hello”
  • 第一个网格中两个文本框的值 第二个网格现在相同,即“世界”,

我创建了一个 zul 文件,在其中使用宏组件,如下所示:

<?component name="mygrid1" macro-uri="grid1.zul" inline="true"?>
<zk>
<vbox hflex="1">
  <borderlayout height="500px" width="500px">
    <west size="50%">
      <mygrid1 id="grid1" index="1" />
    </west>
    <center>
      <mygrid1 id="grid2" index="2" />
    </center>
  </borderlayout>
</vbox>
</zk>
<zscript>
  fillInDuplicateBox(String value, Textbox duplicateBox) {
    if (!"".contentEquals(duplicateBox.value))
            return;
    duplicateBox.value = value;
  }
</zscript>
</window>

宏组件如下所示:

<zk>
<vbox hflex="1">
  <grid width="300px">
    <rows>
      <row> Box 1: <textbox id="${concat("newBox", arg.index)}" onChange="fillInDuplicateBox(${concat("newBox, arg.index)}.value, ${concat("duplicateBox", arg.index)})" hflex="1" /></row>
      <row> Box 2: <textbox id="${concat("duplicateBox", arg.index)}" hflex="1" /></row>
    </rows>
  </grid>                   
</vbox>
</zk>

我还尝试了以下代码来创建宏组件,

<zk>
  <vbox hflex="1">
    <grid width="300px">
      <rows>
         <row> Box 1: <textbox id="newBox${arg.index}" onChange="fillInDuplicateBox(newBox${arg.index}.value, duplicateBox${arg.index})" hflex="1" /></row>
         <row> Box 2: <textbox id="duplicateBox${arg.index}" hflex="1" /></row>
      </rows>
    </grid> 
  </vbox>
</zk>

但这些都不起作用。我不知道如何在宏组件中动态创建组件的 id。第一个网格的文本框 id 必须是“newBox1”、“duplicateBox1”,第二个网格的文本框 id 必须是“newBox2”、“duplicateBox2”

请指出是否有更好的方法来完成此任务。

谢谢, 索尼

I am trying to create two grids which perform exactly the same function without having to duplicate the code of the code for the grid twice. So, I decided to use a macro component. But, I am not sure how to create the ids of the components in the macro component dynamically. The code does the following:

The first grid(west region) has two rows with two textboxes. If I add "hello" to the first textbox in this grid then the value of the second textbox is also set to "hello".

The second grid(center region) has two rows with two textboxes. If I add "world" to the first textbox in this grid then the value of the second textbox is also set to "world"

  • The values of both textboxes in the first grid are now same i.e. "hello"
  • The values of both textboxes in the second grid are now same i.e. "world"

I created a zul file in which I use a macro component like so:

<?component name="mygrid1" macro-uri="grid1.zul" inline="true"?>
<zk>
<vbox hflex="1">
  <borderlayout height="500px" width="500px">
    <west size="50%">
      <mygrid1 id="grid1" index="1" />
    </west>
    <center>
      <mygrid1 id="grid2" index="2" />
    </center>
  </borderlayout>
</vbox>
</zk>
<zscript>
  fillInDuplicateBox(String value, Textbox duplicateBox) {
    if (!"".contentEquals(duplicateBox.value))
            return;
    duplicateBox.value = value;
  }
</zscript>
</window>

Macro component is shown below:

<zk>
<vbox hflex="1">
  <grid width="300px">
    <rows>
      <row> Box 1: <textbox id="${concat("newBox", arg.index)}" onChange="fillInDuplicateBox(${concat("newBox, arg.index)}.value, ${concat("duplicateBox", arg.index)})" hflex="1" /></row>
      <row> Box 2: <textbox id="${concat("duplicateBox", arg.index)}" hflex="1" /></row>
    </rows>
  </grid>                   
</vbox>
</zk>

I also tried the following code to create the macro component

<zk>
  <vbox hflex="1">
    <grid width="300px">
      <rows>
         <row> Box 1: <textbox id="newBox${arg.index}" onChange="fillInDuplicateBox(newBox${arg.index}.value, duplicateBox${arg.index})" hflex="1" /></row>
         <row> Box 2: <textbox id="duplicateBox${arg.index}" hflex="1" /></row>
      </rows>
    </grid> 
  </vbox>
</zk>

None of this works. I am not sure how to dynamically create the ids of the components in the macro component. The textbox ids of the first grid must be "newBox1", "duplicateBox1" and the textbox ids of the second grid must be "newBox2", "duplicateBox2"

Please point out if there is a better way of achieving this task.

Thanks,
Sony

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

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

发布评论

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

评论(1

薔薇婲 2024-12-18 23:52:56

我不确定是否可以动态创建这样的 id,但我很确定最好避免它。有更好的方法可以完全避免 id 问题并使用数据绑定或事件处理来实现此目的。您的示例表明每个网格的主要目的是将一个文本框的值复制到另一个文本框 - 我想您正在尝试实现更大的目标,所以请让我们知道这个答案是否会引导您找到您需要的东西。

我简化并扩展了您的示例,给出了两个示例,第一个示例使用 onChanging 在您键入时立即复制。第二对框使用数据绑定。

<?component name="mygrid1" macro-uri="/grid1.zul" ?>
<zk>
   <window>
      <vbox hflex="1">
         <mygrid1 id="grid1" myGridTitle="First" />
         <mygrid1 id="grid2" myGridTitle="Another" />
      </vbox>
   </window>
</zk>

以下是 grid1.zul 中的宏组件:

<zk>
   <zscript><![CDATA[
   String myBoundString = "initial value";
]]></zscript>
   <vbox hflex="1">
      <grid>
         <rows>
            <row>
               <hbox><label value="${arg.myGridTitle}" /> Source</hbox>
               <textbox id="originalText" hflex="1" onChanging="duplicateText.value = event.value" />
            </row>
            <row>
               <hbox><label value="${arg.myGridTitle}" /> Source copies here:</hbox>
               <textbox id="duplicateText" hflex="1" />
            </row>
            <row>
               Bound to myBoundString:
               <textbox id="boundText1" value="@{myBoundString}" hflex="1" />
            </row>
            <row>
               Bound to boundText1:
               <textbox id="boundText2" value="@{boundText1.value, load-when=boundText1.onChange}" hflex="1" />
            </row>
         </rows>
      </grid>
   </vbox>
</zk>

在数据绑定示例中,您必须更改“初始值”,然后在活页夹更新boundText2 之前按 Tab 键移开。另请注意,文本框确实有 id(boundText1 和boundText2),但这对在宏组件的多个实例中实现预期功能没有影响。

I am not sure if it is possible to dynamically create ids like that, but I am pretty certain it would be better to avoid it. There are better ways to do this by avoiding the id problem altogether and to use data binding or event handling instead. Your example indicates the main purpose for each grid is to copy the value of one Textbox to a different Textbox - I imagine there is something bigger you are trying to achieve so let us know if this answer leads you to what you need or not.

I have simplified and expanded your sample to give two examples, the first uses the onChanging to immediately copy as you type. The second pair of boxes uses databinding.

<?component name="mygrid1" macro-uri="/grid1.zul" ?>
<zk>
   <window>
      <vbox hflex="1">
         <mygrid1 id="grid1" myGridTitle="First" />
         <mygrid1 id="grid2" myGridTitle="Another" />
      </vbox>
   </window>
</zk>

Here is the macro component in grid1.zul:

<zk>
   <zscript><![CDATA[
   String myBoundString = "initial value";
]]></zscript>
   <vbox hflex="1">
      <grid>
         <rows>
            <row>
               <hbox><label value="${arg.myGridTitle}" /> Source</hbox>
               <textbox id="originalText" hflex="1" onChanging="duplicateText.value = event.value" />
            </row>
            <row>
               <hbox><label value="${arg.myGridTitle}" /> Source copies here:</hbox>
               <textbox id="duplicateText" hflex="1" />
            </row>
            <row>
               Bound to myBoundString:
               <textbox id="boundText1" value="@{myBoundString}" hflex="1" />
            </row>
            <row>
               Bound to boundText1:
               <textbox id="boundText2" value="@{boundText1.value, load-when=boundText1.onChange}" hflex="1" />
            </row>
         </rows>
      </grid>
   </vbox>
</zk>

In the databinding example, you have to change "intial value" and then tab away before the binder updates boundText2. Also notice that the TextBoxes do have ids (boundText1 and boundText2) but that has no impact on achieving the expected functionality in multiple instances of the macro component.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文