UI-Grid 排序

距离上次写博客已经有一个月时间了,今天记录哈angularJS中排序问题。

要求是点击商品价格可以进行升序或降序进行排序。

先上一段代码:

app.controller('newProductController', [
    '$scope','newProductService', 'gridDecorator','subject','datePickerDecorator', 'modalDecorator','selectDecorator','forwardDecorator', '$state','$window','$timeout', function($scope,newProductService, gridDecorator,subject,datePickerDecorator, modalDecorator,selectDecorator,forwardDecorator, $state,$window,$timeout) {
        var gridOptions = {
            columnDefs: [
                { name: 'id',displayName:'编号' },
                { name: 'onlineProductId',displayName:'商品ID' },
                { name: 'name',displayName:'商品名称'},
                { name: 'showPosition',displayName:'全部位置',cellFilter: 'newProductShowPositionTypeFormat',minWidth:120,minWidth:120},//,cellFilter: 'newProductCatalogTypeFormat',minWidth:70,maxWidth:70
                { name: 'fromFirst',displayName:'商品来源',cellFilter: 'newProductfromFirstTypeFormat'},
                { name: 'status',displayName:'状态',cellFilter: 'newProductStatusTypeFormat'},
                { name: 'coinPrice',displayName:'商品价格',minWidth:90,enableSorting: true},//useExternalSorting是否自定义排序
                { name: 'startDate',displayName:'开始时间',minWidth:140 ,enableSorting: true},//enableSorting是否排序
                { name: 'endDate',displayName:'结束时间',minWidth:140 ,enableSorting: true},
                { name: 'classifyFirst',displayName:'商品分类',minWidth:100 },
                { name: 'operation',displayName:'操作', enableSorting: false,minWidth:240,
                    cellTemplate:'<div align="left" style="margin-left: 10px;">' +
                    '<button class="btn btn-info" type="button" ng-click="grid.appScope.edit(row.entity.id)" >编辑</button> '+
                    '<button type="button"  class="btn btn-danger"  ng-click="grid.appScope.delete(row.entity.id)">删除</button> </div>'}
            ],
            simple:false

        };
        //在这里进行商品价格排序
        //orderBy:sortFlag;


        var id = !subject.authenticationInfo.credentials.customerId? -1 : subject.authenticationInfo.credentials.customerId;
        var searchOption = {customerId:id};
        var gridDecorator = gridDecorator.getInstance(newProductService,searchOption,gridOptions,$scope,"newProduct");
        gridDecorator.autoDecorate();

        //填充商品来源下拉框
        selectDecorator.getInstance(null, platformConstants.fromFirst, $scope, "fromFirst").autoDecorate();
        填充商品状态下拉框
        selectDecorator.getInstance(null, platformConstants.status, $scope, "status").autoDecorate();
        填充全部分类下拉框
        //selectDecorator.getInstance(null, platformConstants.expireFlag, $scope, "expireFlag").autoDecorate();

        //删除的预览初始化装饰器 ,将gridDecorator传入做回调,新建和编辑交给forward装饰器做处理(这是删除的添加)
        modalDecorator.getInitInstance(newProductService,{},$scope,"newProduct",gridDecorator).autoDecorate();

        //将覆盖modal装饰器渲染的新建和编辑行为(会自行跳转到编辑页面)
        forwardDecorator.getInitInstance(newProductService, {templateCuUrl: 'tvpublish.newproduct.edit'}, $scope, "newProduct").autoDecorate();
    }]);
大家可以看到商品价格后面的 enableSorting: true就是是否排序。

当直接查询的时候是没有排序的,当点击商品价格的时候,会自动带参数向后台发送请求。如下图所示:



上面是直接查询的url,下面是点击商品价格时的url。

大家可以看到此处的参数就是name: 'coinPrice',而这个coinPrice就是后台商品价格的属性名称,而最后去查询的时候sql语句会变成

SELECT tnp.new_product_id, tnp.online_product_id, tnp.coin_price , tnp.intergration_price, tnp.name, tnp.start_date AS startDate, tnp.end_date AS endDate, tnp.create_date, tnp.create_by, tnp.update_date, tnp.update_by, tnp.status, tnp.classify_first, tnp.classify_second, tnp.from_first, tnp.from_second, tnp.show_position, u.name AS createNameBy, u1.name AS updateNameBy FROM tv_new_product tnp LEFT JOIN sys_t_user u ON tnp.create_by = u.id LEFT JOIN sys_t_user u1 ON tnp.update_by = u1.id WHERE 1 = 1 order by coinPrice asc limit ?,?

此时要注意最后的order by coinPrice asc  而这个参数就是前台带过来的  但是我的表中并没有coinPrice这个字段,此时报一个错误:


这个意思是说没有coinPrice这个字段。

解决方法:刚开始我的想法是在前台进行转换,但是我并没有找到一个属性可以把coinPrice转换成coin_price带过来,后来我直接把sql中的tnp.coin_price起个别名,换成tnp.coin_price as coinPrice  问题就解决了。如下图所示:




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小盒子_spring

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值