无法在Angular中通过JQuery样式化通过JQuery添加的TD。

huangapple go评论84阅读模式
英文:

Cannot style TDs added via JQuery in Angular

问题

I'm setting up a form in Angular. In the middle of hotel.view.html file, I have a table:

<table id="rooms">
   <tr>
     <td>Room Type</td><td>Beds</td><td>Q.ty</td>
   </tr>
   <tr>
     <td><input [(ngModel)]="HotelModel.rooms[0].type" name="type" type="text"></td>
     <td><input [(ngModel)]="HotelModel.rooms[0].beds" name="beds" type="number" class="smallField"></td>
     <td><input [(ngModel)]="HotelModel.rooms[0].qty" name="qty" type="number" class="smallField"></td>
   </tr>      
 </table>

In hotel.component.css I have:

.smallField {
    display: inline-block; 
    width: 50px;
}
            
.bigField {
    display: inline-block; 
    width: 170px;
}

The reason for that "display: inline-block;" is that, initially, I was adding a DIV. However, nothing happens if I remove it.

The fields in the table row are displayed correctly.
Now, I need to add a line when pressing a button. I do it this way:

$("#addRoom").on("click", function() {
        $('#rooms tr:last').after(
          '<tr><td><input [(ngModel)]="HotelModel.rooms[' + rm + '].type" name="type" type="text"></td>' + 
              '<td><input [(ngModel)]="HotelModel.rooms[' + rm + '].beds" name="beds" type="number" class="smallField"></td>' + 
              '<td><input [(ngModel)]="HotelModel.rooms[' + rm + '].qty" name="qty" type="number" class="smallField"></td></tr>');
          rm = rm + 1;
        });

Now, the table row is correctly added, and the fields are displayed, but they don't take the "class="smallField"", so that I have something like this:

无法在Angular中通过JQuery样式化通过JQuery添加的TD。

Any ideas about how to style them? Thanks

英文:

I'm setting up a form in Angular. In the middle of hotel.view.html file, I have a table:

&lt;table id=&quot;rooms&quot;&gt;
   &lt;tr&gt;
     &lt;td&gt;Room Type&lt;/td&gt;&lt;td&gt;Beds&lt;/td&gt;&lt;td&gt;Q.ty&lt;/td&gt;
   &lt;/tr&gt;
   &lt;tr&gt;
     &lt;td&gt;&lt;input [(ngModel)]=&quot;HotelModel.rooms[0].type&quot; name=&quot;type&quot; type=&quot;text&quot;&gt;&lt;/td&gt;
     &lt;td&gt;&lt;input [(ngModel)]=&quot;HotelModel.rooms[0].beds&quot; name=&quot;beds&quot; type=&quot;number&quot; class=&quot;smallField&quot;&gt;&lt;/td&gt;
     &lt;td&gt;&lt;input [(ngModel)]=&quot;HotelModel.rooms[0].qty&quot; name=&quot;qty&quot; type=&quot;number&quot; class=&quot;smallField&quot;&gt;&lt;/td&gt;
   &lt;/tr&gt;      
 &lt;/table&gt;

In hotel.component.css I have:

.smallField {
    display: inline-block; 
    width: 50px;
}
            
.bigField {
    display: inline-block; 
    width: 170px;
}

The reason for that "display: inline-block;" is that, initially, I was adding a DIV. However, nothing happens if I remove it.

The fields in the table row are displayed correctly.
Now, I need to add a line when pressing a button. I do it this way:

$(&quot;#addRoom&quot;).on(&quot;click&quot;, function() {
        $(&#39;#rooms tr:last&#39;).after(
          &#39;&lt;tr&gt;&lt;td&gt;&lt;input [(ngModel)]=&quot;HotelModel.rooms[&#39; + rm + &#39;].type&quot; name=&quot;type&quot; type=&quot;text&quot;&gt;&lt;/td&gt;&#39; + 
              &#39;&lt;td&gt;&lt;input [(ngModel)]=&quot;HotelModel.rooms[&#39; + rm + &#39;].beds&quot; name=&quot;beds&quot; type=&quot;number&quot; class=&quot;smallField&quot;&gt;&lt;/td&gt;&#39; + 
              &#39;&lt;td&gt;&lt;input [(ngModel)]=&quot;HotelModel.rooms[&#39; + rm + &#39;].qty&quot; name=&quot;qty&quot; type=&quot;number&quot; class=&quot;smallField&quot;&gt;&lt;/td&gt;&lt;/tr&gt;&#39;);
          rm = rm + 1;
        });

Now, the table row is correctly added and the fields are displayed, but they don't take the "class="smallField", so that I have something like this:

无法在Angular中通过JQuery样式化通过JQuery添加的TD。

Any ideas about how to style them? Thanks

答案1

得分: 2

如我在上面的评论中提到的,永远不要在Angular项目中使用jQuery。你正在做的事情相当基础,可以只使用Angular来完成。

我假设你的数据在一个数组中,所以你只需要向该数组添加一个新项。Angular会知道这个变化,这比尝试使用一个单独的库来注入Angular需要知道和解析的原始HTML要容易得多。

只需使用Angular的ngFor循环来显示数组。按钮将向数组添加一个新项,这就是你需要做的一切。

英文:

As I mentioned in a comment above never use jQuery in an Angular project. What you are doing here is fairly basic and it can be accomplished with Angular alone.

I assume your data is in an array, so all you have to do is add a new item to that array. Angular will know about it, and it's so much easier than trying to use a separate library to inject raw HTML that Angular will somehow need to know about and parse.

Just use an Angular ngFor loop to show the array. The button will add a new item to the array, and that's all you have to do.

&lt;table id=&quot;rooms&quot;&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Room Type&lt;/th&gt;
      &lt;th&gt;Beds&lt;/th&gt;
      &lt;th&gt;Q.ty&lt;/th&gt;
    &lt;/tr&gt;
  &lt;thead&gt;
  &lt;tbody&gt;
    &lt;tr *ngFor=&quot;let room of roomsList&quot;&gt;
      &lt;td&gt;&lt;input [(ngModel)]=&quot;room.type&quot; name=&quot;type&quot; type=&quot;text&quot;&gt;&lt;/td&gt;
      &lt;td&gt;&lt;input [(ngModel)]=&quot;room.beds&quot; name=&quot;beds&quot; type=&quot;number&quot; class=&quot;smallField&quot;&gt;&lt;/td&gt;
      &lt;td&gt;&lt;input [(ngModel)]=&quot;room.qty&quot; name=&quot;qty&quot; type=&quot;number&quot; class=&quot;smallField&quot;&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;  
&lt;/table&gt;

&lt;button type=&quot;button&quot; (click)=&quot;onAddRoomType()&quot;&gt;Add Room Type&lt;/button&gt;
roomsList = [...HotelModel.rooms]; //makes a copy of this array into this new array

onAddRoomType() {
  this.roomsList.push({ type: &#39;&#39;, beds: 0, qty: 0 });
}

huangapple
  • 本文由 发表于 2023年5月11日 03:52:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76222137.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定