【问题标题】:opening a modal for each item in a for loop separately分别为 for 循环中的每个项目打开一个模式
【发布时间】:2017-03-24 14:29:36
【问题描述】:

我正在显示大约 5 个面板,其中从一个数组呈现,然后显示每个面板都有自己的 ID、名称和要共享的内容。在每个面板中,我都有一个展开图标,我允许用户将选定的面板展开成一个大模式。我正在使用引导程序中的示例,但当前当我单击以模式打开时 - 它以大模式打开,但它为数组中的每个项目打开,而不仅仅是选定的面板。这是我到目前为止所拥有的:

 <div class="col-sm-4" *ngFor="let item of items; let i = index">
         <div class="panel panel-warning">
            <div class="panel-heading">
                {{item.title}}
               <div class="box-header-btns pull-right">
                  <a title="Expand" type="button" data-toggle="modal" data-target=".bs-example-modal-lg">
                      <i class="glyphicon glyphicon-resize-full"></i>
                  </a>
               </div>
             </div>
             <div class="panel-body modal fade bs-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
                 <div class="modal-dialog modal-lg" role="document">
                      <div class="modal-content">
                           {{item.content}}
                      </div>
                 </div>
             </div>
         </div>
  </div>

我的数组循环显示面板:

 items: Array<Panel> = [
        new Panel(1, 'panel 1', 'show text', 'test data in modal'),
        new Panel(2, 'panel 2','show image', 'more test data'),
        new Panel(3, 'panel 3', 'show graph', 'more and more data')
    ];

class Panel {
  constructor(
    public id: number, 
    public title: string, 
    public footer: string, 
    public content: any) 
}

目前,当我为一个项目打开一个模式时,它还会在模式关闭之前循环遍历所有其他项目。

【问题讨论】:

  • 你的 expandPanel() 方法是什么样的?
  • 请减少缩进,难以阅读
  • 也请发布您的模态 HTML 部分,另外根据我的说法,您需要动态提供data-target
  • 实际上,我认为这里的问题是您正在为ngFor每个 迭代创建一个独特的模式对话框
  • @aluanHaddad 我想了想并添加了更多代码,希望它会有所帮助。我可以看到 ngFor 是如何导致模态循环的,但不确定如何分离但仍能正常工作。

标签: jquery twitter-bootstrap angular


【解决方案1】:

这是一个带有工作代码的基本 plunkr(非常丑陋)。 https://plnkr.co/edit/lISLyUaMbc1W28sWRugi?p=info

正如我之前所说,这里的问题是您正在为循环的每次迭代创建一个不同的模式对话框。但是,由于您忘记包含选择项目的函数,因此几乎不可能说出实际错误的原因

@Component({
  selector: 'app',
  template: `

<div class="col-sm-4" *ngFor="let item of items; let i = index">
  <div class="panel panel-warning">
    <div class="panel-heading">
      <div class="box-header-btns pull-right" (click)="selectedItem = item">
        <a title="Expand" type="button" data-toggle="modal" data-target=".bs-example-modal-lg">
          <i class="glyphicon glyphicon-resize-full"></i> {{item.title}}
        </a>
      </div>
    </div>
  </div>
</div>
<div class="panel-body modal fade bs-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
  <div class="modal-dialog modal-lg" role="document">
    <div class="modal-content">
      {{selectedItem?.content}}
    </div>
  </div>
</div>
<pre>{{selectedItem && selectItem.content | json}}</pre>
  `
})
export class App {
  items = [{
    id: 1,
    title: 'panel 1',
    footer: 'show text',
    content: 'test data in modal'
  }, {
    id: 2,
    title: 'panel 2',
    footer: 'show image',
    content: 'more test data'
  }, {
    id: 3,
    title: 'panel 3',
    footer: 'show graph',
    content 'more and more data'
  }];
}

【讨论】:

  • 谢谢!我现在试一试。我真的没有得到&lt;pre&gt;{{selectedItem &amp;&amp; selectItem.content | json}}&lt;/pre&gt; 更多的是用于测试数据显示吗?
  • 只是为了在紧要关头调试输出,否则使用 Augery 之类的。
  • 这很好 - 谢谢你的帮助!我可能有另一个处理相同代码的问题,但我会为此创建一个单独的问题,希望您也能提供帮助。
  • @bluePearl 您可能想研究使用 ng-bootstrap 或其他更高级别、面向角度的库,它会为您节省大量时间。很高兴我能提供帮助:)
【解决方案2】:

内容应该加载到子组件中,例如看下面的代码在那个expandPanel中你可以添加一个参数并尝试使用它

<div class="col-sm-4" *ngFor="let item of items; let i = index">
            <div class="panel panel-warning">
                <div class="panel-heading">
                    {{item.title}}
                    <div class="box-header-btns pull-right">
                        <a title="Expand" (click)="expandPanel(i);" type="button" data-toggle="modal" data-target=".bs-example-modal-lg">
                            <i class="glyphicon glyphicon-resize-full"></i>
                        </a>
                    </div>
                </div>
                <div class="panel-body modal fade bs-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
                    <div class="modal-dialog modal-lg" role="document" *ngFor="let children of item.children">
                        <div class="modal-content">
                            {{children.content}}
                        </div>
                    </div>
                </div>
            </div>
        </div>

json文件应该是这样的

this.items= [
            {
                titile: "xxxxx",  children: [
                    { content: 'aaaaa'},
                   { content: 'bbbbbb'},
                  { content: 'cccccc'}
                ]
            },
            {
                title: "zzzzzz", children: [
                    { content: 'gggggg'},
                   { content: 'jjjjjjjjjj'}
                ]
            },
            {
                title: "ddddd", children: [
                    { content: 'ppppp'}
                ]
            }
        ];



 expandPanel(index: number) {
        this.items[index].active = !this.items[index].active;
        this.collapseAnother(index);
    }

collapseAnother(index: number) {
        for (var i = 0; i < this.items.length; i++) {
            if (i != index) {
                this.items[i].active = false;
            }
        }
    };

【讨论】:

  • 这没有任何意义。
  • 我试过这个,当我打开一个模式时,它会在关闭之前循环遍历所有项目。
  • 我正在使用引导程序,因此它会处理一些功能,例如关闭和打开,所以我不必配置它 - 我知道我的 div 中有 expandPanel 但我忘了删除它,因为我不再使用它。我会试一试,看看它是否按我想要的方式工作,并将其用于将来参考。谢谢
猜你喜欢
  • 2020-09-14
  • 1970-01-01
  • 2019-07-31
  • 2019-06-08
  • 2012-09-04
  • 2021-12-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多