Created
December 14, 2015 12:55
-
-
Save vistajess/667e78b2f9d26734cb1b to your computer and use it in GitHub Desktop.
new_orders.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var Orders = function() { | |
| var showUuid = function(uuid) { | |
| $('#remote_uuid').html(uuid); | |
| }; | |
| var mustacheRender = function(temp,obj,target) { | |
| var template = temp.html(); | |
| Mustache.parse(template); | |
| var rendered = Mustache.render(template,obj); | |
| target.html(rendered); | |
| } | |
| var renderRefreshButton = function() { | |
| if( $('#refresh_table_btn').size() ) { | |
| return null; | |
| } else { | |
| var refresh_button = '<button type="button" \ | |
| class="btn btn-success" \ | |
| id="refresh_table_btn"> \ | |
| <i class="fa fa-refresh"></i>\ | |
| Refresh Table \ | |
| </button>'; | |
| $('#datatable_orders_filter').append(refresh_button); | |
| }; | |
| } | |
| var handleOrders = function(api,userid,access_token,pageNum,total) { | |
| var table = $('#datatable_orders').DataTable({ | |
| /* | |
| FOR REFACTORING | |
| */ | |
| "ajax": function (data, callback, settings) { | |
| $.get(api + "/me/orders?access_token=" + access_token + "&limit="+total+"&page=" + pageNum) | |
| .success(function(res) { | |
| var element = '<div id="pagination-buttons" class="pull-right"> \ | |
| <button type="button" class="btn btn-default" id="prev-page"> \ | |
| <i class="fa fa-chevron-left"></i> \ | |
| </button> \ | |
| <button type="button" class="btn btn-default" id="next-page"> \ | |
| <i class="fa fa-chevron-right"></i> \ | |
| </button> \ | |
| </div>'; | |
| // $(".table-scrollable").next().children('.col-md-7').append(element); | |
| callback({ | |
| data: res.data | |
| }); | |
| }); | |
| }, | |
| "columns": [ | |
| { "data": "created_at" }, | |
| { "data": "remote_uuid" }, | |
| { "data": | |
| function(data) { | |
| // Find the total amount excluding the SHIPPING FEE | |
| return obj = data.products.map(function(val, key){ | |
| return val.quantity * val.selling_price; | |
| }).reduce(function(total,current){ | |
| return total + current; | |
| },0); | |
| } | |
| }, | |
| { "data": | |
| function(data) { | |
| //Find the total quantity of all items in each order | |
| return obj = data.products.map(function(val, key){ | |
| return val.quantity; | |
| }).reduce(function(total,current){ | |
| return total + current; | |
| },0); | |
| } | |
| }, | |
| { "data" : "status" }, | |
| { "data" : | |
| function(data) { | |
| var products = data.products.filter(function(item,key){ | |
| return item.is_voucher == false; | |
| }); | |
| var vouchers = data.products.filter(function(item,key){ | |
| return item.is_voucher == true; | |
| }); | |
| return '<button type="button" \ | |
| class="btn btn-primary btn-summary" \ | |
| id="order_id'+data.id+'" \ | |
| data-toggle="modal" \ | |
| data-uuid="'+data.remote_uuid+'" \ | |
| data-target="#modal_orders" \ | |
| data-vouchers="'+encodeURIComponent(JSON.stringify(vouchers))+'"\ | |
| data-products="'+encodeURIComponent(JSON.stringify(products))+'"> \ | |
| Summary \ | |
| </button>'; | |
| } | |
| }, | |
| { "data": | |
| function (data) { | |
| return data.actions.map(function(val,idx){ | |
| return val.text === 'Print Way Bill' | |
| ? '<a \ | |
| class="btn btn-default action-btn" \ | |
| data-id="'+userid+'" \ | |
| href='+val.url+'> \ | |
| '+val.text+' \ | |
| </a>' | |
| : '<button \ | |
| class="btn btn-info action-btn" \ | |
| data-id="'+userid+'" \ | |
| data-order-url='+val.url+'> \ | |
| '+val.text+' \ | |
| </button>'; | |
| }).join(""); | |
| } | |
| } | |
| ], | |
| "columnDefs": [ | |
| { | |
| "targets": 2,//index of column starting from 0 | |
| "data": "net_amount", //this name should exist in your JSON response | |
| "render": function ( data, type, full, meta ) { | |
| return '<span>P '+data+'</span>'; | |
| } | |
| } | |
| ], | |
| "lengthMenu": [ | |
| [15, 20, 50, 100, 150], | |
| [15, 20, 50, 100, 150] // change per page values here | |
| ], | |
| "bPaginate": true, | |
| "bFilter" : true, | |
| "bLengthChange": false, | |
| "order": [[0, 'desc']] | |
| }); | |
| table.on('draw', function() { | |
| renderRefreshButton(); | |
| // FOR THE SUMMARY | |
| var btn_summary = $('.btn-summary'); | |
| btn_summary.click(function() { | |
| var products = decodeURIComponent($(this).data('products')); | |
| var vouchers = decodeURIComponent($(this).data('vouchers')); | |
| var product_result = JSON.parse(products).map(function(item,key){ | |
| return item; | |
| }); | |
| var voucher_result = JSON.parse(vouchers).map(function(item,key){ | |
| return item; | |
| }); | |
| mustacheRender($('#product-template'),{ "products" : product_result},$('#product-target')); | |
| mustacheRender($('#voucher-template'),{ "vouchers" : voucher_result},$('#voucher-target')); | |
| // FOR THE PRODUCT ACTIONS ================== | |
| var action_btn = $('.btn-action-products'); | |
| action_btn.click(function() { | |
| var $this = $(this); | |
| var url = $(this).data('product-url'); | |
| $this.addClass('disabled').html('<span class="fa fa-spin fa-spinner"></span> Loading'); | |
| $.post( url, function( data ) { | |
| console.log('success'); | |
| }); | |
| $this.fadeOut(); | |
| table.ajax.reload(function(){ | |
| console.log('successfully reloaded'); | |
| }); | |
| }); | |
| showUuid($(this).data('uuid')); | |
| //========================== | |
| }); | |
| // FOR THE PRODUCT ACTION | |
| var order_action = $('.action-btn'); | |
| order_action.click(function() { | |
| var $this = $(this); | |
| var url = $(this).data('order-url'); | |
| $this.addClass('disabled').html('<span class="fa fa-spin fa-spinner"></span> Loading'); | |
| $.post( url, function( data ) { | |
| table.ajax.reload(); | |
| }); | |
| $this.delay().fadeOut('slow',function(){ | |
| $this.fadeIn().html('Success').delay(2000).fadeOut(); | |
| }); | |
| }); | |
| $('#refresh_table_btn').click(function(){ | |
| $this = $(this); | |
| $this.addClass('disabled').html('<span class="fa fa-spin fa-spinner"></span> Loading'); | |
| table.ajax.reload(function(){ | |
| $('#refresh_table_btn').removeClass('disabled').html('<i class="fa fa-refresh"></i> Refresh Table'); | |
| }); | |
| }); | |
| }); | |
| } | |
| return { | |
| init: function(api,userid,access_token,pageNum,total) { | |
| handleOrders(api,userid,access_token,pageNum,total); | |
| } | |
| } | |
| }(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment