dongyinglan8707 2015-07-20 17:29
浏览 73
已采纳

如何使用Angularjs操作从SQL返回的数据

I am using AngularJS to call a SQL query and return a whole table. Is there a way to manipulate data on the front end once I've retrieved the data or is better to do the manipulation on the processing page before it gets back to Angular?

The code below shows the app.js file that I am using to retrieve the data and I have included a line of HTML to show a sample of what type of output I am trying to achieve.

app.js

var dashboardApp = angular.module('dashboardApp', []);
dashboardApp.controller('dashboardController', function($scope, $http, $location, $anchorScroll) {

    $http.get('lib/dbp.php').success(function(data) {
      $scope.surveydata = data;
    }); 

});

Sample HTML

<ul>
    <li ng-repeat="item in surveydata['data']">{{item.location}}</li>
</ul>

{{surveydata['data'].location.length}}

Sample Output

{ "data": [ 
  { 
    "surveyid": "51", 
    "customerName": "Steve Jones", 
    "companyName": "Acme", 
    "projectName": "Various Project", 
    "customerPhone": "", 
    "customerEmail": "", 
    "location": "Dallas", 
    "q1": "1", 
    "q1Comment": "None", 
    "q2": "1", 
    "q2Comment": "None", 
    "q3": "1", 
    "q3Comment": "None", 
    "q4": "2", 
    "q4Comment": "None", 
    "q5": "1", 
    "q5Comment": "None", 
    "q6": "Yes", 
    "q6Comment": "Always.", 
    "q7": "Yes", 
    "q7Comment": "Always.", 
    "q8": "Yes", 
    "q8Comment": "Certainly.", 
    "q9": "Yes", 
    "q9Comment": "None", 
    "q10Comment": "None", 
    "q11Comment": "Keep up the good work!", 
    "q12": "No", 
    "timestamp": "2015-07-17 11:08:24", 
    "ipaddress": "" 
  }

The ng-repeat section works fine for basic data output but with the location.length what I'd actually like to do is be able to count the number of locations individually, i.e. zone 1 has 5 surveys submitted or zone 2 has 8.

I am new to Angularjs so I am trying to figure out what the best practice is for this type of situation.

What I would like to see as output is a count of all records with the location of "Dallas" or an average of scores from all records from "Dallas". There are numerous other things I would like to do but having a grasp on how to do simple data manipulation like those mentioned would help me for other applications of the data.

  • 写回答

1条回答 默认 最新

  • dsvf46980 2015-07-20 18:02
    关注

    Using an SQL query to always return an entire table is not typically a good practice. It's acceptable if you are guaranteed to have a small table forever (a strong statement). Otherwise it's best to target your queries for whatever data will be presented, and use a second query for high-level information like count.

    I don't know what your backend looks like, but a typical approach would be something like this (pseudocode):

    data = {}
    SELECT surveyid, customerName, ... FROM "YourTable" WHERE (...) LIMIT 48 OFFSET 0;
    response -> data.rows = response
    SELECT count(*) FROM "YourTable" WHERE (...);
    response -> data.count = response
    return data
    

    Then on the frontend you're manipulating { rows: [...], count: 123 }, which should be in the format you want given your needs above.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)