/fa/jquery/jquery-ui/jqgrid/js/fa.jqgrid.js

https://bitbucket.org/gawel/fajquery · JavaScript · 91 lines · 88 code · 2 blank · 1 comment · 5 complexity · 616c7067dcb33b82251dd0bb435d4249 MD5 · raw file

  1. (function($) {
  2. $.fa.extend({
  3. jqgrid: function(table, pager, options) {
  4. var current_id;
  5. var base_url = window.location.href.split('?')[0];
  6. var editRow = function(id) {
  7. if (id) {
  8. var edit_url = base_url+'/'+id+'.xhr?_method=PUT';
  9. var item_url = base_url+'/'+id+'/edit.xhr';
  10. var form = $('<form title="Edit record"></form>');
  11. } else {
  12. id = 'new';
  13. var edit_url = base_url+'.xhr';
  14. var item_url = base_url+'/new.xhr';
  15. var form = $('<form title="New record"></form>');
  16. }
  17. pager.after(form);
  18. $.get(item_url, function(html) {
  19. form.append(html);
  20. form.dialog({
  21. modal: true,
  22. buttons: {
  23. 'Ok': function() {
  24. var data = form.formToArray();
  25. // avoid PHP arrays
  26. data = $.param(data).replace(/%5B%5D=/g, '=');
  27. $.post(edit_url, data, function(html) {
  28. if (/ui-state-error/.test(html)) {
  29. form.empty();
  30. form.append(html);
  31. } else {
  32. form.append(html);
  33. form.dialog('close');
  34. form.dialog('destroy');
  35. form.remove();
  36. }
  37. table.trigger('reloadGrid')
  38. });
  39. },
  40. 'Cancel': function() {
  41. form.dialog('close');
  42. form.dialog('destroy');
  43. form.remove();
  44. }
  45. }
  46. });
  47. setTimeout(function() {
  48. var text = $('textarea', form);
  49. if (text.length) {
  50. form.dialog('option', 'width', ''+(parseInt(text.css('width'))+50));
  51. form.dialog('option', 'position', ['center','top']);
  52. }
  53. }, 10);
  54. });
  55. }
  56. var settings = {
  57. url: base_url,
  58. datatype: "json",
  59. height: $(document).height()-200,
  60. width: $(document).width()-30,
  61. rowNum: parseInt(($(document).height()-200)/22),
  62. rowList:[10,20,50,100],
  63. pager: '#'+pager.attr('id'),
  64. sortname: 'id',
  65. viewrecords: true, sortorder: "desc",
  66. onCellSelect: function(id) { current_id = id },
  67. ondblClickRow:editRow
  68. }
  69. delete options['callback'];
  70. $.extend(settings, options);
  71. options['pager_id'] = '#'+pager.attr('id');
  72. table.jqGrid(settings)
  73. table.jqGrid('navGrid', options.pager_id,
  74. {search:true});
  75. $('#add_'+table.attr('id'))
  76. .unbind('click')
  77. .click(function() { editRow(); });
  78. $('#edit_'+table.attr('id'))
  79. .unbind('click')
  80. .click(function() { editRow(current_id); });
  81. $('#del_'+table.attr('id'))
  82. .unbind('click')
  83. .click(function() {
  84. table.jqGrid('delGridRow', current_id,
  85. {url:base_url+'/'+current_id+'.json?_method=DELETE'});
  86. });
  87. }
  88. });
  89. })(jQuery);