PageRenderTime 28ms CodeModel.GetById 13ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

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