CodeIgniter Forums
Should data be manipulated in the controller or is it "View"'s job - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12)
+--- Thread: Should data be manipulated in the controller or is it "View"'s job (/showthread.php?tid=65888)



Should data be manipulated in the controller or is it "View"'s job - rliy001 - 08-05-2016

    [attachment=658][attachment=658]Hi there,

New to CodeIgniter. 
Hoping to use AngularJS on the Views and Code igniter for back end.


I'm wondering whether to touch and manipulate the data in the controller/ get it into the required format before passing it on to the view
OR not do anything in the controller and simply pass the data to the view so it becomes AngularJS's problem.

On one particular case, data is fetched from the database in one query and returned in a single array like so...

Season              Grade      RoundDate        Time      GameID         Team1        Team2      Team1Score       Team2Score  
------------------------------------------------------------------------------------------------------------------------------------------
Winter 2016     MENS 1      5 Aug             10:00            1              TEAM A      TEAM B       110                     140
Winter 2016     MENS 1      5 Aug             11:00            2              TEAM C      TEAM D       130                     110
Winter 2016     MENS 1      1 Aug             10:00            3              TEAM B      TEAM C       95                       120
Winter 2016     MENS 1      1 Aug             11:00            4              TEAM A      TEAM D       95                       120
Winter 2016     MENS 1      24 Jul             10:00            5              TEAM A      TEAM D       75                       107
Winter 2016     MENS 1      23 Jul             10:00            6              TEAM B      TEAM C       110                     140


Controller simply passes this data to the view in a single array $data

In the view I would like my data to display like below. As you can see the data is grouped by a several columns so I'm a little lost as to whether it is Angular's job to do this grouping and display on the page or whether I should touch the data in the controller and somehow return the data to the view in several arrays representing each of those grouped sections... or whether I should make multiple calls to the database to fetch each of the required data sections on the fly. Or a completely different approach that I have overlooked. Little help?

-------------------------------------------------
WINTER 2016 - MENS 1

-------------------------------------------------
5 AUG 2016

10:00 TEAM A  100 - 120 TEAM B
11:00 TEAM C 150 - 90 TEAM D


-------------------------------------------------
1 AUG 2016

10:00 TEAM B  111 - 87 TEAM D
11:00 TEAM A 89 - 90 TEAM C

-------------------------------------------------

25 JUL 2016

10:00 TEAM A  111 - 87 TEAM D


-------------------------------------------------
23 JUL 2016

10:00 TEAM B  111 - 87 TEAM C



I have attached how my data is currently rendered by AngularJS. As you can see it is currently displayed as a flat table. No groupings present


RE: Should data be manipulated in the controller or is it "View"'s job - JayAdra - 08-05-2016

I haven't used Angular before, but in general I would recommend a single database call, and do all of your processing/grouping/sorting of data in your controller (or better yet, a library, to keep controllers lean).

The view should then simply be iterating over the data and displaying it.


RE: Should data be manipulated in the controller or is it "View"'s job - PaulD - 08-05-2016

I think Jay Adra is correct generally, but in your particular example on date sorting, it is just as easy to send all the data in a single array, and have a little check that on date change put new title and carry on. A small bit of logic in the view can save a lot of hassles otherwise. I can't see any advantage to doing array manipulations within the controller or a library just to group out the results like that.

However, I suppose it depends on the complexity of the real data and what you are allowing users to do with it, and what you are doing with the page in the future. For instance will it always be a simple list of dates, or will you be doing further manipulations, like onclick revealing a panel showing more match details etc.

Generally, I agree that manipulations should be done in controllers or libraries, but in similar cases to your example, I think it might be a bit too puritan to insist that every tiny bit of logic (in this case for date separation), a line or two at most of simple code, should not be in the view, particularly since that logic is purely view related.

But, if you are going to need a similar list on multiple pages/locations then a small library routine to sort a general array into a more easily looped array for that type of list view might not be a bad idea either.

So I suppose I am saying it depends.

Best wishes,

Paul.


RE: Should data be manipulated in the controller or is it "View"'s job - d4jk4 - 10-21-2016

Data should be done in controllers or libraries. But you can push anywhere you want: views, controllers, models, helpers, libraries...