如何在jquery中传递html数据表的行添加功能

How to pass html in jquery datatables row add function

本文关键字:数据表 添加 功能 html jquery      更新时间:2023-09-26

下面是我从angular向jquery数据表中添加行的方法

reviewManger.GetUnapprovedReviews().then(function (data) {
    if (data != null) {
        var result = Reviews.Common.MakeValidJSON(data);
        if (result != null) {
            $scope.reviews = result;
            var table = $("#editable");
            var tp = table.DataTable();
            for (var i = 0; i < $scope.reviews.length; i++) {
                tp.row.add([
                    $scope.reviews[i].ID,
                    $scope.reviews[i].AddedOn,
                    $scope.reviews[i].Company.Name,
                    $scope.reviews[i].User.Name,
                    $scope.reviews[i].Description,
                    $sce.trustAsHtml("<span ng-click='EnableDisable(" + $scope.reviews[i].ID + ")>Change</span>")
                ]).draw();
            }
        }
    }
}, function (error) {
});

问题是我没有看到在所有行的最后一列呈现HTML 在jquery数据表中,所有其他列都填充在所有行。

如何在jQuery数据表行添加html ?

我必须编译angular模板才能工作使用了$compile from angular以下是我修改的代码:

  if (result != null) {
                    $scope.reviews = result;
                    var table: any = $("#editable");
                    //table.DataTable();
                    var tp = table.DataTable();
                    for (var i = 0; i < $scope.reviews.length; i++) {
                        var id = $scope.reviews[i].ID;
                        var checked = $scope.reviews[i].Enabled == "1" ? true : false;
                        tp.row.add(
                            [
                                $scope.reviews[i].ID,
                                $scope.reviews[i].AddedOn,
                                $scope.reviews[i].Company.Name,
                                $scope.reviews[i].User.Name,
                                $scope.reviews[i].Description,
                                "<div class='switch'><div class='onoffswitch'><input ng-checked='" + checked+"' class='onoffswitch-checkbox' id= 'stat" + id + "' type= 'checkbox'><label class='onoffswitch-label' for='stat" + id + "'><span class='onoffswitch-inner'></span><span class='onoffswitch-switch'></span></label></div></div>"
                                //"<div class='switch'><div class='onoffswitch'><input checked='' class='onoffswitch- checkbox' id= 'stat" + id + "' type= 'checkbox'><label class='onoffswitch- label' for='stat" + id + "'><span class='onoffswitch- inner'></span><span class='onoffswitch-switch'></span></label></div></div>"
                                //"<button ng-click='EnableDisable(" + $scope.reviews[i].ID + ")'>Change</button>"
                            ]
                            );
                    }
                    tp.draw();
                    $compile(table)($scope); 
                }

您必须将受信任的html与<div ng-bind-html="tp.row[x]"></div>或类似的东西绑定以获得html替换。我看不到你的加价,所以要视情况而定。把标记贴出来可能对你有好处。