Advertisement
CordSac

KendoGrid

Apr 8th, 2020
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function InitProductServicesGrid() {
  2.     var prodServiceDataSource = new kendo.data.DataSource({
  3.         transport: {
  4.             type: "json",
  5.             read:
  6.                 {
  7.                     url: SERVER_PATH + "/LTSService/ProductsService.asmx/GetProductServiceDetailsList",
  8.                     type: "POST",
  9.                     contentType: 'application/json',
  10.                     data: GetAdditonalData,
  11.                     datatype: "json"
  12.                 },
  13.             create:
  14.             {
  15.                 url: SERVER_PATH + "/LTSService/ProductsService.asmx/SaveProductService",
  16.                 type: "POST",
  17.                 contentType: 'application/json',
  18.                 datatype: "json"
  19.             },
  20.             update:
  21.             {
  22.                 url: SERVER_PATH + "/LTSService/ProductsService.asmx/SaveProductService",
  23.                 type: "POST",
  24.                 contentType: 'application/json',
  25.                 datatype: "json"
  26.             },
  27.             destroy:
  28.             {
  29.                 url: SERVER_PATH + "/LTSService/ProductsService.asmx/DeleteProductService",
  30.                 type: "POST",
  31.                 contentType: 'application/json',
  32.                 datatype: "json"
  33.             },
  34.             parameterMap: function (options, operations) {
  35.                 if (operations !== "read" && options !== null && options !== undefined) {
  36.                     var productService = {};
  37.                     if (operations === "destroy") {
  38.                         productService = { "productService": options, "serviceIdToReplaceWith": null };
  39.                     } else {
  40.                         productService = { "productService": options };
  41.                     }
  42.                     return JSON.stringify(productService);
  43.                 }
  44.                 else {
  45.                     // fix length to page size
  46.                     options.pageSize = 500;
  47.                     if (options.sort !== null && options.sort !== undefined && options.sort !== "" && options.sort.length > 0) {
  48.                         options.sortColumn = options.sort[0].field;
  49.                         options.direction = options.sort[0].dir;
  50.                     }
  51.                     else {
  52.                         options.sortColumn = "";
  53.                         options.direction = "";
  54.                     }
  55.                     return JSON.stringify(options);
  56.                 }
  57.             }
  58.         },
  59.         schema: {
  60.             data: function (result) {
  61.                 return JSON.parse(result.d);
  62.             },
  63.             total: function (result) {
  64.                 if (result !== undefined && result !== '' && result !== null && result.d != "[]" && result.d !== "null") {
  65.                     return JSON.parse(result.d)[0].RowCount;
  66.                 }
  67.                 else {
  68.                     return 0;
  69.                 }
  70.             },
  71.             model: {
  72.                 id: "Id",
  73.                 fields: {
  74.                     Id: { type: "int" },
  75.                     CategoryId: { type: "string" },
  76.                     ServiceTypeId: { type: "string" },
  77.                     ServiceName: { type: "string" },
  78.                     ServiceCode: { type: "string" },
  79.                     ShowInMemberLeft: { type: "boolean" },
  80.                     ShowInWrapUp: { type: "boolean" },
  81.                     ShowInPhoneCenter: { type: "boolean" },
  82.                     ShowInKiosk: { type: "boolean" },
  83.                     ServiceTime: { type: "string" },
  84.                     CategoryName: { type: "string" },
  85.                     ServiceTypeName: { type: "string" },
  86.                     IsActive: { type: "boolean"}
  87.                 }
  88.             }
  89.         },
  90.         requestEnd: function (e) {
  91.             if (e.type === "destroy") {
  92.                 var grid = $("#productServicesGrid").data("kendoGrid");
  93.                 grid.dataSource.read();
  94.             }
  95.         },
  96.         error: function (e) {
  97.             e.preventDefault();
  98.             if (e.xhr !== undefined && e.xhr !== null) {
  99.                 var messageBody = e.xhr.responseJSON.Message;
  100.                 ShowGritterMessage("Errors", messageBody, false, '../App_Themes/Default/LtsImages/errorMessageIcon_large.png');
  101.                 var grid = $("#productServicesGrid").data("kendoGrid");
  102.                 grid.cancelChanges();
  103.             }
  104.         },
  105.         pageSize: 20,
  106.     });
  107.  
  108.     $("#productServicesGrid").kendoGrid({
  109.         dataSource: prodServiceDataSource,
  110.         sortable: true,
  111.         filterable: false,
  112.         pageable: true,
  113.         dataBound: gridDataBound,
  114.         editable: {
  115.             mode: "inline",
  116.             confirmation: false
  117.         },
  118.         columns: [
  119.             { field: "Id", title: "", hidden: true },
  120.             {
  121.                 field: "ServiceName",
  122.                 title: "Product/Service",
  123.                 sortable: true,
  124.                 editor: function (container) {
  125.                     var serviceTxtBox = RnderProductService();
  126.                     $(serviceTxtBox).appendTo(container);
  127.                 },
  128.                 headerTemplate: '<a class="k-link" href="#" title="Product/Service">Product/Service</a>',
  129.                 width: '25%'
  130.             },
  131.             {
  132.                 field: "ServiceCode",
  133.                 title: "Code",
  134.                 sortable: true,
  135.                 editor: function (container) {
  136.                     var serviceCode = RenderServiceCode();
  137.                     $(serviceCode).appendTo(container);
  138.                 }
  139.             },
  140.             {
  141.                 field: "CategoryName",
  142.                 title: "Category",
  143.                 sortable: true,
  144.                 template: "#=RenderCategoryTemplate(CategoryId,CategoryName)#",
  145.                 editor: function (container, options) {
  146.                     var categoryDropDown = RenderCategoryName(options.model.CategoryId);
  147.                     $(categoryDropDown).appendTo(container);
  148.                 }
  149.             },
  150.             {
  151.                 field: "ServiceTypeName",
  152.                 title: "Type",
  153.                 sortable: true,
  154.                 template: "#=RenderServiceTypesTemplate(ServiceTypeId, ServiceTypeName)#",
  155.                 editor: function (container, options) {
  156.                     var typeDropDown = RenderServiceTypes(options.model.ServiceTypeId);
  157.                     $(typeDropDown).appendTo(container);
  158.                 }
  159.             },
  160.             {
  161.                 field: "ShowInKiosk",
  162.                 title: "Show In Kiosk",
  163.                 sortable: false,
  164.                 template: "#=RenderYesNoTemplate(ShowInKiosk)#",
  165.                 editor: function (container, options) {
  166.                     var kioskChkBox = RenderCheckBox(options.field);
  167.                     $(kioskChkBox).appendTo(container);
  168.                 },
  169.                 headerTemplate: '<a class="k-link" href="#" title="Show In Kiosk">Show In Kiosk</a>'
  170.             },
  171.             {
  172.                 field: "ShowInMemberLeft",
  173.                 title: "Show In Left W/O Service",
  174.                 sortable: false,
  175.                 template: "#=RenderYesNoTemplate(ShowInMemberLeft)#",
  176.                 editor: function (container, options) {
  177.                     var showMemberChkBox = RenderCheckBox(options.field);
  178.                     $(showMemberChkBox).appendTo(container);
  179.                 },
  180.                 headerTemplate: '<a class="k-link" href="#" title="Show In Left W/O Service">Show In Left W/O Service</a>'
  181.             },
  182.             {
  183.                 field: "ShowInWrapUp",
  184.                 title: "Show In Wrap-Up",
  185.                 sortable: false,
  186.                 template: "#=RenderYesNoTemplate(ShowInWrapUp)#",
  187.                 editor: function (container, options) {
  188.                     var showInWrapUpChkBox = RenderCheckBox(options.field);
  189.                     $(showInWrapUpChkBox).appendTo(container);
  190.                 },
  191.                 headerTemplate: '<a class="k-link" href="#" title="Show In Wrap-Up">Show In Wrap-Up</a>'
  192.             },
  193.             {
  194.                 field: "ServiceTime",
  195.                 title: "Time Standard",
  196.                 sortable: false,
  197.                 editor: function (container, options) {
  198.                     var serviceTimeTxtBox = RenderServiceTime();
  199.                     $(serviceTimeTxtBox).appendTo(container);
  200.  
  201.                 },
  202.                 headerTemplate: '<a class="k-link" href="#" title="Time Standard">Time Standard</a>'
  203.             },
  204.             {
  205.                 title: "Action", command: [
  206.                     {
  207.                         name: 'startEdit',
  208.                         click: startEdit,
  209.                         template: "<a title='Edit' class='k-grid-startEdit k-button'><span class='k-icon k-i-edit'></span></a><a title='Update' class='k-button k-button-icontext k-primary k-grid-update' style='display:none;'><span class='k-icon k-i-check'></span></a>"
  210.                     },
  211.                     {
  212.                         name: "deleteRow",
  213.                         click: deleteRow,
  214.                         template: "<a title='Delete' class='k-grid-deleteRow k-button'><span class='k-icon k-i-delete'></span></a><a title='Cancel' class='k-button k-button-icontext k-grid-cancel' style='display:none;'><span class='k-icon k-i-cancel'></span></a>"
  215.                     }
  216.                     ,
  217.                     {
  218.                         name: "hideRow",
  219.                         click: hideRow,
  220.                         template: function (dataItem) {
  221.                             const isActive = dataItem.isActive;
  222.                             console.log('Value: ',isActive);
  223.                             return `<a title=${isActive ? "Hide" : "Show"} class="k-grid-hideRow k-button"><span class="k-icon k-i-${isActive ? 'lock' : 'unlock'}"></span></a>`
  224.                         }
  225.                     }
  226.                     ],
  227.                 width: "120px"
  228.             }
  229.         ],
  230.         save: function (e) {
  231.             var errors = ValidateProductService(e.model);
  232.             if (errors.length > 0) {
  233.                 e.preventDefault();
  234.                 var messageBody = GenerateMsgBody(errors);
  235.                 ShowGritterMessage("Errors", messageBody, false, '../App_Themes/Default/LtsImages/errorMessageIcon_large.png');
  236.             }
  237.         },
  238.         edit: function (e) {
  239.             $(e.container).find('.k-grid-update').show().end().find('.k-grid-startEdit').hide().end().find('.k-grid-cancel').show().end().find('.k-grid-deleteRow').hide();
  240.         },
  241.         cancel: function (e) {
  242.             setTimeout(function () {
  243.                 if (LOWEST_GROUP_LEVEL > 0) {
  244.                     // though this is a redundant grid initialization,
  245.                     // there's a limitation on the kendo grid with respect to the behavior of hidden buttons when cancelling a record.
  246.                     // this solution was suggested here by the Telerik team
  247.                     // https://www.telerik.com/forums/hide-edit-and-delete-button-based-on-the-status-of-each-record
  248.                     $("#productServicesGrid").data("kendoGrid").trigger("dataBound");
  249.                 }
  250.             });
  251.         }
  252.     });
  253.  
  254.     var grid = $("#productServicesGrid").data("kendoGrid");
  255.     $("button#addProductServicesTop, button#addProductServicesBottom").click(function () {
  256.         grid.addRow();
  257.     });
  258. }
  259.  
  260.  
  261. function gridDataBound(e) {
  262.     var rows = e.sender.tbody.children();
  263.     var ss = e.sender.tbody.find(".k-grid-hideRow");
  264.     var currentPage = e.sender.dataSource.page();
  265.     if (rows.length == 0) {
  266.         if (currentPage > 1) {
  267.             this.dataSource.page(currentPage - 1);
  268.         }
  269.     }
  270.  
  271.     e.sender.tbody.find(".k-grid-hideRow").click(function (evt) {
  272.         const row = $(evt.target).closest("tr");
  273.         const dataItem = e.sender.dataItem(row);
  274.         dataItem.set("isActive", !dataItem.isActive);
  275.     });
  276.  
  277. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement