0

I need to hide the delete button if there are less than 2 records in the grid. This is the js code. For some reason, the flag showDelCurrencyButton is not working out here and is always false.

Any other way to do it?

showDelCurrencyButton = false;
grid.jqGrid({
            datatype: 'local',
            jsonReader: jqgrid.jsonReader('CurrCd'),
            mtype: 'POST',
            pager: '#currencyPager',
            colNames: ['Abbrev.', 'Name', 'Symbol'],
            colModel: [
                {
                    name: 'CurrCd', index: 'CurrCd', width: 200, sortable: false,
                    editable: true,
                    edittype: 'select', stype: 'select',
                    editrules: { required: true },
                    editoptions: {
                        dataUrl: getServerPath() + 'Ajax/GetCurrencies',
                        buildSelect: function (data) {
                            var currSelector = $("<select id='selCurr' />");
                            $(currSelector).append($("<option/>").val('').text('---Select Currency---'));
                            var currs = JSON.parse(data);
                            $.each(currs, function () {
                                var text = this.CurName;
                                var value = this.CurCode;
                                $(currSelector).append($("<option />").val(value).text(text));
                            });

                            return currSelector;
                        }
                    }
                },

                { name: 'CurrName', index: 'CurrName', width: 200, sortable: false },
                { name: 'CurrSymbol', index: 'CurrSymbol', width: 200, sortable: false },

            ],
            loadtext: 'Loading...',
            caption: "Available Currencies",
            scroll: true,
            hidegrid: false,
            height: 116,
            width: 650,
            rowNum: 1000,
            altRows: true,
            altclass: 'gridAltRowClass',
            onSelectRow: webview.legalentities.billing.onCurrencySelected,
            loadComplete: function (data) {
                if (data.length > 1) {
                   showDelCurrencyButton = true;
                }
                var rowIds = $('#currencyGrid').jqGrid('getDataIDs');
                $("#currencyGrid").jqGrid('setSelection', rowIds[0]);
            },
            rowNum: 1000
        });
        grid.jqGrid('navGrid', '#currencyPager', {           
            edit: false,
            del: (showDelCurrencyButton == true),
            deltitle: 'Delete record',
            search: false,
            refresh: false
        }
       });
sarsnake
  • 26,667
  • 58
  • 180
  • 286

1 Answers1

1

Your current code uses datatype: 'local' without specifying data parameter with input data. It seems strange. In any way you can hide the Delete button dynamically identifying it by id. It's "del_" + grid[0].id in your case. Thus you can use $("#del_" + grid[0].id).hide(); to hide it. By the way one can use this instead of grid[0] inside of loadComplete.

I'd recommend you to read the old answer for more details and to read the answer, which shows how to disable/enable navigator buttons (like Delete button) instead of hiding.

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • $("del_" + grid[0].id).hide(); does not seem to find the delete button so it can't hide it. – sarsnake Jul 11 '16 at 20:38
  • @sarsnake: It should be of cause `$("#del_" + grid[0].id).hide();` because `"del_" + grid[0].id` is the id of the button. – Oleg Jul 11 '16 at 20:56
  • Thanks Oleg! I just went through my code again and was going to mark this as an answer. Clearly, need more coffee. – sarsnake Jul 11 '16 at 21:02