yvanui.radiogroup.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /**
  2. * YvanUI
  3. *
  4. * Copyright (c) 2018 www.yvanui.com. All rights reserved.
  5. * @author luoyifan@qq.com
  6. * @time 2018-11-27 11:16:00
  7. */
  8. (function ($) {
  9. var idx = 1;
  10. function createBox(target) {
  11. //_2
  12. var $group = $('<div class="checkgroup-container"></div>');
  13. $(target).append($group);
  14. return $group;
  15. }
  16. function init(target) {
  17. var state = $.data(target, "radiogroup");
  18. var opts = state.options;
  19. var $group = state.group;
  20. var id = '_checkgroup_' + (++idx);
  21. $group.attr('id', id);
  22. if (opts.label) {
  23. if ($.type(opts.label) === 'object') {
  24. state.label = $(opts.label);
  25. state.label.attr("for", $group);
  26. } else {
  27. $(state.label).remove();
  28. state.label = $("<label class=\"textbox-label\"></label>").html(opts.label);
  29. state.label.css("textAlign", opts.labelAlign).attr("for", id);
  30. if (opts.labelPosition === "after") {
  31. state.label.insertAfter($group);
  32. } else {
  33. state.label.insertBefore($group);
  34. }
  35. state.label.removeClass("textbox-label-left textbox-label-right textbox-label-top");
  36. state.label.addClass("textbox-label-" + opts.labelPosition);
  37. }
  38. } else {
  39. $(state.label).remove();
  40. }
  41. var data = opts.data;
  42. if ($.type(data) === 'function') {
  43. data = data.call(this);
  44. }
  45. if ($.type(data) === 'object') {
  46. data = [];
  47. for (var name in opts.data) {
  48. if (!opts.data.hasOwnProperty(name)) continue;
  49. var item = {};
  50. item.name = opts.name;
  51. item[opts.idValue || 'value'] = name;
  52. item[opts.textValue || 'label'] = opts.data[name];
  53. data.push(data);
  54. }
  55. } else if ($.type(opts.data) === 'array') {
  56. data = opts.data;
  57. }
  58. if (data) {
  59. for (var i = 0; i < data.length; i++) {
  60. var $dom = $('<input type="radio" name="' + opts.name + '" />');
  61. $group.append($dom);
  62. var extend = {
  63. onChange: (function (meValue) {
  64. return function (v) {
  65. if (v) {
  66. if ($.type(opts.onChange) === 'function') {
  67. opts.onChange.call(target, meValue);
  68. }
  69. }
  70. };
  71. })(data[i][opts.idValue])
  72. };
  73. if (opts.idValue) {
  74. extend.value = data[i][opts.idValue];
  75. }
  76. if (opts.textValue) {
  77. extend.label = data[i][opts.textValue];
  78. }
  79. $dom.radiobutton($.extend({}, data[i], extend));
  80. }
  81. }
  82. }
  83. function resizeLabel(target) {
  84. var state = $.data(target, "radiogroup");
  85. var opts = state.options;
  86. var $dom = state.group;
  87. $dom._size(opts, $dom.parent());
  88. if (opts.label && opts.labelPosition) {
  89. if (opts.labelPosition === "top") {
  90. state.label._size({ width: opts.labelWidth }, $dom);
  91. } else {
  92. state.label._size({ width: opts.labelWidth, height: 'auto' }, $dom);
  93. state.label.css("float", "left");
  94. state.label.css("padding-top", "10px");
  95. }
  96. }
  97. }
  98. $.fn.radiogroup = function (options, param) {
  99. if (typeof options === 'string') {
  100. return $.fn.radiogroup.methods[options](this, param);
  101. }
  102. options = options || {};
  103. return this.each(function () {
  104. var state = $.data(this, 'radiogroup');
  105. if (state) {
  106. $.extend(state.options, options);
  107. } else {
  108. $.data(this, 'radiogroup', {
  109. options: $.extend({}, $.fn.radiogroup.defaults, options),
  110. group: createBox(this)
  111. });
  112. }
  113. init(this);
  114. resizeLabel(this);
  115. });
  116. };
  117. $.fn.radiogroup.methods = {
  118. options: function (jq) {
  119. var opts = jq.data("radiogroup");
  120. return $.extend(opts.options, { value: opts.checkbox.find(".checkbox-value").val() });
  121. }, checkAll: function (jq) {
  122. return jq.each(function () {
  123. setValue(this, true);
  124. });
  125. }, uncheckAll: function (jq) {
  126. return jq.each(function () {
  127. setValue(this, false);
  128. });
  129. }, clear: function (jq) {
  130. return jq.each(function () {
  131. setValue(this, false);
  132. });
  133. }, reset: function (jq) {
  134. return jq.each(function () {
  135. var opts = $(this).checkbox("options");
  136. setValue(this, opts.originalValue);
  137. });
  138. }
  139. };
  140. $.fn.radiogroup.defaults = {
  141. value: null,
  142. primary: true,
  143. label: null,
  144. data: [],
  145. labelPosition: 'before',
  146. labelAlign: 'right',
  147. width: 280,
  148. labelWidth: 90,
  149. idValue: 'id',
  150. textValue: 'text',
  151. onChange: function (value) {
  152. }
  153. };
  154. })(jQuery);