【问题标题】:Return angular datatable on angular datatable rowCallback function在角度数据表rowCallback函数上返回角度数据表
【发布时间】:2018-01-11 07:23:14
【问题描述】:

我有一个带有嵌套数据的angular datatable,我正在尝试在行点击函数上创建另一个数据表。rowCallBack 父数据表的函数。

这是我的外部数据表 html;

<table id="tblEmailsByCase" datatable="" dt-options="dtCaseOptions" dt-columns="dtCaseColumns" dt-instance="dtCaseInstance" class="display table table-striped table-bordered table-hover">
    <!-- THIS TABLE IS GENERATED DYNAMICALLY -->
</table>

这是我生成数据表的方式;

    // adding btn column to list

    if($scope.lstEmailsByCases[i].users != null && $scope.lstEmailsByCases[i].users.length > 0 )
    {
        $scope.lstEmailsByCases[i].btn = '<button id="' + 'btn' + i + '">+</button>';
    }

// creating table's column..

    $scope.dtCaseInstance = {};
    $scope.dtCaseOptions = DTOptionsBuilder.fromSource($scope.lstEmailsByCases)
        .withOption('data', $scope.lstEmailsByCases) //pass data source here
        .withOption('dataSrc', '')
        .withOption('rowCallback', rowCallbackCases)
        .withOption('createdRow', createdRowCases)

    //define columns
    $scope.dtCaseColumns = [
        DTColumnBuilder.newColumn('btn').withTitle(''), //this is to show the '+' button
        DTColumnBuilder.newColumn('caseId').withTitle('Case Id'),


    ];

//CALLED WHEN ROW IS CREATED
function createdRowCases(row, data, dataIndex) {
    // Recompiling so we can bind Angular directive
    $compile(angular.element(row).contents())($scope);
}

//HERE IT IS CALLED WHENEVER ROW IS CLICKED
function rowCallbackCases(tabRow, data, dataIndex) {
    if(tabRow.cells[0].firstChild != null)
        {
            $(tabRow).unbind('click');
            $(tabRow).on('click', function() {

                $(this).find('.a1-icon').toggleClass('fa-rotate-180');
                var tr = $(tabRow);
                var table = $scope.dtCaseInstance.DataTable;
                var row = table.row(tr);

                if (row.child.isShown()) {
                    // This row is already open - close it
                    row.child.hide();
                    tr.removeClass('shown');
                    tabRow.cells[0].firstChild.innerHTML = "+"; //change button value to +
                } else if (typeof row.child(formatCaseUsersTable(row.data())) != "undefined") {
                    // Open this row
                    row.child(formatCaseUsersTable(row.data())).show();
                    tr.addClass('shown');
                    tabRow.cells[0].firstChild.innerHTML = "-"; //change button value to -
                }

            });
        }
}

它工作得很好,并生成这样的数据表;

现在我正在尝试在 + 按钮单击时生成另一个数据表。所以这就是我在 formatCaseUsersTable 函数中所做的事情,该函数是从该数据表的 rowCallBack 函数调用的。这是该函数;

//CALLED TO CREATE SUB GRID
function formatCaseUsersTable(d) {

    //if detail does not exist
    if (typeof d.users == "undefined") return;

    //defining table
    var html2 = '<table id="tblCaseUsers" datatable="" dt-options="dtCaseUsersOptions" dt-columns="dtCaseUsersColumns" dt-instance="dtCaseUsersInstance" class="display table table-striped table-bordered table-hover">';

    $scope.dtCaseUsersInstance = {};
    $scope.dtCaseUsersOptions = DTOptionsBuilder.fromSource(d.users)
        .withOption('data', d.users) //pass data source here
        .withOption('dataSrc', '')
        .withOption('rowCallback', rowCallbackCaseUsers)
        .withOption('createdRow', createdRowCaseUsers)

    //define columns
    $scope.dtCaseUsersColumns = [
        DTColumnBuilder.newColumn('btn').withTitle(''), //this is to show the '+' button
        DTColumnBuilder.newColumn('userId').withTitle('User Id'),
        DTColumnBuilder.newColumn('userName').withTitle('User Name'),


    ];

  //add button in each row.. this button will be used to show user that the row is expandable
    for (i = 0; i < d.users.length; i++) {
        if(d.users[i].emailsDetail != null && d.users[i].emailsDetail.length > 0 )
        {
            d.users[i].btn = '<button id="' + 'btn' + i + '">+</button>';
        }
    }

    return html2;
}

但它没有相应地工作..这是在+按钮单击时创建的html..

当我检查生成的 html 时,表定义是这样的;

但它没有显示在网格中,也没有任何列详细信息。

有人知道我错过了什么吗?

【问题讨论】:

  • 我不喜欢在您的示例中将 AngularJS 与 jQuery 混合在一起的想法,但看起来您只是在您的 formatCaseUsersTable 方法中返回 html2 字符串而不编译它,所以angularjs 可以将datatable 指令附加到它。 Can you try returning something like return $compile(angular.element(html2))($scope); and see if it helps?
  • 您尝试过建议的解决方案吗?
  • @StanislavKvitash 抱歉回复晚了...有点忙..
  • 哦,伙计...这确实很神奇.. return $compile(angular.element(html2))($scope); 谢谢..让它回答所以我可以标记一下...
  • 对你有帮助,添加为答案:)

标签: javascript angularjs datatable


【解决方案1】:

正如我们从您的代码中看到的 - formatCaseUsersTable 方法只是将 html2 作为字符串返回,没有 compiling it,因此 AngularJS 可以将 datatable directive 附加到 table element。您必须使用 $compile service 将字符串编译为模板,所以这应该适用于您的情况:

function formatCaseUsersTable(d) {
    //..

    //defining table
    var html2 = '<table id="tblCaseUsers" datatable="" dt-options="dtCaseUsersOptions" dt-columns="dtCaseUsersColumns" dt-instance="dtCaseUsersInstance" class="display table table-striped table-bordered table-hover">';
    //....

    return $compile(angular.element(html2))($scope);
} 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-21
    • 1970-01-01
    • 2017-11-27
    • 2016-01-22
    • 2018-05-13
    相关资源
    最近更新 更多