Bind button in a jQuery DataTable

1,699 views
Skip to first unread message

Jehu

unread,
Sep 10, 2011, 6:43:42 AM9/10/11
to AngularJS
Hi there!

Thanks to the AngularJS Developers! I've tried backbone, spline,
knockout - but angular seems to be the best choice for some planned
realtime applications.

Doing some tests and experiments, i stumble to the following problem:
I've createt a custom directive for a jQuery DataTable Plugin.
Here's a fiddle to make thinks clear: http://jsfiddle.net/Jehu/2FfTW/4/

I want to bind the button in a grid row to a method in my controler.
Button should later be used to trigger REST actions by $xhr service.
I hope, the grid will represent allways the data model due bindings.
So, would you reccommend the use of DataTables Plugin or should i
better use a simple html table and add filtering and sorting with
angular?

Thanks in advance.
Jehu

Misko Hevery

unread,
Sep 12, 2011, 1:45:19 PM9/12/11
to ang...@googlegroups.com
  • To make the button work, you need a bit more code.
The issue is that just because you add ng:click it does not magically tell angular that it needs to register a listener. So you have to compile the DOM, but the dataTable will add and remove rows without angular's knowledge, and so it will be hard to keep calling the compile.

So the simplest thing to do is this:
  • add a click listener to the button, or generic listener to the root of the table. 
  • Inside the click listener you can do element.scope() to get the current scope and then do scope.removeItem(...);
This should do the trick

-- misko


--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/angular?hl=en.


Jehu

unread,
Sep 13, 2011, 2:44:13 AM9/13/11
to AngularJS
Thank you misko!

On Sep 12, 7:45 pm, Misko Hevery <mi...@hevery.com> wrote:
>    - To make the button work, you need a bit more code.
>
> The issue is that just because you add ng:click it does not magically tell
> angular that it needs to register a listener. So you have to compile the
> DOM, but the dataTable will add and remove rows without angular's knowledge,
> and so it will be hard to keep calling the compile.
>
> So the simplest thing to do is this:
>
>    - add a click listener to the button, or generic listener to the root of
>    the table.
>    - Inside the click listener you can do element.scope() to get the current

Makarand Bhatamrekar

unread,
Dec 10, 2013, 10:31:25 AM12/10/13
to ang...@googlegroups.com, mi...@hevery.com
Hi,

You can also try to add the following lines to to your controller

 $scope.options.fnCreatedRow = function( nRow, aData, iDataIndex ) {
        $compile(nRow)($scope);
    }

also, take a look at this example
http://jsfiddle.net/TNy3w/2/

regards
Reply all
Reply to author
Forward
0 new messages