ui-sref not working when generate dynamic content for datatable columns
Asked Answered
E

2

5

I'd to link a column of my data-table to a dynamic Angularjs view.

Table 1 :

ID | Name | Action 

1  | Jack | Edit

Edit should be a link to #/clients/1/edit

/clients/:id/edit (app.client_edit) is already created and it's working.

I'm trying the code below:

$scope.dataTableOpt = {
 "columnDefs": [
    {
       "targets": 0,
       "render": function ( data ) {
          return "<a ui-sref=\"app.client_view({id: $row[0]})\">Edit</a>";
       }
    }
 ],
 'ajax': 'http://localhost/admin/clients'
};

Given result:

Link1 = < a ui-sref="app.client_view({'id': '1'})">edit</ a>

Expected result:

Link2 = < a ui-sref="app.client_view({id: '1'})" class="ng-scope" href="#!/client/2">edit</ a>

When I put < a ui-sref="app.client_view({'id': '1'})">test< / a> on the page statically it's working but not sure why it doesn't work when it's dynamically generated.

Please advise.

Emeraldemerge answered 15/7, 2015 at 7:14 Comment(0)
E
9

my way:

DataTable set options:

"columnDefs": [
   {
      "targets": [0, 1],
      "render": function ( data, type, row ) {
         var href = $compile('<a ui-sref="stateName({id: ' + $stateParams.id + '})"></a>')($scope)[0].href;
         return '<a href="' + href + '">' + data + '</a>';
       }
    }
]
Emeraldemerge answered 3/8, 2015 at 9:12 Comment(1)
Thank you so much abbas, i spent 2 days to find this solution and it worked like charm. Thank you so much.Bidget
A
1

@Abbas Moazzemi

If Use .withOption('createdRow' as below, you dont need to use $compile:

.withOption('createdRow', function(row) {
    // Recompiling so we can bind Angular directive to the DT
    $compile(angular.element(row).contents())($scope);
})
Abdominal answered 23/12, 2017 at 5:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.