云朵爱打呼噜 發表於 2019-12-30 12:24:00

Angular ng-zorro table拖拽配置列宽

<p>一、技术框架及版本</p>
<ol>
<li>JS框架 Angular,版本:6.0.0</li>
<li>UI框架 ng-zorro,版本:1.8.1</li>
</ol>
<p>二、前提</p>
<ol>
<li>搭建Angular环境</li>
<li>在Angular项目中引入ng-zorro</li>
<li>创建一个自定义指令,用于监听鼠标事件</li>
<li>在要监听的table 单元格元素标签上使用自定义指令</li>
</ol>
<p>  下面用一个demo示例来说明:</p>
<p>三、代码</p>
<ul>
<li>在组件模板上使用ng-zorro table ,components.component.html
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)"> 1</span> <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">div </span><span style="color: rgba(255, 0, 0, 1)">nz-row nzGutter</span><span style="color: rgba(0, 0, 255, 1)">="0"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)"> 2</span>   <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">div </span><span style="color: rgba(255, 0, 0, 1)">nz-col nzSpan</span><span style="color: rgba(0, 0, 255, 1)">="20"</span><span style="color: rgba(255, 0, 0, 1)"> nzOffset</span><span style="color: rgba(0, 0, 255, 1)">="2"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)"> 3</span>       <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">nz-table   </span><span style="color: rgba(255, 0, 0, 1)">nzBordered #ajaxTable nzShowSizeChanger </span><span style="color: rgba(0, 0, 255, 1)">="false"</span><span style="color: rgba(255, 0, 0, 1)"> </span><span style="color: rgba(0, 0, 255, 1)">="tableDatas"</span><span style="color: rgba(255, 0, 0, 1)"> </span><span style="color: rgba(0, 0, 255, 1)">="loading"</span>
<span style="color: rgba(0, 128, 128, 1)"> 4</span> <span style="color: rgba(255, 0, 0, 1)">      </span><span style="color: rgba(0, 0, 255, 1)">="totalItems"</span><span style="color: rgba(255, 0, 0, 1)"> [(nzPageIndex)]</span><span style="color: rgba(0, 0, 255, 1)">="currentPage"</span><span style="color: rgba(255, 0, 0, 1)"> [(nzPageSize)]</span><span style="color: rgba(0, 0, 255, 1)">="itemsPerpage"</span><span style="color: rgba(255, 0, 0, 1)"> (nzPageIndexChange)</span><span style="color: rgba(0, 0, 255, 1)">="getData()"</span><span style="color: rgba(255, 0, 0, 1)"> (nzPageSizeChange)</span><span style="color: rgba(0, 0, 255, 1)">="getData()"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)"> 5</span>       <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">thead </span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)"> 6</span>         <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">tr</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)"> 7</span>         <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">th </span><span style="color: rgba(255, 0, 0, 1)">appDynamicColumnWidth</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>ID<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">th</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)"> 8</span>         <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">th </span><span style="color: rgba(255, 0, 0, 1)">appDynamicColumnWidth</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>Name<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">th</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)"> 9</span>         <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">th </span><span style="color: rgba(255, 0, 0, 1)">appDynamicColumnWidth</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>Gender<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">th</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">10</span>         <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">th </span><span style="color: rgba(255, 0, 0, 1)">appDynamicColumnWidth</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>Age<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">th</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">11</span>         <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">tr</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">12</span>       <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">thead</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">13</span>       <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">tbody </span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">14</span>         <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">tr </span><span style="color: rgba(255, 0, 0, 1)">*ngFor</span><span style="color: rgba(0, 0, 255, 1)">="let data of ajaxTable.data"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">15</span>         <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">td</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>{{data.id}}<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">td</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">16</span>         <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">td</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>{{data.name}}<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">td</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">17</span>         <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">td</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>{{data.gender}}<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">td</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">18</span>         <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">td</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>{{data.age}}<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">td</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">19</span>         <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">tr</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">20</span>       <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">tbody</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">21</span>   <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">nz-table</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">22</span>   <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">div</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">23</span>   <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">div </span><span style="color: rgba(255, 0, 0, 1)">nz-col nzSpan</span><span style="color: rgba(0, 0, 255, 1)">="2"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">24</span>
<span style="color: rgba(0, 128, 128, 1)">25</span>   <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">div</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">26</span> <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">div</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">27</span>
<span style="color: rgba(0, 128, 128, 1)">28</span> <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">br</span><span style="color: rgba(0, 0, 255, 1)">&gt;&lt;</span><span style="color: rgba(128, 0, 0, 1)">br</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span></pre>
</div>
<p>&nbsp;</p>
</li>
<li>组件ts文件 ,components.component.ts
<div class="cnblogs_code">
<pre>import { Component, OnInit } from '@angular/core'<span style="color: rgba(0, 0, 0, 1)">;
import { TableDataService } from </span>'../services/table--data.service'<span style="color: rgba(0, 0, 0, 1)">;

@Component({
selector: </span>'app-components'<span style="color: rgba(0, 0, 0, 1)">,
templateUrl: </span>'./components.component.html'<span style="color: rgba(0, 0, 0, 1)">,
styleUrls: [</span>'./components.component.css'<span style="color: rgba(0, 0, 0, 1)">]
})
export class ComponentsComponent implements OnInit {

public tableDatas: Array</span>&lt;Object&gt; =<span style="color: rgba(0, 0, 0, 1)"> [];

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 等待动画</span>
public loading: <span style="color: rgba(0, 0, 255, 1)">boolean</span> = <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 分页配置</span>
public currentPage: number = 1<span style="color: rgba(0, 0, 0, 1)">;
public itemsPerpage: number </span>= 5<span style="color: rgba(0, 0, 0, 1)">;
public totalItems: number </span>= 0<span style="color: rgba(0, 0, 0, 1)">;
constructor(
    private tableDataService:TableDataService
) {
   
}

ngOnInit() {
    </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.getData();
}


getData(){
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 从服务器查询表格数据</span>
    let result = <span style="color: rgba(0, 0, 255, 1)">this</span>.tableDataService.getTableData(<span style="color: rgba(0, 0, 255, 1)">this</span>.currentPage,<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.itemsPerpage);
    console.log(</span>"result:"<span style="color: rgba(0, 0, 0, 1)">,result);
    </span><span style="color: rgba(0, 0, 255, 1)">this</span>.totalItems = result["totalCounts"<span style="color: rgba(0, 0, 0, 1)">];
    </span><span style="color: rgba(0, 0, 255, 1)">this</span>.tableDatas = result["list"<span style="color: rgba(0, 0, 0, 1)">];
   
}
}</span></pre>
</div>
<p>&nbsp;</p>
</li>
<li>封装模拟请求服务器的接口服务,table-data.service.ts&nbsp;
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)"> 1</span> import { Injectable } from '@angular/core'<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)"> 2</span>
<span style="color: rgba(0, 128, 128, 1)"> 3</span> <span style="color: rgba(0, 0, 0, 1)">@Injectable({
</span><span style="color: rgba(0, 128, 128, 1)"> 4</span>   providedIn: 'root'
<span style="color: rgba(0, 128, 128, 1)"> 5</span> <span style="color: rgba(0, 0, 0, 1)">})
</span><span style="color: rgba(0, 128, 128, 1)"> 6</span> <span style="color: rgba(0, 0, 0, 1)">export class TableDataService {
</span><span style="color: rgba(0, 128, 128, 1)"> 7</span>
<span style="color: rgba(0, 128, 128, 1)"> 8</span> <span style="color: rgba(0, 0, 0, 1)">constructor() { }
</span><span style="color: rgba(0, 128, 128, 1)"> 9</span>
<span style="color: rgba(0, 128, 128, 1)">10</span> <span style="color: rgba(0, 0, 0, 1)">getTableData(currentPage: number, itemsPerpage: number): Object {
</span><span style="color: rgba(0, 128, 128, 1)">11</span>   let totalDatas =<span style="color: rgba(0, 0, 0, 1)"> [
</span><span style="color: rgba(0, 128, 128, 1)">12</span>       { id: 0, name: "张三", gender: "男", age: 23<span style="color: rgba(0, 0, 0, 1)"> },
</span><span style="color: rgba(0, 128, 128, 1)">13</span>       { id: 1, name: "李四", gender: "男", age: 25<span style="color: rgba(0, 0, 0, 1)"> },
</span><span style="color: rgba(0, 128, 128, 1)">14</span>       { id: 2, name: "王五", gender: "男", age: 28<span style="color: rgba(0, 0, 0, 1)"> },
</span><span style="color: rgba(0, 128, 128, 1)">15</span>       { id: 3, name: "赵六", gender: "男", age: 22<span style="color: rgba(0, 0, 0, 1)"> },
</span><span style="color: rgba(0, 128, 128, 1)">16</span>       { id: 4, name: "马七", gender: "男", age: 29<span style="color: rgba(0, 0, 0, 1)"> },
</span><span style="color: rgba(0, 128, 128, 1)">17</span>       { id: 5, name: "锅巴", gender: "男", age: 30<span style="color: rgba(0, 0, 0, 1)"> },
</span><span style="color: rgba(0, 128, 128, 1)">18</span>       { id: 6, name: "关羽", gender: "男", age: 56<span style="color: rgba(0, 0, 0, 1)"> },
</span><span style="color: rgba(0, 128, 128, 1)">19</span>       { id: 7, name: "张飞", gender: "男", age: 28<span style="color: rgba(0, 0, 0, 1)"> },
</span><span style="color: rgba(0, 128, 128, 1)">20</span>       { id: 8, name: "刘备", gender: "男", age: 26<span style="color: rgba(0, 0, 0, 1)"> },
</span><span style="color: rgba(0, 128, 128, 1)">21</span>       { id: 9, name: "王启年", gender: "男", age: 23<span style="color: rgba(0, 0, 0, 1)"> },
</span><span style="color: rgba(0, 128, 128, 1)">22</span>       { id: 10, name: "郭麒麟", gender: "男", age: 28<span style="color: rgba(0, 0, 0, 1)"> },
</span><span style="color: rgba(0, 128, 128, 1)">23</span>       { id: 11, name: "陈萍萍", gender: "男", age: 45<span style="color: rgba(0, 0, 0, 1)"> },
</span><span style="color: rgba(0, 128, 128, 1)">24</span>       { id: 12, name: "影子", gender: "男", age: 67<span style="color: rgba(0, 0, 0, 1)"> },
</span><span style="color: rgba(0, 128, 128, 1)">25</span>       { id: 13, name: "高达", gender: "男", age: 89<span style="color: rgba(0, 0, 0, 1)"> },
</span><span style="color: rgba(0, 128, 128, 1)">26</span>       { id: 14, name: "上山虎", gender: "男", age: 24<span style="color: rgba(0, 0, 0, 1)"> },
</span><span style="color: rgba(0, 128, 128, 1)">27</span>       { id: 15, name: "海棠朵朵", gender: "男", age: 18<span style="color: rgba(0, 0, 0, 1)"> },
</span><span style="color: rgba(0, 128, 128, 1)">28</span> <span style="color: rgba(0, 0, 0, 1)">    ]
</span><span style="color: rgba(0, 128, 128, 1)">29</span>
<span style="color: rgba(0, 128, 128, 1)">30</span>
<span style="color: rgba(0, 128, 128, 1)">31</span>   let result =<span style="color: rgba(0, 0, 0, 1)"> [];
</span><span style="color: rgba(0, 128, 128, 1)">32</span>   let startNumber = (currentPage - 1) *<span style="color: rgba(0, 0, 0, 1)"> itemsPerpage;
</span><span style="color: rgba(0, 128, 128, 1)">33</span>   let endNumber = (currentPage) *<span style="color: rgba(0, 0, 0, 1)"> itemsPerpage;
</span><span style="color: rgba(0, 128, 128, 1)">34</span>
<span style="color: rgba(0, 128, 128, 1)">35</span>   totalDatas.map((item, index) =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 128, 128, 1)">36</span>       <span style="color: rgba(0, 0, 255, 1)">if</span> ((index &gt;= startNumber) &amp;&amp; (index &lt;<span style="color: rgba(0, 0, 0, 1)"> endNumber)) {
</span><span style="color: rgba(0, 128, 128, 1)">37</span> <span style="color: rgba(0, 0, 0, 1)">      result.push(item);
</span><span style="color: rgba(0, 128, 128, 1)">38</span>       } <span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 128, 128, 1)">39</span>         <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Do-nothing</span>
<span style="color: rgba(0, 128, 128, 1)">40</span> <span style="color: rgba(0, 0, 0, 1)">      }
</span><span style="color: rgba(0, 128, 128, 1)">41</span> <span style="color: rgba(0, 0, 0, 1)">    })
</span><span style="color: rgba(0, 128, 128, 1)">42</span>
<span style="color: rgba(0, 128, 128, 1)">43</span>
<span style="color: rgba(0, 128, 128, 1)">44</span>   <span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 128, 128, 1)">45</span> <span style="color: rgba(0, 0, 0, 1)">      currentPage: currentPage,
</span><span style="color: rgba(0, 128, 128, 1)">46</span> <span style="color: rgba(0, 0, 0, 1)">      itemsPerpage: itemsPerpage,
</span><span style="color: rgba(0, 128, 128, 1)">47</span> <span style="color: rgba(0, 0, 0, 1)">      totalCounts: totalDatas.length,
</span><span style="color: rgba(0, 128, 128, 1)">48</span> <span style="color: rgba(0, 0, 0, 1)">      list: result,
</span><span style="color: rgba(0, 128, 128, 1)">49</span> <span style="color: rgba(0, 0, 0, 1)">    }
</span><span style="color: rgba(0, 128, 128, 1)">50</span> <span style="color: rgba(0, 0, 0, 1)">}
</span><span style="color: rgba(0, 128, 128, 1)">51</span> }</pre>
</div>
<p>&nbsp;</p>
</li>
<li>自定义指令,dynamic-column-width.directivej.ts&nbsp;
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)"> 1</span> import { Directive, ElementRef, HostListener } from '@angular/core'<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)"> 2</span>
<span style="color: rgba(0, 128, 128, 1)"> 3</span> <span style="color: rgba(0, 0, 0, 1)">@Directive({
</span><span style="color: rgba(0, 128, 128, 1)"> 4</span>   selector: ''
<span style="color: rgba(0, 128, 128, 1)"> 5</span> <span style="color: rgba(0, 0, 0, 1)">})
</span><span style="color: rgba(0, 128, 128, 1)"> 6</span> <span style="color: rgba(0, 0, 0, 1)">export class DynamicColumnWidthDirective {
</span><span style="color: rgba(0, 128, 128, 1)"> 7</span>   public elem = <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)"> 8</span>   public mouseDown: <span style="color: rgba(0, 0, 255, 1)">boolean</span> = <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)"> 9</span>   public oldX: number = 0<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">10</span>   public oldWidth: number = 0<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">11</span>   public width: number = 0<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">12</span>
<span style="color: rgba(0, 128, 128, 1)">13</span> <span style="color: rgba(0, 0, 0, 1)">constructor(private el: ElementRef) {
</span><span style="color: rgba(0, 128, 128, 1)">14</span>   
<span style="color: rgba(0, 128, 128, 1)">15</span> <span style="color: rgba(0, 0, 0, 1)">}
</span><span style="color: rgba(0, 128, 128, 1)">16</span>
<span style="color: rgba(0, 128, 128, 1)">17</span>   ngOnInit(): <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 128, 128, 1)">18</span>   
<span style="color: rgba(0, 128, 128, 1)">19</span> <span style="color: rgba(0, 0, 0, 1)">}
</span><span style="color: rgba(0, 128, 128, 1)">20</span>
<span style="color: rgba(0, 128, 128, 1)">21</span>   @HostListener('mousedown', ['$event'<span style="color: rgba(0, 0, 0, 1)">])
</span><span style="color: rgba(0, 128, 128, 1)">22</span> <span style="color: rgba(0, 0, 0, 1)">onMousedown(e) {
</span><span style="color: rgba(0, 128, 128, 1)">23</span>   <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 判断当前点击的是否是单元格元素</span>
<span style="color: rgba(0, 128, 128, 1)">24</span>   <span style="color: rgba(0, 0, 255, 1)">if</span> (<span style="color: rgba(0, 0, 255, 1)">this</span>.el != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) {
</span><span style="color: rgba(0, 128, 128, 1)">25</span>       <span style="color: rgba(0, 0, 255, 1)">if</span> (e.offsetX &gt; <span style="color: rgba(0, 0, 255, 1)">this</span>.el.nativeElement.offsetWidth - 15<span style="color: rgba(0, 0, 0, 1)">) {
</span><span style="color: rgba(0, 128, 128, 1)">26</span>         <span style="color: rgba(0, 0, 255, 1)">this</span>.el.nativeElement.mouseDown = <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">27</span>         <span style="color: rgba(0, 0, 255, 1)">this</span>.el.nativeElement.oldX =<span style="color: rgba(0, 0, 0, 1)"> e.x;
</span><span style="color: rgba(0, 128, 128, 1)">28</span>         <span style="color: rgba(0, 0, 255, 1)">this</span>.el.nativeElement.oldWidth = <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.el.nativeElement.offsetWidth;
</span><span style="color: rgba(0, 128, 128, 1)">29</span> <span style="color: rgba(0, 0, 0, 1)">      }
</span><span style="color: rgba(0, 128, 128, 1)">30</span>   } <span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 128, 128, 1)">31</span>       <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Do-nothing</span>
<span style="color: rgba(0, 128, 128, 1)">32</span> <span style="color: rgba(0, 0, 0, 1)">    }
</span><span style="color: rgba(0, 128, 128, 1)">33</span> <span style="color: rgba(0, 0, 0, 1)">}
</span><span style="color: rgba(0, 128, 128, 1)">34</span>
<span style="color: rgba(0, 128, 128, 1)">35</span>
<span style="color: rgba(0, 128, 128, 1)">36</span>
<span style="color: rgba(0, 128, 128, 1)">37</span>   @HostListener('mouseup', ['$event'<span style="color: rgba(0, 0, 0, 1)">])
</span><span style="color: rgba(0, 128, 128, 1)">38</span> <span style="color: rgba(0, 0, 0, 1)">onMouseup(e) {
</span><span style="color: rgba(0, 128, 128, 1)">39</span>   <span style="color: rgba(0, 0, 255, 1)">if</span> (<span style="color: rgba(0, 0, 255, 1)">this</span>.el != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) {
</span><span style="color: rgba(0, 128, 128, 1)">40</span>       <span style="color: rgba(0, 0, 255, 1)">this</span>.el.nativeElement.mouseDown = <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">41</span>       <span style="color: rgba(0, 0, 255, 1)">this</span>.el.nativeElement.style.cursor = 'default'<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">42</span> <span style="color: rgba(0, 0, 0, 1)">    }
</span><span style="color: rgba(0, 128, 128, 1)">43</span> <span style="color: rgba(0, 0, 0, 1)">}
</span><span style="color: rgba(0, 128, 128, 1)">44</span>
<span style="color: rgba(0, 128, 128, 1)">45</span>
<span style="color: rgba(0, 128, 128, 1)">46</span>
<span style="color: rgba(0, 128, 128, 1)">47</span>   @HostListener('mousemove', ['$event'<span style="color: rgba(0, 0, 0, 1)">])
</span><span style="color: rgba(0, 128, 128, 1)">48</span> <span style="color: rgba(0, 0, 0, 1)">onMousemove(e) {
</span><span style="color: rgba(0, 128, 128, 1)">49</span>   <span style="color: rgba(0, 0, 255, 1)">if</span>(<span style="color: rgba(0, 0, 255, 1)">this</span>.el != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">){
</span><span style="color: rgba(0, 128, 128, 1)">50</span>       <span style="color: rgba(0, 0, 255, 1)">if</span> (e.offsetX &gt; <span style="color: rgba(0, 0, 255, 1)">this</span>.el.nativeElement.offsetWidth - 15<span style="color: rgba(0, 0, 0, 1)">) {
</span><span style="color: rgba(0, 128, 128, 1)">51</span>         <span style="color: rgba(0, 0, 255, 1)">this</span>.el.nativeElement.style.cursor = 'col-resize'<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">52</span>       } <span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 128, 128, 1)">53</span>         <span style="color: rgba(0, 0, 255, 1)">this</span>.el.nativeElement.style.cursor = 'default'<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">54</span> <span style="color: rgba(0, 0, 0, 1)">      }
</span><span style="color: rgba(0, 128, 128, 1)">55</span> <span style="color: rgba(0, 0, 0, 1)">    }
</span><span style="color: rgba(0, 128, 128, 1)">56</span>
<span style="color: rgba(0, 128, 128, 1)">57</span>   <span style="color: rgba(0, 0, 255, 1)">if</span> (<span style="color: rgba(0, 0, 255, 1)">this</span>.el.nativeElement.mouseDown != <span style="color: rgba(0, 0, 255, 1)">null</span> &amp;&amp; <span style="color: rgba(0, 0, 255, 1)">this</span>.el.nativeElement.mouseDown == <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">) {
</span><span style="color: rgba(0, 128, 128, 1)">58</span>       <span style="color: rgba(0, 0, 255, 1)">this</span>.el.nativeElement.style.cursor = 'default'<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">59</span>       let wid = 0<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">60</span>       <span style="color: rgba(0, 0, 255, 1)">if</span> (<span style="color: rgba(0, 0, 255, 1)">this</span>.el.nativeElement.oldWidth + (e.x - <span style="color: rgba(0, 0, 255, 1)">this</span>.el.nativeElement.oldX) &gt; 0<span style="color: rgba(0, 0, 0, 1)">) {
</span><span style="color: rgba(0, 128, 128, 1)">61</span>         wid = <span style="color: rgba(0, 0, 255, 1)">this</span>.el.nativeElement.oldWidth + (e.x - <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.el.nativeElement.oldX);
</span><span style="color: rgba(0, 128, 128, 1)">62</span> <span style="color: rgba(0, 0, 0, 1)">      }
</span><span style="color: rgba(0, 128, 128, 1)">63</span>       <span style="color: rgba(0, 0, 255, 1)">this</span>.el.nativeElement.style.width = wid + 'px'<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">64</span>       <span style="color: rgba(0, 0, 255, 1)">this</span>.el.nativeElement.style.cursor = 'col-resize'<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">65</span>       <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> ng-zorro table 列宽可以根据thead每个单元格的宽度自动适配,所以不用再做修改</span>
<span style="color: rgba(0, 128, 128, 1)">66</span>   } <span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 128, 128, 1)">67</span>       <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> console.log("无所谓!");</span>
<span style="color: rgba(0, 128, 128, 1)">68</span> <span style="color: rgba(0, 0, 0, 1)">    }
</span><span style="color: rgba(0, 128, 128, 1)">69</span> <span style="color: rgba(0, 0, 0, 1)">}
</span><span style="color: rgba(0, 128, 128, 1)">70</span> }</pre>
</div>
<p>&nbsp;</p>
</li>
<li><strong><strong>在模块中依赖注入自定义指令,components.module.ts&nbsp;</strong></strong>
<div class="cnblogs_code">
<pre>import { NgModule } from '@angular/core'<span style="color: rgba(0, 0, 0, 1)">;
import { CommonModule } from </span>'@angular/common'<span style="color: rgba(0, 0, 0, 1)">;

import { ComponentsRoutingModule } from </span>'./components-routing.module'<span style="color: rgba(0, 0, 0, 1)">;
import { ComponentsComponent } from </span>'./components.component'<span style="color: rgba(0, 0, 0, 1)">;
import { NgZorroAntdModule } from </span>'ng-zorro-antd'<span style="color: rgba(0, 0, 0, 1)">;
import { DynamicColumnWidthDirective } from </span>'../directives/dynamic-column-width.directive'<span style="color: rgba(0, 0, 0, 1)">;

@NgModule({
imports: [
    CommonModule,
    ComponentsRoutingModule,
    NgZorroAntdModule.forRoot(),
],
declarations:
})
export class ComponentsModule { }</span></pre>
</div>
<p><strong>注意:</strong></p>
</li>
<li><strong>自定义指令需要在待使用组件所在模块module文件中引入,如上。</strong></li>
<li><strong>如果存在多个组件都需要使用该自定义指令,请将新建一个公共模块,在公共模块中依赖注入自定义指令,在待使用组件中依赖注入公共模块。</strong></li>
</ul>
<p><strong>四、运行截图</strong></p>
<p><strong><img src="https://img2018.cnblogs.com/i-beta/1477871/201912/1477871-20191230114714777-374642260.png"></strong></p>
<p>&nbsp;</p>
<p>五、源码</p>
<p>  源码请参考&nbsp;https://github.com/guojije/dynamicColumn</p><br><br>
来源:https://www.cnblogs.com/mycnblogs-guoguo/p/12119111.html
頁: [1]
查看完整版本: Angular ng-zorro table拖拽配置列宽