Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var jqGridBC01020E = '';
- $(document).ready(function() {
- setFieldPattern("houseNo","0011000");
- setFieldPattern("moo","0010000");
- setFieldPattern("agentPhone","0010000");
- setFieldPattern("agentMobile","0010000");
- var actRtn = $("#act").val();
- if (actRtn != 'NEW' && !actRtn.match(/INQ*/) && !actRtn.match(/UPD*/) && !actRtn.match(/ADD*/) && !actRtn.match(/DEL*/)) {
- $("#barSearch").show();
- $("#gridSearch").show();
- $("#form1").hide();
- createGrid();
- } else {
- $("#barSearch").hide();
- $("#gridSearch").hide();
- $("#form1").show();
- $("#DEL").prop("disabled", false);
- $("#UPD").show();
- $("#ADD").hide();
- $("#agentCode").focus();
- if ($("#recStatus").val() == '0') {
- $("#DEL").prop("disabled", true);
- }
- if ($("#act").val() == 'ADDERR') {
- $("#agentCode").focus();
- $("#recStatus").val("");
- $("#updatedBy").val("");
- $("#updatedDate").val("");
- $("#UPD").hide();
- $("#ADD").show();
- $("#DEL").prop("disabled", true);
- } else if ($("#act").val() == 'ADDRTN' || $("#act").val() == 'NEW') {
- $("#UPD").hide();
- $("#DEL").prop("disabled", true);
- $("#ADD").show();
- $("#agentCode").focus();
- }
- }
- setDDList();
- setFormatDate("contactDate", 3);
- $('#contactDate').datepicker({
- language: 'th-th',
- autoclose: true,
- orientation: "auto",
- format: "dd/mm/yyyy"
- });
- setFormatDate("expireDate", 3);
- $('#expireDate').datepicker({
- language: 'th-th',
- autoclose: true,
- orientation: "auto",
- format: "dd/mm/yyyy"
- });
- afterLoad();
- });
- function setDDList()
- {
- //===================== ชุด จังหวัด + อำเภอ + ตำบล =====================//
- //----- จังหวัด
- $("#provinceCode").select2({
- width: "100%",
- placeholder: "กรุณาเลือก",
- allowClear: true
- }).on('change', function(){
- $("#amphurCode").select2("val", "");
- $("#districtCode").select2("val", "");
- if($(this).select2("val").trim() == '')
- {
- $("#amphurCode").select2("readonly", true);
- $("#amphurCodeCtl").val('');
- $("#amphurNameCtl").val('');
- $("#districtCode").select2("readonly", true);
- $("#districtCodeCtl").val('');
- $("#districtNameCtl").val('');
- $("#zipCodeCtl").val('');
- }
- else
- {
- $("#amphurCode").select2("readonly", false);
- $("#amphurCodeCtl").val('');
- $("#amphurNameCtl").val('');
- $("#districtCode").select2("readonly", true);
- $("#districtCodeCtl").val('');
- $("#districtNameCtl").val('');
- $("#zipCodeCtl").val('');
- }
- });
- //----- อำเภอ
- $("#amphurCode").select2({
- ajax: {
- url: "../bc/BCAgentInfoServlet?act=SRHAMPHUR",
- dataType: 'json',
- //term คือค่าที่ Key เข้ามาใน dropDownList
- data: function(term, page) {
- return {
- AmphurName : term
- , ProvinceCode : $("#provinceCode").val()
- };
- },
- results: function(data, page ) {
- alert(data[0].text);
- //var test = [{text:data.objectName, children:[{id:data.objectCode, text:data.objectName}]}];
- return { results: data }
- }
- }
- , allowClear: true
- , placeholder: "กรุณาเลือก"
- , initSelection: function (element, callback) {
- return $.getJSON("../bc/BCAgentInfoServlet?act=SRHAMPHUR", {ProvinceCode : $("#provinceCode").val()}, function(data) {
- amphurId = '';
- amphurName = '';
- if(typeof(data) !== 'undefined' && data.length > 0)
- {
- for(i=0; i<data.length; i++)
- {
- amphurArray = data[i].id.split(":");
- if(amphurArray[0] == element.val())
- {
- amphurId = amphurArray[0];
- amphurName = amphurArray[1];
- break;
- }
- }
- }
- return callback({ id: amphurId, text: amphurName });
- });
- }
- }).on('change', function(){
- $("#districtCode").select2("val", "");
- if($(this).select2("val").trim() == '')
- {
- $("#amphurCodeCtl").val('');
- $("#amphurNameCtl").val('');
- $("#districtCode").select2("readonly", true);
- $("#districtCodeCtl").val('');
- $("#districtNameCtl").val('');
- $("#zipCodeCtl").val('');
- }
- else
- {
- $("#districtCode").select2("readonly", false);
- $("#districtCodeCtl").val('');
- $("#districtNameCtl").val('');
- $("#zipCodeCtl").val('');
- var amphurArray = $(this).select2("val").split(":");
- $("#amphurCodeCtl").val(amphurArray[0]);
- $("#amphurNameCtl").val(amphurArray[1]);
- }
- });
- //----- ตำบล
- $("#districtCode").select2({
- ajax: {
- url: "../bc/BCAgentInfoServlet?act=SRHDISTRICT",
- dataType: 'json',
- //term คือค่าที่ Key เข้ามาใน dropDownList
- data: function(term, page) {
- return {
- DistrictName : term
- , ProvinceCode : $("#provinceCode").val()
- , AmphurCode : $("#amphurCodeCtl").val()
- };
- },
- results: function(data, page ) {
- //alert(data[0].text);
- //var test = [{text:data.objectName, children:[{id:data.objectCode, text:data.objectName}]}];
- return { results: data }
- }
- }
- //, width:'300px'
- //, formatResult: formatList
- , allowClear: true
- , placeholder: "กรุณาเลือก"
- , initSelection: function (element, callback) {
- return $.getJSON("../bc/BCAgentInfoServlet?act=SRHDISTRICT",
- {ProvinceCode : $("#provinceCode").val(), AmphurCode : $("#amphurCodeCtl").val()}
- , function(data) {
- districtId = '';
- districtName = '';
- if(typeof(data) !== 'undefined' && data.length > 0)
- {
- for(i=0; i<data.length; i++)
- {
- districtArray = data[i].id.split(":");
- if(districtArray[0] == element.val()) {districtId = districtArray[0]; districtName = districtArray[1]; break;}
- }
- }
- return callback({ id: districtId, text: districtName });
- });
- }
- }).on('change', function(){
- if($(this).select2("val").trim() == '')
- {
- $("#districtCodeCtl").val('');
- $("#districtNameCtl").val('');
- $("#zipCodeCtl").val('');
- }
- else
- {
- var districtArray = $(this).select2("val").split(":");
- $("#districtCodeCtl").val(districtArray[0]);
- $("#districtNameCtl").val(districtArray[1]);
- $("#zipCodeCtl").val(districtArray[2]);
- }
- });
- if($("#provinceCode").select2("val")=='')
- {
- $("#amphurCode").select2("readonly", true);
- }
- if($("#amphurCode").select2("val")=='')
- {
- $("#districtCode").select2("readonly", true);
- }
- //----- set ค่าตอน Inquiry มาแสดงที่จอภาพ
- if($("#amphurCodeCtl").val() != '')
- {
- $('#amphurCode').select2('data', {id:$("#amphurCodeCtl").val(), text:$("#amphurNameCtl").val()} );
- }
- if($("#districtCodeCtl").val() != '')
- {
- $('#districtCode').select2('data', {id:$("#districtCodeCtl").val(), text:$("#districtNameCtl").val()} );
- }
- }
- // End created by tosspoka 2015-05-03
- function createGrid() {
- jqGridBC01020E = jQuery("#masterGrid");
- //$(function () {
- $('#act').val('SRH'); // Added for serialize
- // Setup grid
- jqGridBC01020E.jqGrid({
- url: "../bc/BCAgentInfoServlet?" + $("#frmBC01020E").serialize(),
- datatype: "json",
- mtype: 'POST',
- colNames: ['', 'รหัสผู้รับจ้าง', 'ชื่อบริษัทผู้รับจ้าง', 'ที่อยู่', 'โทรศัพท์', 'วันที่ทำสัญญา', 'วันที่สิ้นสุดสัญญา', 'สถานะข้อมูล'],
- colModel: [{
- name: 'act',
- index: 'act',
- width: '3%',
- align: 'center',
- sortable: false,
- formatter: function(cellval, options, rowObject) {
- return "<span class=\"glyphicon glyphicon-pencil\" style=\"cursor:pointer;\" onclick=\"getData('" + options.rowId + "');\"><\/span>";
- }
- }, {
- name: 'agentCode',
- index: 'aoBCAgentInfo.agentCode',
- align: 'center',
- editrules: {
- required: true
- },
- editoptions: {
- size: 20,
- maxlength: 20
- },
- editable: true,
- width: '10%'
- }, {
- name: 'agentName',
- index: 'aoBCAgentInfo.agentName',
- align: 'center',
- editrules: {
- required: true
- },
- editoptions: {
- size: 20,
- maxlength: 20
- },
- editable: true,
- width: '10%'
- }, {
- name: 'address',
- index: 'aoBCAgentInfo.address',
- align: 'left',
- width: '32%'
- }, {
- name: 'agentPhone',
- index: 'aoBCAgentInfo.agentPhone',
- align: 'center',
- editoptions: {
- readonly: true,
- size: 20
- },
- width: '10%'
- }, {
- name: 'contactDate',
- index: 'aoBCAgentInfo.contactDateDisplay',
- align: 'right',
- editrules: {
- required: true
- },
- editoptions: {
- size: 20,
- maxlength: 20
- },
- editable: true,
- width: '15%'
- }, {
- name: 'expireDate',
- index: 'aoBCAgentInfo.expireDateDisplay',
- align: 'center',
- width: '10%'
- },{
- name: 'recStatus',
- index: 'aoBCAgentInfo.recStatus',
- align: 'center',
- formatter: formatStatus,
- width: '10%'
- }],
- height: 300,
- width: 700
- ,
- sortable: true
- ,
- multiSort: true
- ,
- autoencode: true,
- pager: "#masterGridPager",
- viewrecords: true // display the number of total records from the query in the pager bar
- ,
- multiselect: true
- ,
- loadComplete: function(data) {
- if (data.rows.length > 0) {
- for (var id = 1; id <= data.rows.length; id++) {
- rowData = jqGridBC01020E.getRowData(id);
- if (rowData.recStatus == null) {
- continue;
- }
- if (rowData.recStatus.indexOf("Inactive") != -1) {
- jQuery("#jqg_masterGrid_" + id).prop('disabled', true);
- }
- }
- }
- if (jqGridBC01020E.getGridParam("reccount") == 0) {
- // count data in grid
- //var htmlString = $("#reloading").html();
- if ($("#reloading").html() != "") {
- $("#reloading").delay(3000).fadeOut();
- $("#reloading").stop();
- $("#reloading").fadeIn(0);
- }
- showMessage("reloading", "warning", "ไม่พบข้อมูลที่ต้องการค้นหา", "");
- } else if (jqGridBC01020E.getGridParam("reccount") != 0) {
- $("#reloading").html("");
- }
- $("#agentCodeSrh").select();
- $("#agentCodeSrh").focus();
- },
- beforeSelectRow: function(rowid, e) {
- if ($("#jqg_masterGrid_" + rowid).prop("disabled")) {
- return false;
- } else {
- return true;
- }
- },
- onSelectAll: function(aRowids, status) {
- if (status) {
- for (var i = 0; i < aRowids.length; i++) {
- if ($("#jqg_masterGrid_" + aRowids[i]).prop("disabled")) {
- $("#jqg_masterGrid_" + aRowids[i]).removeAttr("checked");
- }
- }
- //modify the selarrrow parameter
- jqGridBC01020E[0].p.selarrrow = jqGridBC01020E.find("tr.jqgrow:has(td > input.cbox:checked)")
- .map(function() {
- return this.id;
- }) // convert to set of ids
- .get(); // convert to instance of Array
- }
- }
- });
- setTimeout(function() {
- $("#masterGrid").setGridWidth(parseInt($("#form_body").width()) - 20);
- }, 330);
- $(window).bind('resize', function() {
- $("#masterGrid").setGridWidth($("#form_body").width() - 20);
- }).trigger('resize');
- }
- function showMessage(vaReload, vaType, vaErrMsg, vaFocus)
- {
- var vaAlert = "alert-warning";
- if(vaType == "danger")
- {
- vaAlert = "alert-danger";
- }
- else if(vaType == "success")
- {
- vaAlert = "alert-success";
- }
- $("#"+vaReload).html("<div class='alert " + vaAlert +" alert-dismissable' style='text-align: center; margin-bottom: 5px;'>" + "<button type='button' class='close' data-dismiss='alert' aria-hidden='true' style='padding-top: 7px;'>×<\/button><b>" + vaErrMsg +" <\/b><\/div>");
- $('html, body').animate({ scrollTop: 0 }, 0);
- //setFocus
- if(vaFocus.length > 0)
- {
- if($("#"+vaFocus).prop('tagName') == "SELECT" || $("#"+vaFocus).attr('class').indexOf("select") > -1){
- $("#"+vaFocus).select2("focus");
- }
- else{
- $("#"+vaFocus).focus();
- }
- }
- }//showMessage
- function verify(paAct) {
- if (paAct == 'SRH') {
- searchData();
- } else if (paAct == 'OPNSRH') {
- openBarSearch();
- } else if (paAct == 'ADD') {
- addData();
- } else if (paAct == 'UPD') {
- editData();
- } else if (paAct == 'DEL') {
- confirmDelete();
- } else if (paAct == 'DELMLT') {
- deleteDataMultiple();
- } else if (paAct == 'CLR' || paAct == 'NEW') {
- newData();
- }
- }
- function openBarSearch() {
- $("#barSearch").show();
- $("#gridSearch").show();
- $("#form1").hide();
- if (jqGridBC01020E == '') {
- createGrid();
- }
- }
- function formatStatus(cellvalue, options, rowObject) {
- var desc = '';
- if (cellvalue == '0') {
- desc = '<span style=\'color:red\'>Inactive<\/span>';
- }
- return desc;
- }
- function searchData() {
- $('#act').val('SRH'); // Added for serialize
- jqGridBC01020E.setGridParam({
- url: "../bc/BCAgentInfoServlet?" + $("#frmBC01020E").serialize(),
- page: 1
- });
- jqGridBC01020E.trigger("reloadGrid");
- }
- function deleteData() {
- $("#act").val('DEL');
- $("#frmBC01020E").submit();
- }
- function confirmDelete() {
- $("#modalConfirm").html("คุณต้องการลบข้อมูลนี้หรือไม่");
- $("#modalButton").html("<button type=\"button\" class=\"btn btn-default btn-xs\" data-dismiss=\"modal\"><b> ยกเลิก <\/b><\/button><button type=\"button\" class=\"btn btn-primary btn-xs\" data-dismiss=\"modal\" onclick=\"deleteData();\"><b> ตกลง <\/b><\/button>");
- $('#myModal').modal('show');
- }
- function deleteDataMultiple() {
- var gr = jqGridBC01020E.getGridParam('selarrrow');
- var rowData = '';
- var agentCode = new Array();
- for (var i = 0; i < gr.length; i++) {
- rowData = jqGridBC01020E.getRowData(gr[i]);
- agentCode[i] = rowData.agentCode;
- }
- if (gr != "")
- jqGridBC01020E.delGridRow(gr, {
- url: "../bc/BCAgentInfoServlet",
- delData: {
- act: "DELMLT",
- agentCode: agentCode.toString()
- },
- reloadAfterSubmit: true,
- afterComplete: function(response, postdata, formid) {
- if ($("#reloading").html() != "") {
- $("#reloading").delay(3000).fadeOut();
- $("#reloading").stop();
- $("#reloading").fadeIn(0);
- }
- //$("#reloading").stop();
- showMessage("reloading", "success", "ลบข้อมูลเรียบร้อยแล้ว", "");
- //$("#reloading").html("<div class='alert alert-success alert-dismissable' style='height: 37px; text-align: center; " + "vertical-align:middle; line-height: 30px; padding-top: 3px; margin-bottom: 5px;'>" + "<button type='button' class='close' data-dismiss='alert' aria-hidden='true' style='padding-top: 7px;'>×<\/button><label class='control-label'>" + "ลบข้อมูลเรียบร้อยแล้ว<\/label><\/div>");
- //$("#reloading").fadeIn(0);
- $("body").scrollTop(0);
- //$("#reloading").delay(3000).fadeOut();
- },
- errorTextFormat: function(response) {
- if (response.statusText == "D") {
- return '<span class="ui-icon ui-icon-alert" ' + 'style="float:left; margin-right:.3em;"><\/span>' + "ไม่สามารถลบข้อมูลได้";
- } else {
- return '<span class="ui-icon ui-icon-alert" ' + 'style="float:left; margin-right:.3em;"><\/span>' + response.statusText;
- }
- }
- });
- else {
- $("#modalConfirm").html("กรุณาเลือกข้อมูลที่ต้องการลบ");
- $("#modalButton").html("<button type=\"button\" class=\"btn btn-primary btn-xs\" data-dismiss=\"modal\"><b> ตกลง <\/b>");
- $('#myModal').modal('show');
- }
- }
- function getData(id) {
- rowData = jqGridBC01020E.getRowData(id);
- $("#agentCode").val(rowData.agentCode);
- $("#act").val('INQ');
- $("#frmBC01020E").submit();
- }
- function addData() {
- if (validateData()) {
- $("#act").val('ADD');
- $("#frmBC01020E").submit();
- }
- }
- function editData() {
- if (validateData()) {
- $("#act").val('UPD');
- $("#frmBC01020E").submit();
- }
- }
- function addCommas(y) {
- if (y.indexOf(",")) {
- x = y.replace(/,/g, "");
- } else {
- x = y;
- }
- var a;
- var check = x.split(".");
- if ((x.split(".").length > 1) && check[1]) {
- if (check[1] != "" && check[1] != "0") {
- a = parseFloat(x);
- a = Math.round(a * 100) / 100;
- }
- } else {
- a = x;
- }
- var parts = a.toString().split(".");
- parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
- parts = parts.join(".");
- return parts;
- }
- function validateText(x) {
- x = x.replace(/[0-9]/g, "");
- return x;
- }
- function validateDouble(keyCode) {
- var isShift = false;
- if (keyCode == 16) {
- isShift = true;
- }
- if (((keyCode >= 48 && keyCode <= 57) || keyCode == 8 ||
- keyCode <= 37 || keyCode <= 39 || keyCode == 190 ||
- (keyCode >= 96 && keyCode <= 105)) && isShift == false) {
- return true;
- } else {
- return false;
- }
- }
- function isDate(ExpiryDate) {
- var objDate, // date object initialized from the ExpiryDate string
- mSeconds, // ExpiryDate in milliseconds
- day, // day
- month, // month
- year; // year
- // date length should be 10 characters (no more no less)
- if (ExpiryDate.length !== 10) {
- return false;
- }
- // third and sixth character should be '/'
- if (ExpiryDate.substring(2, 3) !== '/' || ExpiryDate.substring(5, 6) !== '/') {
- return false;
- }
- // extract month, day and year from the ExpiryDate (expected format is mm/dd/yyyy)
- // subtraction will cast variables to integer implicitly (needed
- // for !== comparing)
- day = ExpiryDate.substring(0, 2) - 0;
- month = ExpiryDate.substring(3, 5) - 1; // because months in JS start from 0
- year = ExpiryDate.substring(6, 10) - 0;
- // test year range
- if (year < 1000 || year > 3000) {
- return false;
- }
- // convert ExpiryDate to milliseconds
- mSeconds = (new Date(year, month, day)).getTime();
- // initialize Date() object from calculated milliseconds
- objDate = new Date();
- objDate.setTime(mSeconds);
- // compare input date and parts from Date() object
- // if difference exists then date isn't valid
- if (objDate.getFullYear() !== year ||
- objDate.getMonth() !== month ||
- objDate.getDate() !== day) {
- return false;
- }
- // otherwise return true
- return true;
- }
- function validateDate(valDate) {
- var ExpiryDate = valDate;
- // check date and print message
- if (isDate(ExpiryDate)) {
- return true;
- } else {
- return false;
- }
- }
- function validateData() {
- var vbRsl = false;
- var vaErrMsg = "กรุณาระบุ";
- var vaFocus = "";
- $("#agentCode").removeClass("has-error");
- $("#agentName").removeClass("has-error");
- $("#startDateDiv").removeClass("has-error");
- $("#finishDateDiv").removeClass("has-error");
- if ($("#agentCode").val() == '') {
- if (vaErrMsg != "กรุณาระบุ") vaErrMsg = vaErrMsg + ",";
- vaErrMsg = vaErrMsg + " รหัสพัสดุสาขา";
- if (vaFocus == "") {
- vaFocus = "agentCode";
- }
- $("#agentCodeDiv").addClass("has-error");
- }
- if (validateDate($("#contactDate").val()) == false && $("#contactDate").val() != '') {
- if (vaErrMsg != "กรุณาระบุ") vaErrMsg = vaErrMsg + ",";
- vaErrMsg = vaErrMsg + " วันที่ทำสัญญาให้ถูกต้อง";
- if (vaFocus == "") {
- vaFocus = "contactDate";
- }
- $("#contactDateDiv").addClass("has-error");
- }
- if (validateDate($("#expireDate").val()) == false && $("#expireDate").val() != '') {
- if (vaErrMsg != "กรุณาระบุ") vaErrMsg = vaErrMsg + ",";
- vaErrMsg = vaErrMsg + " วันที่สิ้นสุดสัญญาให้ถูกต้อง";
- if (vaFocus == "") {
- vaFocus = "expireDate";
- }
- $("#expireDateDiv").addClass("has-error");
- }
- if ($("#contactDate").val() != '' && $("#expireDate").val() != '') {
- var vaDteStr = $("#contactDate").val();
- var vaDteFin = $("#expireDate").val();
- var vnDateDif = calDateDif(vaDteFin, vaDteStr, true);
- if (vnDateDif >= 0) {
- if (vaErrMsg != "กรุณาระบุ") vaErrMsg = vaErrMsg + ",";
- vaErrMsg = vaErrMsg + " วันที่สิ้นสุดสัญญา มากกว่า วันที่ทำสัญญา";
- if (vaFocus == "") {
- vaFocus = "expireDate";
- }
- $("#expireDateDiv").addClass("has-error");
- }
- }
- if (vaErrMsg != 'กรุณาระบุ') {
- showMessage("reloading", "warning", vaErrMsg, vaFocus);
- } else {
- vbRsl = true;
- }
- return vbRsl;
- }
- function newData() {
- $("#act").val('NEW');
- $("#frmBC01020E").submit();
- }
- function getConfirm(confirmMessage, callback) {
- confirmMessage = confirmMessage || '';
- $('#confirmbox').modal({
- show: true,
- backdrop: false,
- keyboard: false
- });
- $('#confirmMessage').html(confirmMessage);
- $('#confirmFalse').click(function() {
- $('#confirmbox').modal('hide');
- if (callback) callback(false);
- });
- $('#confirmTrue').click(function() {
- $('#confirmbox').modal('hide');
- if (callback) callback(true);
- });
- }
- // Description : Calculate two date and return value date2 - date1
- // Parameter : date1 = first date format dd/mm/yyyy
- // date2 = second date format dd/mm/yyyy
- // typeEra = true : Buddish Era and false : Cristian Era
- function calDateDif(date1, date2, typeEra) {
- var datePat = /^(\d{2})\/(\d{2})(\/(\d{4}))?$/;
- var matchArray1 = null;
- var matchArray2 = null;
- if (typeEra == undefined) {
- typeEra = true;
- }
- if (date1 != '' && date1 != null && date2 != '' && date2 != null) {
- matchArray1 = date1.match(datePat);
- matchArray2 = date2.match(datePat);
- if (matchArray1 == null || matchArray2 == null) {
- alert("ไม่สามารถเปรียบเทียบค่าได้ เพราะ รูปแบบไม่ถูกต้อง");
- }
- else {
- day1 = matchArray1[1];
- month1 = matchArray1[2];
- year1 = matchArray1[4];
- day2 = matchArray2[1];
- month2 = matchArray2[2];
- year2 = matchArray2[4];
- if (typeEra) {
- year1 = year1 - 543;
- year2 = year2 - 543;
- }
- var one_day = 1000 * 60 * 60 * 24
- return ((Date.parse(eval(month2) + "/" + day2 + "/" + year2) - Date.parse(eval(month1) + "/" + day1 + "/" + year1))) / (one_day);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment