AbidMufasa

User.js

Jul 30th, 2017
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Load Data in Table when documents is ready  
  2. $(document).ready(function () {
  3.     loadData();
  4. });
  5.  
  6. function ShowApi(){
  7.     $.ajax({
  8.         url: "api/User",
  9.         type: "GET",
  10.         success: function (data) {
  11.             alert(data[0]);
  12.         }
  13.     });
  14. }
  15.  
  16. function loadData() {
  17.     $.ajax({
  18.         url: "http://localhost:50167/api/User",
  19.         type: "GET",
  20.         contentType: "application/json;charset=utf-8",
  21.         dataType: "json",
  22.         success: function (result) {
  23.             var html = '';
  24.             $.each(result, function (key, item) {
  25.                 html += '<tr>';
  26.                 html += '<td>' + item.UserId + '</td>';
  27.                 html += '<td>' + item.UserName + '</td>';
  28.                 html += '<td>' + item.Password + '</td>';
  29.                 html += '<td>' + item.IsActive + '</td>';
  30.                 html += '<td><a href="#" onclick="return getbyID(' + item.UserId + ')">Edit</a> | <a href="#" onclick="Delele(' + item.UserId + ')">Delete</a></td>';
  31.                 html += '</tr>';
  32.             });
  33.             $('.tbody').html(html);
  34.         },
  35.         error: function (errormessage) {
  36.             alert(errormessage.responseText);
  37.         }
  38.     });
  39. }
  40.  
  41. function Add() {
  42.     //var res = validate();
  43.     //if (res == false) {
  44.     //    return false;
  45.     //}
  46.     var usrObj = {
  47.         UserId: $('#UserId').val(),
  48.         UserName: $('#UserName').val(),
  49.         Password: $('#Password').val(),
  50.         IsActive: $('#IsActive').val()
  51.     };
  52.     $.ajax({
  53.         url: "http://localhost:50167/api/User",
  54.         data: JSON.stringify(usrObj),
  55.         type: "POST",
  56.         contentType: "application/json;charset=utf-8",
  57.         dataType: "json",
  58.         success: function (result) {
  59.             loadData();
  60.             $('#myModal').modal('hide');
  61.         },
  62.         error: function (errormessage) {
  63.             alert(errormessage.responseText);
  64.         }
  65.     });
  66. }
  67.  
  68. //Function for getting the Data Based upon User ID
  69. function getbyID(UsrID) {
  70.     $('#Name').css('border-color', 'lightgrey');
  71.     $('#Age').css('border-color', 'lightgrey');
  72.     $('#State').css('border-color', 'lightgrey');
  73.     $('#Country').css('border-color', 'lightgrey');
  74.     $.ajax({
  75.         url: "http://localhost:50167/api/User/" + UsrID,
  76.         typr: "GET",
  77.         contentType: "application/json;charset=UTF-8",
  78.         dataType: "json",
  79.         success: function (result) {
  80.             $('#UserId').val(result.UserId);
  81.             $('#UserName').val(result.Name);
  82.             $('#Password').val(result.Age);
  83.             $('#IsActive').val(result.State);
  84.  
  85.             $('#myModal').modal('show');
  86.             $('#btnUpdate').show();
  87.             $('#btnAdd').hide();
  88.         },
  89.         error: function (errormessage) {
  90.             alert(errormessage.responseText);
  91.         }
  92.     });
  93.     return false;
  94. }
  95.  
  96. //function for updating User's record
  97. function Update() {
  98.     var res = validate();
  99.     if (res === false) {
  100.         return false;
  101.     }
  102.     var usrObj = {
  103.         UserId: $('#UserId').val(),
  104.         UserName: $('#UserName').val(),
  105.         Password: $('#Password').val(),
  106.         IsActive: $('#IsActive').val()
  107.        
  108.     };
  109.     $.ajax({
  110.         url: "http://localhost:50167/api/User",
  111.         data: JSON.stringify(usrObj),
  112.         type: "POST",
  113.         contentType: "application/json;charset=utf-8",
  114.         dataType: "json",
  115.         success: function (result) {
  116.             loadData();
  117.             $('#myModal').modal('hide');
  118.             $('#UserId').val("");
  119.             $('#UserName').val("");
  120.             $('#Password').val("");
  121.             $('#IsActive').val("");
  122.         },
  123.         error: function (errormessage) {
  124.             alert(errormessage.responseText);
  125.         }
  126.     });
  127. }
  128.  
  129. //function for deleting User's record
  130. function Delele(id) {
  131.     var ans = confirm("Are you sure you want to delete this Record?");
  132.     if (ans) {
  133.         $.ajax({
  134.             url: "http://localhost:50167/api/User/" + id,
  135.             type: "DELETE",
  136.             contentType: "application/json;charset=UTF-8",
  137.             dataType: "json",
  138.             success: function (result) {
  139.                 loadData();
  140.             },
  141.             error: function (errormessage) {
  142.                 alert(errormessage.responseText);
  143.             }
  144.         });
  145.     }
  146. }
  147.  
  148. //Function for clearing the textboxes  
  149. function clearTextBox() {
  150.     $('#UserId').val("");
  151.     $('#UserName').val("");
  152.     $('#Password').val("");
  153.     $('#IsActive').val("");
  154.     $('#btnUpdate').hide();
  155.     $('#btnAdd').show();
  156.     $('#UserName').css('border-color', 'lightgrey');
  157.     $('#Password').css('border-color', 'lightgrey');
  158.     $('#IsActive').css('border-color', 'lightgrey');
  159. }
  160. //Valdidation using jquery  
  161. function validate() {
  162.     var isValid = true;
  163.     if ($('#UserName').val().trim() === "") {
  164.         $('#UserName').css('border-color', 'Red');
  165.         isValid = false;
  166.     }
  167.     else {
  168.         $('#UserName').css('border-color', 'lightgrey');
  169.     }
  170.     if ($('#Password').val().trim() === "") {
  171.         $('#Password').css('border-color', 'Red');
  172.         isValid = false;
  173.     }
  174.     else {
  175.         $('#Age').css('border-color', 'lightgrey');
  176.     }
  177.     if ($('#IsActive').val().trim() === "") {
  178.         $('#IsActive').css('border-color', 'Red');
  179.         isValid = false;
  180.     }
  181.     else {
  182.         $('#IsActive').css('border-color', 'lightgrey');
  183.     }
  184.     return isValid;
  185. }
Add Comment
Please, Sign In to add comment