简体   繁体   中英

How to pass html in jquery datatables row add function

Following is the way im adding rows in jquery datatables from angular

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) {
});

The problem is i dont see the rendered HTML in last column of all rows in jquery data tables , all other columns are filled in all rows.

How to add html in jQuery data table row?

I had to compile the angular template in order to work Used $compile from angular Following is the code i changed :

  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); 
                }

you must bind trusted html with <div ng-bind-html="tp.row[x]"></div> or something like that to get the html replacement. I can't see your markup so it depends. Might benefit you to post the markup.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM