/eps/assets/5691f829/listview/jquery.yiilistview.js

https://bitbucket.org/orlac/eps · JavaScript · 114 lines · 63 code · 10 blank · 41 comment · 12 complexity · 03623e4864948310867f3b279afc7f94 MD5 · raw file

  1. /**
  2. * jQuery Yii ListView plugin file.
  3. *
  4. * @author Qiang Xue <qiang.xue@gmail.com>
  5. * @link http://www.yiiframework.com/
  6. * @copyright Copyright &copy; 2008-2010 Yii Software LLC
  7. * @license http://www.yiiframework.com/license/
  8. * @version $Id: jquery.yiilistview.js 3296 2011-06-22 17:15:17Z qiang.xue $
  9. */
  10. ;(function($) {
  11. /**
  12. * yiiListView set function.
  13. * @param options map settings for the list view. Availablel options are as follows:
  14. * - ajaxUpdate: array, IDs of the containers whose content may be updated by ajax response
  15. * - ajaxVar: string, the name of the GET variable indicating the ID of the element triggering the AJAX request
  16. * - pagerClass: string, the CSS class for the pager container
  17. * - sorterClass: string, the CSS class for the sorter container
  18. * - updateSelector: string, the selector for choosing which elements can trigger ajax requests
  19. * - beforeAjaxUpdate: function, the function to be called before ajax request is sent
  20. * - afterAjaxUpdate: function, the function to be called after ajax response is received
  21. */
  22. $.fn.yiiListView = function(options) {
  23. return this.each(function(){
  24. var settings = $.extend({}, $.fn.yiiListView.defaults, options || {});
  25. var $this = $(this);
  26. var id = $this.attr('id');
  27. if(settings.updateSelector == undefined) {
  28. settings.updateSelector = '#'+id+' .'+settings.pagerClass.replace(/\s+/g,'.')+' a, #'+id+' .'+settings.sorterClass.replace(/\s+/g,'.')+' a';
  29. }
  30. $.fn.yiiListView.settings[id] = settings;
  31. if(settings.ajaxUpdate.length > 0) {
  32. $(settings.updateSelector).die('click').live('click',function(){
  33. $.fn.yiiListView.update(id, {url: $(this).attr('href')});
  34. return false;
  35. });
  36. }
  37. });
  38. };
  39. $.fn.yiiListView.defaults = {
  40. ajaxUpdate: [],
  41. ajaxVar: 'ajax',
  42. pagerClass: 'pager',
  43. loadingClass: 'loading',
  44. sorterClass: 'sorter'
  45. // updateSelector: '#id .pager a, '#id .sort a',
  46. // beforeAjaxUpdate: function(id) {},
  47. // afterAjaxUpdate: function(id, data) {},
  48. // url: 'ajax request URL'
  49. };
  50. $.fn.yiiListView.settings = {};
  51. /**
  52. * Returns the key value for the specified row
  53. * @param id string the ID of the list view container
  54. * @param index integer the zero-based index of the data item
  55. * @return string the key value
  56. */
  57. $.fn.yiiListView.getKey = function(id, index) {
  58. return $('#'+id+' > div.keys > span:eq('+index+')').text();
  59. };
  60. /**
  61. * Returns the URL that generates the list view content.
  62. * @param id string the ID of the list view container
  63. * @return string the URL that generates the list view content.
  64. */
  65. $.fn.yiiListView.getUrl = function(id) {
  66. var settings = $.fn.yiiListView.settings[id];
  67. return settings.url || $('#'+id+' > div.keys').attr('title');
  68. };
  69. /**
  70. * Performs an AJAX-based update of the list view contents.
  71. * @param id string the ID of the list view container
  72. * @param options map the AJAX request options (see jQuery.ajax API manual). By default,
  73. * the URL to be requested is the one that generates the current content of the list view.
  74. */
  75. $.fn.yiiListView.update = function(id, options) {
  76. var settings = $.fn.yiiListView.settings[id];
  77. $('#'+id).addClass(settings.loadingClass);
  78. options = $.extend({
  79. type: 'GET',
  80. url: $.fn.yiiListView.getUrl(id),
  81. success: function(data,status) {
  82. $.each(settings.ajaxUpdate, function(i,v) {
  83. var id='#'+v;
  84. $(id).replaceWith($(id,'<div>'+data+'</div>'));
  85. });
  86. if(settings.afterAjaxUpdate != undefined)
  87. settings.afterAjaxUpdate(id, data);
  88. $('#'+id).removeClass(settings.loadingClass);
  89. },
  90. error: function(XMLHttpRequest, textStatus, errorThrown) {
  91. $('#'+id).removeClass(settings.loadingClass);
  92. alert(XMLHttpRequest.responseText);
  93. }
  94. }, options || {});
  95. if(options.data!=undefined && options.type=='GET') {
  96. options.url = $.param.querystring(options.url, options.data);
  97. options.data = {};
  98. }
  99. options.url = $.param.querystring(options.url, settings.ajaxVar+'='+id);
  100. if(settings.beforeAjaxUpdate != undefined)
  101. settings.beforeAjaxUpdate(id);
  102. $.ajax(options);
  103. };
  104. })(jQuery);