Advertisement
Guest User

ember data grid

a guest
Nov 7th, 2013
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //    JS
  2. App.DataGridView = Em.View.extend({
  3.  
  4.     tagName : 'table',
  5.    
  6.     layoutName : 'dataGrid',
  7.  
  8.     orderBy : null,
  9.     order : 'asc',
  10.     currentPage: 1,
  11.     limit: 10,
  12.     total : 0,
  13.  
  14.     init : function() {
  15.         this._super();
  16.         var pagingParams = this.get('controller.pagingParams');
  17.         if (pagingParams) {
  18.             this.set('orderByProp', pagingParams.orderBy);
  19.             this.set('limit', pagingParams.limit);
  20.             this.set('currentPage', Math.floor(pagingParams.offset / pagingParams.limit) + 1);
  21.             this.set('order', pagingParams.desc ? 'desc' : 'asc');
  22.         }
  23.     },
  24.    
  25.     onCollectionUpdate : function() {
  26.         this.set('total', this.get('controller.collectionSize'));
  27.     }.observes('controller.collectionSize'),
  28.    
  29.     didInsertElement : function() {
  30.         this.onCollectionUpdate();
  31.     },
  32.    
  33.     maxPage : function() {
  34.         return Math.floor(this.get('total')/this.get('limit')) + 1;
  35.     }.property('total', 'limit'),
  36.    
  37.     columns : function() {
  38.         var orderByProp = this.get('orderByProp');
  39.         var order = this.get('order');
  40.         var columns = this.get('columnNames').split('|').map(function(item) {
  41.             var arr = item.split('=');
  42.             var label = arr.length==2 ? arr[1].trim() : arr[0].trim();
  43.             var name = arr.length==2 ? arr[0].trim() : "";
  44.             var sortClass = "";
  45.             if (orderByProp === name) {
  46.                 sortClass = order;
  47.             }
  48.             return  Em.Object.create({name : name, label : label, sortClass: sortClass});
  49.         });
  50.        
  51.         return columns;
  52.     }.property('columnNames').cacheable(),
  53.    
  54.     colCount : function() {
  55.         return this.get('columns.length');
  56.     }.property('columns'),
  57.    
  58.     pagingToShow : function() {
  59.         var res = [];
  60.         var currentPage  = this.get('currentPage');
  61.         var maxPage  = this.get('maxPage');
  62.         if (currentPage>3) {
  63.             res.push({label:'...'});
  64.         }
  65.         if (currentPage>2) {
  66.             res.push({label:currentPage-2, link:true});
  67.             res.push({label:currentPage-1, link:true});
  68.         } else if (currentPage==2) {
  69.             res.push({label:1, link:true});
  70.         }
  71.        
  72.         res.push({label:currentPage});
  73.        
  74.         if (currentPage < maxPage) {
  75.             res.push({label:currentPage+1, link:true});
  76.         }
  77.         if (currentPage + 1 < maxPage) {
  78.             res.push({label:currentPage+2, link:true});
  79.         }
  80.         if (currentPage + 2 < maxPage) {
  81.             res.push({label:"..."});
  82.         }
  83.        
  84.         return res;
  85.     }.property('currentPage', 'maxPage'),
  86.    
  87.     isLimit10 : function() {
  88.         return this.get('limit') == 10;
  89.     }.property('limit'),
  90.     isLimit20 : function() {
  91.         return this.get('limit') == 20;
  92.     }.property('limit'),
  93.     isLimit30 : function() {
  94.         return this.get('limit') == 30;
  95.     }.property('limit'),
  96.    
  97.     actions : {
  98.         sortCol : function(col) {
  99.             if (this.get('orderBy') != col) {
  100.                 if (this.get('orderBy')) {
  101.                     this.get('orderBy').set('sortClass', '');
  102.                    
  103.                 }
  104.                
  105.                 col.set('sortClass', 'asc');
  106.                 this.set('orderBy', col);
  107.                 this.set('order', 'asc');
  108.             } else if (this.get('order') == 'asc') {
  109.                 this.set('order', 'desc');
  110.                 col.set('sortClass', 'desc');
  111.             } else {
  112.                 this.set('orderBy', null);
  113.                 col.set('sortClass', '');
  114.             }
  115.            
  116.             this.updateState();
  117.         },
  118.        
  119.         firstPage : function() {
  120.             this.set('currentPage', 1);
  121.             this.updateState();
  122.         },
  123.         prevPage : function() {
  124.             var currentPage = this.get('currentPage');
  125.             this.set('currentPage', currentPage > 1 ? currentPage-1 : 1);
  126.             this.updateState();
  127.         },
  128.         toPage : function(page) {
  129.             this.set('currentPage', page);
  130.             this.updateState();
  131.         },
  132.         nextPage : function() {
  133.             var currentPage = this.get('currentPage');
  134.             this.set('currentPage', currentPage < this.get('maxPage') ? currentPage+1 : this.get('maxPage'));
  135.             this.updateState();
  136.         },
  137.         lastPage : function() {
  138.             this.set('currentPage', this.get('maxPage'));
  139.             this.updateState();
  140.         },
  141.         setLimit : function(limit) {
  142.             this.set('limit' , limit);
  143.             this.updateState();
  144.         }
  145.     },
  146.    
  147.     getParams : function() {
  148.         return {
  149.             limit : this.get('limit'),
  150.             offset : (this.get('currentPage')-1)*this.get('limit'),
  151.             orderBy : this.get('orderBy.name'),
  152.             desc : this.get('order') == 'desc'
  153.         };
  154.     },
  155.    
  156.     updateState : function() {
  157.         this.get('controller').send('updateDataGridState', {
  158.             collectionName : this.get('collectionName'),
  159.             params : this.getParams()
  160.         });
  161.     }
  162. });
  163.  
  164.  
  165.  
  166.  
  167. //Grid template
  168.  
  169.  
  170.  
  171. <script type="text/x-handlebars" data-template-name="dataGrid">
  172.     <thead>
  173.         <tr>
  174.             {{#each col in view.columns}}
  175.                 <th>
  176.                     {{#if col.name}}
  177.                         <a {{action sortCol col target="view"}}>{{col.label}}</a>
  178.                         <i {{bind-attr class=":sort col.sortClass"}}></i>
  179.                     {{else}}
  180.                         {{col.label}}
  181.                     {{/if}}
  182.                 </th>
  183.             {{/each}}
  184.         </tr>
  185.     </thead>
  186.     <tbody>
  187.         {{yield}}
  188.     </tbody>
  189.     <tfoot>
  190.         <tr>
  191.             <td {{bind-attr colspan="view.colCount"}}>
  192.                 <div class="paging-controls">
  193.                     <a {{action firstPage target="view"}}>
  194.                         <i class="paging paging-first"></i>
  195.                     </a>
  196.                     <a {{action prevPage target="view"}}>
  197.                         <i class="paging paging-prev"></i>
  198.                     </a>
  199.                    
  200.                     {{#each page in view.pagingToShow}}
  201.                         {{#if page.link}}
  202.                             <a {{action toPage page.label target="view"}}>
  203.                                 {{page.label}}
  204.                             </a>
  205.                         {{else}}
  206.                                 {{page.label}}
  207.                         {{/if}}
  208.                     {{/each}}
  209.                     <a {{action nextPage target="view"}}>
  210.                         <i class="paging paging-next"></i>
  211.                     </a>
  212.                     <a {{action lastPage target="view"}}>
  213.                         <i class="paging paging-last"></i>
  214.                     </a>
  215.                 </div>
  216.                 <div class="limit-controls">
  217.                     Отображать по
  218.                     <a {{action setLimit 10 target="view"}} {{bind-attr class="isLimit10:active"}}> 10 </a>
  219.                     <a {{action setLimit 20 target="view"}} {{bind-attr class="isLimit20:active"}}> 20 </a>
  220.                     <a {{action setLimit 30 target="view"}} {{bind-attr class="isLimit30:active"}}> 30 </a>
  221.                     строк
  222.                 </div>
  223.             </td>
  224.         </tr>
  225.     </tfoot>
  226.  
  227. </script>
  228.  
  229.  
  230. //grid usage
  231.  
  232. {{#view App.DataGridView class="dashletTable" collectionName="audit" collectionBinding="controller.model"
  233.             columnNames="№ | datetime=Дата | userId=Пользователь | Сообщение" }}
  234.                    
  235.                 {{#each cont in controller}}
  236.                     <tr>
  237.                         <td>
  238.                             {{#link-to "audit.detail" cont}}{{cont.id}}{{/link-to}}
  239.                         </td>
  240.                         <td>
  241.                             {{view App.FormatDateView dateBinding='cont.datetime' format="DD.MM.YYYY HH:mm:ss"}}                       
  242.                         </td>
  243.                         <td>
  244.                             {{#if cont.user}}
  245.                                 {{cont.user.name}}
  246.                             {{else}}
  247.                                 Система
  248.                             {{/if}}
  249.                         </td>
  250.                         <td>
  251.                             {{cont.message}}
  252.                         </td>
  253.                     </tr>
  254.                 {{else}}
  255.                     <tr>
  256.                         <td colspan="4">
  257.                             Нет данных
  258.                         </td>
  259.                     </tr>
  260.                 {{/each}}
  261.             {{/view}}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement