【问题标题】:How to bind data using angular js and datatable with extra row and column如何使用 Angular js 和带有额外行和列的数据表绑定数据
【发布时间】:2017-04-01 05:47:44
【问题描述】:

您好,我正在使用 angularjs 和带有数据表 js 的 ASP.NET MVC 创建一个应用程序。

this 文章的帮助下,我已经使用带有角度 js 的数据表实现了表格显示数据。

但我想使用与列名相同的功能在 html 中静态绑定数据,例如:

在文章中作者使用:

<table id="entry-grid" datatable="" dt-options="dtOptions" 
       dt-columns="dtColumns" class="table table-hover">
</table>

但我想通过使用 ng-repeat 根据我的数据使用上述相同的功能来做到这一点:

<table id="tblusers" class="table table-bordered table-striped table-condensed datatable">
  <thead>
    <tr>
      <th width="2%"></th>
      <th>User Name</th>
      <th>Email</th>
      <th>LoginID</th>
      <th>Location Name</th>
      <th>Role</th>
      <th width="7%" class="center-text">Active</th>
    </tr>
  </thead>
  <tbody>
    <tr ng-repeat="user in Users">
      <td><a href="#" ng-click="DeleteUser(user)"><span class="icon-trash"></span></a></td>
      <td><a class="ahyperlink" href="#" ng-click="EditUser(user)">{{user.UserFirstName}} {{user.UserLastName}}</a></td>
      <td>{{user.UserEmail}}</td>
      <td>{{user.LoginID}}</td>
      <td>{{user.LocationName}}</td>
      <td>{{user.RoleName}}</td>
      <td class="center-text" ng-if="user.IsActive == true"><span class="icon-check2"></span></td>
      <td class="center-text" ng-if="user.IsActive == false"><span class="icon-close"></span></td>
    </tr>
  </tbody>
</table>

我还想在表中添加新列,使用按钮单击添加新记录时的相同功能。

有可能吗?

如果是的话,如果有人在 jsfiddle 或任何编辑器中向我展示,那将是很好的,并提前感谢。

DOWNLOAD 在 Visual Studio 编辑器中创建的源代码用于演示

【问题讨论】:

  • 您能否添加控制器和指令代码以了解您的代码
  • @SamuelJMathew 您可以在此article 中看到的所有代码除了我在问题中编写的代码以在 HTML 中静态绑定作为示例之外,我有相同的代码没有变化。

标签: angularjs datatables angular-datatables


【解决方案1】:

您可以在评论中关注davidkonrad的answer,就像下面的结构一样:

HTML:

<table id="entry-grid" datatable="ng" class="table table-hover">
            <thead>
                <tr>
                    <th>
                        CustomerId
                    </th>
                    <th>Company Name </th>
                    <th>Contact Name</th>
                    <th>
                        Phone
                    </th>
                    <th>
                        City
                    </th>
                </tr>
            </thead>
            <tbody>
                <tr ng-repeat="c in Customers">
                    <td>{{c.CustomerID}}</td>
                    <td>{{c.CompanyName}}</td>
                    <td>{{c.ContactName}}</td>
                    <td>{{c.Phone}}</td>\
                    <td>{{c.City}}</td>
                </tr>
            </tbody>
        </table>

像这样以角度创建控制器:

var app = angular.module('MyApp1', ['datatables']);
app.controller('homeCtrl', ['$scope', 'HomeService',
    function ($scope, homeService) {

        $scope.GetCustomers = function () {
            homeService.GetCustomers()
                .then(
                function (response) {
                    debugger;
                    $scope.Customers = response.data;
                });
        }

        $scope.GetCustomers();
    }])

服务:

app.service('HomeService', ["$http", "$q", function ($http, $q) {

    this.GetCustomers = function () {
        debugger;
        var request = $http({
            method: "Get",
            url: "/home/getdata"
        });
        return request;
    }
}]);

【讨论】:

  • 哦,很高兴再次见到您,先生,让我看看它是否有效!
  • 是的,它按预期工作,非常棒,先生,只有一个问题它显示了两个用于排序的图标,并且存在一些设计问题,但没关系,我会找到它。一如既往地感谢您再次帮助我,先生,非常感谢您,也感谢 davidkonrad。
  • 不客气,亲爱的,davidkonrad 的回答也是可以接受的,没有详细检查,但应该可以。
  • 先生需要一点帮助,您能告诉我如何设置第一个或任何不可排序的列吗?
  • 使用此link 满足您的要求
【解决方案2】:

datatable="ng" 指示 angular-dataTables 使用“角度方式”:

<table id="entry-grid" 
   datatable="ng" 
   dt-options="dtOptions" 
   dt-columns="dtColumns" 
   class="table table-hover">
</table> 

然后将 dtColumns 更改为地址列索引而不是 JSON 条目:

$scope.dtColumns = [
   DTColumnBuilder.newColumn(0).withTitle('').withOption('width', '2%'),
   DTColumnBuilder.newColumn(1).withTitle('User Name'),
   DTColumnBuilder.newColumn(2).withTitle('Email'),
   DTColumnBuilder.newColumn(3).withTitle('LoginID'),
   DTColumnBuilder.newColumn(4).withTitle('Location Name'),
   DTColumnBuilder.newColumn(5).withTitle('Role Name'),
   DTColumnBuilder.newColumn(6).withTitle('Active').withOption('width', '7%') 
];

如果您按照上述操作,您可以完全跳过&lt;thead&gt; 部分。最后,我会将最后两个多余的 &lt;td&gt; 减少到一个:

<td class="center-text">
  <span ng-show="user.IsActive == true" class="icon-check2"></span>
  <span ng-show="user.IsActive == false" class="icon-close"></span>
</td>

【讨论】:

  • 我已经尝试过您的代码,但在控制台中出现错误“无法读取未定义的属性'匹配'”,请帮我一个忙,先生下载此code,您可以在最后进行更改吗?看看它是否有效,先生提前谢谢。
  • @padhiyar,很高兴看到一个带有 C# 后端的 Angular 项目 :) 请删除带有链接的评论(为了您自己)。你的问题是这个 -> $scope.dtOptions = DTOptionsBuilder.newOptions().withOption('ajax', { url: "/home/getdata", type: "POST" }) 你不能有 ajax 现在你用 angular ng-repeat 渲染!
  • 使用dtOptions = DTOptionsBuilder.newOptions().withPaginationType('full_numbers')等,即完全跳过withOption('ajax'),我相信它会起作用。
  • 先生,我同意你的观点,但在我的实际应用程序中,我必须调用服务并使用 $http 调用强制获取数据。那么我怎么能通过没有 ajax 先生的调用来做到这一点呢?
  • 先生,您的意思是说我不必更改我用 HTML 编写的表格代码,对吗?我已经尝试过,但是数据表搜索和排序功能无法正常显示数据,但是当我搜索某些内容或单击对标题的数据进行排序时,它会将表内的数据设置为空白。
猜你喜欢
  • 2018-05-27
  • 2015-08-10
  • 2011-10-01
  • 1970-01-01
  • 2019-07-23
  • 1970-01-01
  • 2022-11-04
  • 2016-02-24
  • 1970-01-01
相关资源
最近更新 更多