jquery.comboboxRef.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. (function($) {
  2. function formatData(data){
  3. var datas = [];
  4. for(var i=0;i<data.length;i++){
  5. var ele = {};
  6. ele["key"] = data[i];
  7. ele["value"] = data[i];
  8. datas.push(ele);
  9. }
  10. return datas;
  11. };
  12. $.extend($.fn.combobox.defaults, {
  13. //是否使用同步 默认不使用
  14. async:true,
  15. //是否自动格式化 默认不格式化
  16. defaultFormat:false,
  17. valueField:'key',
  18. textField:'value',
  19. //默认加载第一项后要做的事情
  20. refreshTarget : function(record) {
  21. },
  22. //选中一项后要做的事情
  23. onSelectAfter : function(record) {
  24. },
  25. doBackspace:function(){
  26. },
  27. onLoadSuccess : function() {
  28. var opts = $(this).combobox('options');
  29. var $data = $(this).combobox('getData');
  30. if ($data && $data.length >0 && opts.required)
  31. {
  32. $(this).combobox('clear');
  33. $(this).combobox("setValue", $data[0][opts.valueField]);
  34. var opts = $(this).combobox('options');
  35. opts.refreshTarget.call(this, $data[0]);
  36. }
  37. // var me = this;
  38. //
  39. // $(this).next('.combo').find("input:text").blur(function(){
  40. // var rows = $(me).combobox('getData');
  41. //
  42. // var flag = false;
  43. //
  44. // for(var i=0;i < rows.length;i++)
  45. // {
  46. // //如果手动输入的值和列表里的值一样
  47. // if (this.value == rows[i][me.textField])
  48. // {
  49. // flag = true;
  50. // }
  51. // }
  52. // if (!flag)
  53. // {
  54. // $(me).combobox('clear')
  55. // }
  56. // });
  57. },
  58. loader : function(param, success, error)
  59. {
  60. var opts = $(this).combobox('options');
  61. if (!opts.url)
  62. return false;
  63. $.ajax({
  64. type : opts.method,
  65. url : opts.url,
  66. data : param,
  67. dataType : 'json',
  68. async:opts.async,
  69. success : function(data) {
  70. if (opts.defaultFormat)
  71. {
  72. data = formatData(data);
  73. }
  74. success(data);
  75. },
  76. error : function() {
  77. error.apply(this, arguments);
  78. }
  79. });
  80. }
  81. });
  82. })(jQuery);