简体   繁体   English

如何访问Primeng表的style属性

[英]How to access the style attribute of Primeng table

In an angular project, I need to test that the displayed table width of the primeng data table is set to the maxWidth value i assign to it. 在角度项目中,我需要测试primeng数据表的显示表宽度是否设置为我赋给它的maxWidth值。 To do so, i want to call the [style] attribute to get the width and see if its equal to my maxWidth. 为此,我想调用[style]属性来获取宽度,看它是否等于我的maxWidth。 However, i do not know how to call attributes like this. 但是,我不知道如何调用这样的属性。 How do i go about this? 我该怎么做? Currently i have no clue if I'm going in the correct direction. 目前我不知道如果我朝着正确的方向前进。

I have tried several things but I am not sure of the syntax for it. 我尝试了几件事,但我不确定它的语法。

<p-table class="p-table" ... [style] = "{width: maxWidth}" >

it('should implement maxwidth', () => {
    const widthDebug: DebugElement = fixture.debugElement;
    const tableWidth = widthDebug.query(By.css('.ui-table .ui-widget'));
    const ptable: HTMLElement = tableWidth.nativeElement;

    expect(ptable.textContent).toContain("width: " + component.maxWidth);

});

expected: success (ptable.textContent contains "width: component.maxWidth") actual: TypeError: cannot read property 'nativeElement' of null expected:success(ptable.textContent包含“width:component.maxWidth”)实际:TypeError:无法读取属性“nativeElement”的null

I see that it's now two months after you asked your question, so it's probably too late for my answer to help, but I stumbled across this post while looking up something else about PrimeNG, so I might as well give it a shot. 我看到现在问你问题已经两个月了,所以我的回答可能为时已晚,但我在查找有关PrimeNG的其他内容时偶然发现了这篇文章,所以我不妨试一试。

The problem here is that nativeElement is defined on Dialog class instances of the Angular p-table component. 这里的问题是nativeElement是在Angular p-table组件的Dialog类实例上定义的。 It's not defined on any particular DOM element. 它没有在任何特定的DOM元素上定义。

By.css('.ui-table .ui-widget') is going to find a DOM element for you, not an Angular class instance. By.css('.ui-table .ui-widget')将为您找到一个DOM元素,而不是一个Angular类实例。 In particular what will be found is a <div> inside the <p-dialog> DOM element, and it's this <div> that receives the style set via [style]=... . 特别是在<p-dialog> DOM元素中可以找到的是<div> ,而这个<div>通过[style]=...接收样式集。

As your code is written above tableWidth.style.width would contain (as a string) the value of maxWidth that you're expecting to find. 当你的代码写在tableWidth.style.width之上tableWidth.style.width它将包含(作为一个字符串)你期望找到的maxWidth的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM