Posted By

steho on 01/21/13


Tagged

javascript jquery x-editable


Versions (?)

Code pieces for x-editable


 / Published in: JavaScript
 

Code snippets from the x-editable source code

  1. $(function(){
  2.  
  3. //defaults
  4. $.fn.editable.defaults.url = '/post';
  5.  
  6. //enable / disable
  7. $('#enable').click(function() {
  8. $('#user .editable').editable('toggleDisabled');
  9. });
  10.  
  11. //editables
  12. $('#username').editable({
  13. url: '/post',
  14. type: 'text',
  15. pk: 1,
  16. name: 'username',
  17. title: 'Enter username'
  18. });
  19.  
  20. $('#firstname').editable({
  21. validate: function(value) {
  22. if($.trim(value) == '') return 'This field is required';
  23. }
  24. });
  25.  
  26. $('#sex').editable({
  27. prepend: "not selected",
  28. source: [
  29. {value: 1, text: 'Male'},
  30. {value: 2, text: 'Female'}
  31. ],
  32. display: function(value, sourceData) {
  33. var colors = {"": "gray", 1: "green", 2: "blue"},
  34. elem = $.grep(sourceData, function(o){return o.value == value;});
  35.  
  36. if(elem.length) {
  37. $(this).text(elem[0].text).css("color", colors[value]);
  38. } else {
  39. $(this).empty();
  40. }
  41. }
  42. });
  43.  
  44. $('#status').editable();
  45.  
  46. $('#group').editable({
  47. showbuttons: false
  48. });
  49.  
  50. $('#vacation').editable();
  51.  
  52. $('#dob').editable();
  53.  
  54. $('#event').editable({
  55. placement: 'right',
  56. combodate: {
  57. firstItem: 'name'
  58. }
  59. });
  60.  
  61. $('#comments').editable({
  62. showbuttons: true
  63. });
  64.  
  65. $('#note').editable();
  66. $('#pencil').click(function(e) {
  67. e.stopPropagation();
  68. e.preventDefault();
  69. $('#note').editable('toggle');
  70. });
  71.  
  72. $('#state').editable({
  73. source: ["Alabama","Alaska","Arizona","Arkansas","California","Colorado","Connecticut","Delaware","Florida","Georgia","Hawaii","Idaho","Illinois","Indiana","Iowa","Kansas","Kentucky","Louisiana","Maine","Maryland","Massachusetts","Michigan","Minnesota","Mississippi","Missouri","Montana","Nebraska","Nevada","New Hampshire","New Jersey","New Mexico","New York","North Dakota","North Carolina","Ohio","Oklahoma","Oregon","Pennsylvania","Rhode Island","South Carolina","South Dakota","Tennessee","Texas","Utah","Vermont","Virginia","Washington","West Virginia","Wisconsin","Wyoming"]
  74. });
  75.  
  76. $('#fruits').editable({
  77. pk: 1,
  78. limit: 3,
  79. source: [
  80. {value: 1, text: 'banana'},
  81. {value: 2, text: 'peach'},
  82. {value: 3, text: 'apple'},
  83. {value: 4, text: 'watermelon'},
  84. {value: 5, text: 'orange'}
  85. ]
  86. });
  87.  
  88. $('#tags').editable({
  89. inputclass: 'input-large',
  90. select2: {
  91. tags: ['html', 'javascript', 'css', 'ajax'],
  92. tokenSeparators: [",", " "]
  93. }
  94. });
  95.  
  96. var countries = [];
  97. $.each({"BD": "Bangladesh", "BE": "Belgium", "BF": "Burkina Faso", "BG": "Bulgaria", "BA": "Bosnia and Herzegovina", "BB": "Barbados", "WF": "Wallis and Futuna", "BL": "Saint Bartelemey", "BM": "Bermuda", "BN": "Brunei Darussalam", "BO": "Bolivia", "BH": "Bahrain", "BI": "Burundi", "BJ": "Benin", "BT": "Bhutan", "JM": "Jamaica", "BV": "Bouvet Island", "BW": "Botswana", "WS": "Samoa", "BR": "Brazil", "BS": "Bahamas", "JE": "Jersey", "BY": "Belarus", "O1": "Other Country", "LV": "Latvia", "RW": "Rwanda", "RS": "Serbia", "TL": "Timor-Leste", "RE": "Reunion", "LU": "Luxembourg", "TJ": "Tajikistan", "RO": "Romania", "PG": "Papua New Guinea", "GW": "Guinea-Bissau", "GU": "Guam", "GT": "Guatemala", "GS": "South Georgia and the South Sandwich Islands", "GR": "Greece", "GQ": "Equatorial Guinea", "GP": "Guadeloupe", "JP": "Japan", "GY": "Guyana", "GG": "Guernsey", "GF": "French Guiana", "GE": "Georgia", "GD": "Grenada", "GB": "United Kingdom", "GA": "Gabon", "SV": "El Salvador", "GN": "Guinea", "GM": "Gambia", "GL": "Greenland", "GI": "Gibraltar", "GH": "Ghana", "OM": "Oman", "TN": "Tunisia", "JO": "Jordan", "HR": "Croatia", "HT": "Haiti", "HU": "Hungary", "HK": "Hong Kong", "HN": "Honduras", "HM": "Heard Island and McDonald Islands", "VE": "Venezuela", "PR": "Puerto Rico", "PS": "Palestinian Territory", "PW": "Palau", "PT": "Portugal", "SJ": "Svalbard and Jan Mayen", "PY": "Paraguay", "IQ": "Iraq", "PA": "Panama", "PF": "French Polynesia", "BZ": "Belize", "PE": "Peru", "PK": "Pakistan", "PH": "Philippines", "PN": "Pitcairn", "TM": "Turkmenistan", "PL": "Poland", "PM": "Saint Pierre and Miquelon", "ZM": "Zambia", "EH": "Western Sahara", "RU": "Russian Federation", "EE": "Estonia", "EG": "Egypt", "TK": "Tokelau", "ZA": "South Africa", "EC": "Ecuador", "IT": "Italy", "VN": "Vietnam", "SB": "Solomon Islands", "EU": "Europe", "ET": "Ethiopia", "SO": "Somalia", "ZW": "Zimbabwe", "SA": "Saudi Arabia", "ES": "Spain", "ER": "Eritrea", "ME": "Montenegro", "MD": "Moldova, Republic of", "MG": "Madagascar", "MF": "Saint Martin", "MA": "Morocco", "MC": "Monaco", "UZ": "Uzbekistan", "MM": "Myanmar", "ML": "Mali", "MO": "Macao", "MN": "Mongolia", "MH": "Marshall Islands", "MK": "Macedonia", "MU": "Mauritius", "MT": "Malta", "MW": "Malawi", "MV": "Maldives", "MQ": "Martinique", "MP": "Northern Mariana Islands", "MS": "Montserrat", "MR": "Mauritania", "IM": "Isle of Man", "UG": "Uganda", "TZ": "Tanzania, United Republic of", "MY": "Malaysia", "MX": "Mexico", "IL": "Israel", "FR": "France", "IO": "British Indian Ocean Territory", "FX": "France, Metropolitan", "SH": "Saint Helena", "FI": "Finland", "FJ": "Fiji", "FK": "Falkland Islands (Malvinas)", "FM": "Micronesia, Federated States of", "FO": "Faroe Islands", "NI": "Nicaragua", "NL": "Netherlands", "NO": "Norway", "NA": "Namibia", "VU": "Vanuatu", "NC": "New Caledonia", "NE": "Niger", "NF": "Norfolk Island", "NG": "Nigeria", "NZ": "New Zealand", "NP": "Nepal", "NR": "Nauru", "NU": "Niue", "CK": "Cook Islands", "CI": "Cote d'Ivoire", "CH": "Switzerland", "CO": "Colombia", "CN": "China", "CM": "Cameroon", "CL": "Chile", "CC": "Cocos (Keeling) Islands", "CA": "Canada", "CG": "Congo", "CF": "Central African Republic", "CD": "Congo, The Democratic Republic of the", "CZ": "Czech Republic", "CY": "Cyprus", "CX": "Christmas Island", "CR": "Costa Rica", "CV": "Cape Verde", "CU": "Cuba", "SZ": "Swaziland", "SY": "Syrian Arab Republic", "KG": "Kyrgyzstan", "KE": "Kenya", "SR": "Suriname", "KI": "Kiribati", "KH": "Cambodia", "KN": "Saint Kitts and Nevis", "KM": "Comoros", "ST": "Sao Tome and Principe", "SK": "Slovakia", "KR": "Korea, Republic of", "SI": "Slovenia", "KP": "Korea, Democratic People's Republic of", "KW": "Kuwait", "SN": "Senegal", "SM": "San Marino", "SL": "Sierra Leone", "SC": "Seychelles", "KZ": "Kazakhstan", "KY": "Cayman Islands", "SG": "Singapore", "SE": "Sweden", "SD": "Sudan", "DO": "Dominican Republic", "DM": "Dominica", "DJ": "Djibouti", "DK": "Denmark", "VG": "Virgin Islands, British", "DE": "Germany", "YE": "Yemen", "DZ": "Algeria", "US": "United States", "UY": "Uruguay", "YT": "Mayotte", "UM": "United States Minor Outlying Islands", "LB": "Lebanon", "LC": "Saint Lucia", "LA": "Lao People's Democratic Republic", "TV": "Tuvalu", "TW": "Taiwan", "TT": "Trinidad and Tobago", "TR": "Turkey", "LK": "Sri Lanka", "LI": "Liechtenstein", "A1": "Anonymous Proxy", "TO": "Tonga", "LT": "Lithuania", "A2": "Satellite Provider", "LR": "Liberia", "LS": "Lesotho", "TH": "Thailand", "TF": "French Southern Territories", "TG": "Togo", "TD": "Chad", "TC": "Turks and Caicos Islands", "LY": "Libyan Arab Jamahiriya", "VA": "Holy See (Vatican City State)", "VC": "Saint Vincent and the Grenadines", "AE": "United Arab Emirates", "AD": "Andorra", "AG": "Antigua and Barbuda", "AF": "Afghanistan", "AI": "Anguilla", "VI": "Virgin Islands, U.S.", "IS": "Iceland", "IR": "Iran, Islamic Republic of", "AM": "Armenia", "AL": "Albania", "AO": "Angola", "AN": "Netherlands Antilles", "AQ": "Antarctica", "AP": "Asia/Pacific Region", "AS": "American Samoa", "AR": "Argentina", "AU": "Australia", "AT": "Austria", "AW": "Aruba", "IN": "India", "AX": "Aland Islands", "AZ": "Azerbaijan", "IE": "Ireland", "ID": "Indonesia", "UA": "Ukraine", "QA": "Qatar", "MZ": "Mozambique"}, function(k, v) {
  98. countries.push({id: k, text: v});
  99. });
  100. $('#country').editable({
  101. inputclass: 'input-large',
  102. source: countries
  103. });
  104.  
  105.  
  106.  
  107. $('#address').editable({
  108. url: '/post',
  109. value: {
  110. city: "Moscow",
  111. street: "Lenina",
  112. building: "12"
  113. },
  114. validate: function(value) {
  115. if(value.city == '') return 'city is required!';
  116. },
  117. display: function(value) {
  118. if(!value) {
  119. $(this).empty();
  120. return;
  121. }
  122. var html = '<b>' + $('<div>').text(value.city).html() + '</b>, ' + $('<div>').text(value.street).html() + ' st., bld. ' + $('<div>').text(value.building).html();
  123. $(this).html(html);
  124. }
  125. });
  126.  
  127. $('#user .editable').on('hidden', function(e, reason){
  128. if(reason === 'save' || reason === 'nochange') {
  129. var $next = $(this).closest('tr').next().find('.editable');
  130. if($('#autoopen').is(':checked')) {
  131. setTimeout(function() {
  132. $next.editable('show');
  133. }, 300);
  134. } else {
  135. $next.focus();
  136. }
  137. }
  138. });
  139.  
  140. });

Report this snippet  

You need to login to post a comment.