Question

In a application I'm working on I need to add some filters like for example: approved, with documents, with documents approved and some others. All these filters is based on MySQL queries to DB performed by PHP controller (I'm using Symfony2) and the functions on that controllers will return a well formed JSON. My question is: can I write filters for AngularJS based on this behavior? How? (I mean a little example just for understand the flow)

Was it helpful?

Solution

I think you can do by something similar to below code:

//in your symfony controll
public function sampleAction() {
   $data = $this->getDoctrine()->getManager()->getRepository('YourBundle:SampleEntity')->findAll()->toArray();
   return $this->render('YourBundle:Views_Path:sampleTwigOutput.html.twig', array(
      'data' = json_encode($data)
   ));
}

In your twig file you can have something like

<div ng-init="mydata = {{ data|raw }}"></div>
<table id="sortedData">
  <tr><th>T1</th><th>T2</th></tr>
  <tr ng-repeat="data in mydata | filter:sortData">
     <td>{[{data.name}]}</td>
     <td>{[{data.phone}]}</td>
  </tr>

You also need to change the start and end symbol for to something different (like {[{ and }]}) interpolateProvider
Now you need to define a sortData js function like

angular.module('MySortModule', []).
  filter('sortData', function() {
    // do all your stuffs to sort the data base on whatever you want
    // set them all to out
    return out;
});

You can find more info about it on Angular filter

I would recommend to sort the data in DB through Symfony which is faster and in angular output just dump the data

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top