Angular中添加、删除或编辑后Datatable未更新
创始人
2024-10-31 19:00:36
0

在Angular中更新Datatable需要手动调用Datatable的API方法进行刷新。可以在组件中使用ViewChild来获取Datatable的实例,然后使用addRow、removeRow或manageRow方法来添加、删除或编辑行,最后调用Datatable的draw方法来重新渲染。

示例代码如下:

HTML模板文件:

ID Name Age Actions
{{item.id}} {{item.name}} {{item.age}}

组件代码:

import { Component, ViewChild } from '@angular/core';
import * as $ from 'jquery';
import 'datatables.net';

@Component({
  selector: 'app-datatable',
  templateUrl: './datatable.component.html',
  styleUrls: ['./datatable.component.css']
})
export class DatatableComponent {
  @ViewChild('myTable') table;

  items = [
    { id: 1, name: 'John', age: 28 },
    { id: 2, name: 'Peter', age: 35 },
    { id: 3, name: 'Mary', age: 31 }
  ];

  ngAfterViewInit() {
    $(this.table.nativeElement).DataTable();
  }

  edit(item) {
    // Code to edit the item goes here.
    this.table.nativeElement.api().row("#" + item.id).data(item);
    this.table.nativeElement.api().draw();
  }

  delete(item) {
    // Code to delete the item goes here.
    this.table.nativeElement.api().row("#" + item.id).remove().draw();
  }

  add(item) {
    // Code to add the item goes here.
    this.table.nativeElement.api().row.add(item).draw();
  }
}

相关内容

热门资讯

揭露透视!wepoker免费透... 揭露透视!wepoker免费透视,微乐小程序游戏破解器,演示教程(有挂详细)-哔哩哔哩1、超多福利:...
教你透视!哈糖大菠萝挂,微信小... 教你透视!哈糖大菠萝挂,微信小程序辅助多少钱,绝活儿教程(真的有挂)-哔哩哔哩1、每一步都需要思考,...
辅助透视!hhpoker必备开... 辅助透视!hhpoker必备开挂,微乐自建房安装挂免费,教程书教程(有挂详细)-哔哩哔哩小薇(辅助器...
揭露透视!wepoker辅助器... 揭露透视!wepoker辅助器安装包,微乐麻辣自建房辅助视频,手段教程(有挂解惑)-哔哩哔哩1、起透...
详情透视!wepoker透视挂... 详情透视!wepoker透视挂底牌,小程序跑得快的技巧,指南书教程(有挂攻略)-哔哩哔哩1、玩家可以...
解谜透视!wpk辅助哪里买,微... 解谜透视!wpk辅助哪里买,微乐小程序辅助器代理,窍要教程(存在有挂)-哔哩哔哩1、玩家可以在wpk...
解迷透视!aapoker破解侠... 解迷透视!aapoker破解侠是真的吗,微乐河北自建房脚本免费入口,练习教程(有挂秘籍)-哔哩哔哩1...
教你透视!德州透视竞技联盟,微... 教你透视!德州透视竞技联盟,微乐小程序礼包码,步骤教程(有挂实锤)-哔哩哔哩一、德州透视竞技联盟可以...
必备透视!aapoker脚本怎... 必备透视!aapoker脚本怎么用,微乐自建房辅助工具2025在哪,秘籍教程(有挂工具)-哔哩哔哩1...
分享透视!德州透视插件,微乐自... 分享透视!德州透视插件,微乐自建房免费黑科技推荐,妙计教程(果真有挂)-哔哩哔哩1、不需要AI权限,...