Grails 显示数据

xiaoxiao2021-02-27  156

一、显示数据 1. 在controller中创建一个showlistcontroller 从数据库中获取数据

def show(){} //ware是一个实体类(在数据库中有同名的表) //保存在list中 //一个方法 def getList() { def list = Ware.list(max: maxsize,offset: start); //render是为了传输数据,json为数值对的形式 //data:list render(contentType: "application/json") { [state: 'ok', code:"0" , data: list, listCount:Ware.count()] }

2.在web-app中js文件夹创建一个名为showlist的文件夹, 在里面创建一个show.js的js文件,为了从controller中获取数据并传给gsp页面

//全局变量 list $scope.List = []; //写js的方法 $scope.getList= function (){ //WEBROOT+'/list/getList' list 指的是ListController, getlist是这个类中的方法 $http({method : 'POST',url : WEBROOT+'/list/getList',params:{pageindex:$scope.selPage},headers : window.utf8_headers}) .success(function(rtndata, status, headers, config){ //访问getlist方法成功后,rtndata中存储的就是render 中的数据 $scope.pageL = false; //把数据存储在全局变量list中 $scope.List = rtndata.data;

3.在views文件夹下创建一个showlist的文件夹(此名字一定要与js中的名字叫相同) 在文件夹下创建一个show.gsp (这个名字也要与show.js名字相同) 为了显示数据

//ng-repeat是循环显示数据 list in List list是一个变量名,List是Js中全局变量List <tr class="gradeA" ng-repeat="list in List" > //都是实体类对应的一些属性 <td>{{ list.title }}</td> <td>{{list.jifen}}</td> <td>{{list.price}}</td> <td>{{list.wareCount}}</td> <td>{{list.statusTr}}</td>

4.show.gsp与show.js要有一个对应 show.gsp中

在<body >中最大的div <div ng-controller="showlistctr">

show.js中

//定义自己的一个ID var appid = "list" window.appModels.push(appid) // controller ‘showlistctr’要与gsp中的ng-controller="showlistctr"对应 var app = angular.module(appid, []); app.controller('showlistctr', function($scope,$http) { }
转载请注明原文地址: https://www.6miu.com/read-15436.html

最新回复(0)