Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 568
Abbas Moazzami Avatar asked Jul 15 '15 07:07

Abbas Moazzami


2 Answers

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>';
       }
    }
]
like image 113
Abbas Moazzami Avatar answered Sep 20 '22 12:09

Abbas Moazzami


@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);
})
like image 32
Morteza QorbanAlizade Avatar answered Sep 19 '22 12:09

Morteza QorbanAlizade