Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var controller_name = "consumptions";
- var table_id = "consumptions";
- var editor; // use a global for the submit and return data rendering in the examples
- $(document).ready(function() {
- openFilters();
- year = filters.year==undefined?new Date().getFullYear():filters.year;
- if(year != undefined) {
- editor = new $.fn.dataTable.Editor({
- ajax: {
- edit: {
- type: 'POST',
- url: "/" + controller_name +"/" + customer.id,
- contentType: "application/json",
- data: function (data) {
- //var editor = table.row(focused[0][0].row).data();
- var parent_row_id = $(focused.node()).parents("tr").attr("id");
- data.mirror_customer_id = parent_row_id.replace("row_" + customer.id + "_", "").replace("row_", "");
- data.customer_id = customer.id;
- data.is_contrasted = false;
- return JSON.stringify(data);
- }
- }
- },
- table: $('#' + table_id),
- fields: [
- {
- name: "1_" + year
- },
- {
- name: "2_" + year
- },
- {
- name: "3_" + year
- },
- {
- name: "4_" + year
- },
- {
- name: "5_" + year
- },
- {
- name: "6_" + year
- },
- {
- name: "7_" + year
- },
- {
- name: "8_" + year
- },
- {
- name: "9_" + year
- },
- {
- name: "10_" + year
- },
- {
- name: "11_" + year
- },
- {
- name: "12_" + year
- }
- ],
- idSrc: 'id',
- formOptions: {
- inline: {
- onBlur: 'submit',
- drawType: 'none'
- }
- }
- });
- $( 'input', editor.node() ).on( 'focus', function () {
- this.select();
- } );
- var focused;
- editor.on( 'open', function ( e, mode, action ) {
- focused = table.cell({focused: true});
- var current_now_number = focused[0][0].row;
- if ( mode === 'inline' ) {
- editor.on( 'postSubmit.editorInline', function (e, json, data, action, xhr) {
- this.close();
- if ( focused.any() ) {
- var next = $(focused.node()).next();
- if ( next.length ) {
- editor.one( 'submitComplete', function () {
- table.cell( next ).focus();
- } );
- }
- }
- table.row(current_now_number).data(json.data);
- table.row(current_now_number).node().className = "highlight";
- var current_row = $(table.row(current_now_number).node());
- setTimeout(function(){ current_row.addClass("noHighlight").removeClass("highlight");
- setTimeout(function(){ current_row.removeClass("noHighlight")},550)},500);
- } );
- }
- } );
- editor.on( 'close', function () {
- editor.off( 'postSubmit.editorInline' );
- } );
- var firstLoad = true;
- var table = $('#' + table_id).DataTable({
- "ajax": {
- "type": "GET",
- "url": "/" + controller_name +"/" + customer.id,
- "dataSrc": function (json) {
- totalFooter = json.totalFooter;
- return json.data;
- }
- },
- "rowId" : "id",
- "dom": 'Bfrtip',
- "processing": true,
- "searching": false,
- "paging": false,
- "info": false,
- fixedHeader: {
- header: true,
- headerOffset: $('.page-header.navbar').height()
- },
- "serverSide": true,
- keys: {
- columns: [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13],
- keys: [ 9 ],
- editor: editor,
- editOnFocus: true
- },
- select: {
- style: 'os',
- selector: 'td:first-child',
- blurable: true
- },
- "fnServerParams": function (aoData) {
- aoData = prepareDefaultFilters(table_id, aoData);
- firstLoad = false;
- },
- "createdRow": function (row, data, dataIndex) {
- $(row).addClass("customer" + data.id);
- $(row).attr("id", data.id);
- },
- "footerCallback": function (row, data, start, end, display) {
- var api = this.api();
- $(api.column(2).footer()).html(roundNumber(totalFooter[1], 2));
- $(api.column(3).footer()).html(roundNumber(totalFooter[2], 2));
- $(api.column(4).footer()).html(roundNumber(totalFooter[3], 2));
- $(api.column(5).footer()).html(roundNumber(totalFooter[4], 2));
- $(api.column(6).footer()).html(roundNumber(totalFooter[5], 2));
- $(api.column(7).footer()).html(roundNumber(totalFooter[6], 2));
- $(api.column(8).footer()).html(roundNumber(totalFooter[7], 2));
- $(api.column(9).footer()).html(roundNumber(totalFooter[8], 2));
- $(api.column(10).footer()).html(roundNumber(totalFooter[9], 2));
- $(api.column(11).footer()).html(roundNumber(totalFooter[10], 2));
- $(api.column(12).footer()).html(roundNumber(totalFooter[11], 2));
- $(api.column(13).footer()).html(roundNumber(totalFooter[12], 2));
- $(api.column(14).footer()).html(roundNumber(totalFooter[13], 2));
- },
- "footerCallback": function (row, data, start, end, display) {
- var api = this.api();
- $(api.column(2).footer()).html(roundNumber(totalFooter[1], 2));
- $(api.column(3).footer()).html(roundNumber(totalFooter[2], 2));
- $(api.column(4).footer()).html(roundNumber(totalFooter[3], 2));
- $(api.column(5).footer()).html(roundNumber(totalFooter[4], 2));
- $(api.column(6).footer()).html(roundNumber(totalFooter[5], 2));
- $(api.column(7).footer()).html(roundNumber(totalFooter[6], 2));
- $(api.column(8).footer()).html(roundNumber(totalFooter[7], 2));
- $(api.column(9).footer()).html(roundNumber(totalFooter[8], 2));
- $(api.column(10).footer()).html(roundNumber(totalFooter[9], 2));
- $(api.column(11).footer()).html(roundNumber(totalFooter[10], 2));
- $(api.column(12).footer()).html(roundNumber(totalFooter[11], 2));
- $(api.column(13).footer()).html(roundNumber(totalFooter[12], 2));
- $(api.column(14).footer()).html(roundNumber(totalFooter[13], 2));
- },
- columns: [
- {"data": "id", "visible": false},
- {"data": "commercial_name", "render": function (data, type, full, meta){
- return data + '<a class="putZero" style="float:right" href="#"><i class="far fa-check-square"></i></a>';
- }
- },
- {
- "data": "1_" + year, "className": "editable month1 text-right",
- "createdCell": function (td, cellData, full, row, col) {
- if(!full.enabled[1]){
- $(td).addClass('disabled');
- }
- }
- },
- {
- "data": "2_" + year, "className": "editable month2 text-right",
- "createdCell": function (td, cellData, full, row, col) {
- if(!full.enabled[2]){
- $(td).addClass('disabled');
- }
- }
- },
- {
- "data": "3_" + year, "className": "editable month3 text-right",
- "createdCell": function (td, cellData, full, row, col) {
- if(!full.enabled[3]){
- $(td).addClass('disabled');
- }
- }
- },
- {
- "data": "4_" + year, "className": "editable month4 text-right",
- "createdCell": function (td, cellData, full, row, col) {
- if(!full.enabled[4]){
- $(td).addClass('disabled');
- }
- }
- },
- {
- "data": "5_" + year, "className": "editable month5 text-right",
- "createdCell": function (td, cellData, full, row, col) {
- if(!full.enabled[5]){
- $(td).addClass('disabled');
- }
- }
- },
- {
- "data": "6_" + year, "className": "editable month6 text-right",
- "createdCell": function (td, cellData, full, row, col) {
- if(!full.enabled[6]){
- $(td).addClass('disabled');
- }
- }
- },
- {
- "data": "7_" + year, "className": "editable month7 text-right",
- "createdCell": function (td, cellData, full, row, col) {
- if(!full.enabled[7]){
- $(td).addClass('disabled');
- }
- }
- },
- {
- "data": "8_" + year, "className": "editable month8 text-right",
- "createdCell": function (td, cellData, full, row, col) {
- if(!full.enabled[8]){
- $(td).addClass('disabled');
- }
- }
- },
- {
- "data": "9_" + year, "className": "editable month9 text-right",
- "createdCell": function (td, cellData, full, row, col) {
- if(!full.enabled[9]){
- $(td).addClass('disabled');
- }
- }
- },
- {
- "data": "10_" + year, "className": "editable month10 text-right",
- "createdCell": function (td, cellData, full, row, col) {
- if(!full.enabled[10]){
- $(td).addClass('disabled');
- }
- }
- },
- {
- "data": "11_" + year, "className": "editable month11 text-right",
- "createdCell": function (td, cellData, full, row, col) {
- if(!full.enabled[11]){
- $(td).addClass('disabled');
- }
- }
- },
- {
- "data": "12_" + year, "className": "editable month12 text-right",
- "createdCell": function (td, cellData, full, row, col) {
- if(!full.enabled[12]){
- $(td).addClass('disabled');
- }
- }
- },
- {
- "data": "total", "className": "text-right"
- }
- ]
- });
- attachRemoveEvent(table);
- }
- loadFilters(table_id);
- $('#consumptions').on( 'click', '.putZero',function (evt) {
- evt.preventDefault();
- evt.stopPropagation();
- var current_row = $(this).parents("tr").attr("id");
- var current_data = table.row("#" + current_row.replace("row_", "")).data();
- var customer_sales_id = current_row.replace("row_", "");
- var parent_row = $(this).parents("tr");
- for(var i = 1; i < 13; i++){
- var current_cell = parent_row.find(".month" + i);
- if(current_data[i + "_" + year] == null && !$(current_cell).hasClass("disabled"))
- current_data[i + "_" + year] = 0;
- }
- current_data.year = year;
- current_data.customer_sales_id = customer_sales_id;
- current_data.customer_id = customer.id;
- current_data.is_contrasted = false;
- table.row("#" + current_row.replace("row_", "")).data(current_data);
- $.ajax({
- url: "/consumptions/batchUpdate",
- type: 'post',
- data: current_data,
- dataType: 'json',
- success: function (data) {
- table.row("#" + current_row.replace("row_", "")).invalidate();
- }
- });
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment