MVC : UIBootgrid - various improvements

o minor style fixes and more strict json handling
o easier handle to add commands using a commands named array

$().UIBootgrid({
   ....
   commands: {
      myaction: {
         method: function(){};
         classname: 'fa fa-fw fa-remove',
         sequence: 10
      },
      copy: {
         classname: undefined // hide button
      }
   }
});
This commit is contained in:
Ad Schellevis 2022-10-13 20:41:31 +02:00
parent d854e59771
commit 41911f8a85

View File

@ -63,36 +63,59 @@ $.fn.UIBootgrid = function (params) {
* register commands
*/
this.getCommands = function() {
return {
"command-add": {
let result = {
"add": {
method: this_grid.command_add,
requires: ['get', 'set']
requires: ['get', 'set'],
sequence: 100
},
"command-edit": {
"edit": {
method: this_grid.command_edit,
requires: ['get', 'set']
classname: 'fa fa-fw fa-pencil',
requires: ['get', 'set'],
sequence: 100
},
"command-delete": {
"delete": {
method: this_grid.command_delete,
requires: ['del']
classname: 'fa fa-fwfa-trash-o',
requires: ['del'],
sequence: 500
},
"command-copy": {
"copy": {
method: this_grid.command_copy,
requires: ['get', 'set']
classname: 'fa fa-fw fa-clone',
requires: ['get', 'set'],
sequence: 200
},
"command-info": {
"info": {
method: this_grid.command_info,
requires: ['info']
classname: 'fa fa-fw fa-info-circle',
requires: ['info'],
sequence: 500
},
"command-toggle": {
"toggle": {
method: this_grid.command_toggle,
requires: ['toggle']
requires: ['toggle'],
sequence: 100
},
"command-delete-selected": {
"delete-selected": {
method: this_grid.command_delete_selected,
requires: ['del']
requires: ['del'],
sequence: 100
}
};
// register additional commands
if ( 'commands' in params) {
$.each(params['commands'], function( k, v ) {
if (result[k] === undefined) {
result[k] = {requires: [], sequence: 1};
}
$.each(v, function(ck, cv) {
result[k][ck] = cv;
});
});
}
return result;
};
/**
@ -106,12 +129,48 @@ $.fn.UIBootgrid = function (params) {
multiSelect: true,
rowCount:[7,14,20,50,100,-1],
url: params['search'],
// XXX: needs some work in a couple of controllers to enable
// ajaxSettings: {
// contentType: 'application/json;charset=UTF-8',
// dataType: "json",
// },
requestHandler: function (request) {
return JSON.stringify(request);
},
datakey: 'uuid',
useRequestHandlerOnGet: false,
formatters: {
"commands": function (column, row) {
return '<button type="button" class="btn btn-xs btn-default command-edit bootgrid-tooltip" data-row-id="' + row.uuid + '"><span class="fa fa-fw fa-pencil"></span></button> ' +
'<button type="button" class="btn btn-xs btn-default command-copy bootgrid-tooltip" data-row-id="' + row.uuid + '"><span class="fa fa-fw fa-clone"></span></button>' +
'<button type="button" class="btn btn-xs btn-default command-delete bootgrid-tooltip" data-row-id="' + row.uuid + '"><span class="fa fa-fw fa-trash-o"></span></button>';
commands: function (column, row) {
let html = [];
// sort commands by sequence
let commands = this_grid.getCommands();
let commandlist = Array();
Object.keys(commands).map(function (k) {
let item = commands[k];
item.name = k;
commandlist.push(item)
});
commandlist = commandlist.sort(function(a,b) {
return (a.sequence > b.sequence) ? 1 : ((b.sequence > a.sequence) ? -1 : 0);}
);
let rowid = params.datakey !== undefined ? params.datakey : 'uuid';
commandlist.map(function(command){
let has_option = command.classname !== undefined;
for (let i=0; i < command.requires.length; i++) {
if (!(command.requires[i] in params)) {
has_option = false;
}
}
if (has_option) {
html.push("<button type=\"button\" class=\"btn btn-xs btn-default bootgrid-tooltip command-"+command.name+
"\" data-row-id=\"" + row[rowid] + "\">"+
"<span class=\""+command.classname+"\"></span></button> "
);
}
});
return html.join('\n');
},
"commandsWithInfo": function(column, row) {
return '<button type="button" class="btn btn-xs btn-default command-info bootgrid-tooltip" data-row-id="' + row.uuid + '"><span class="fa fa-fw fa-info-circle"></span></button> ' +
@ -392,7 +451,7 @@ $.fn.UIBootgrid = function (params) {
} else if ($(this).hasClass('command-edit')) {
$(this).attr('title', $.fn.UIBootgrid.defaults.editText);
} else if ($(this).hasClass('command-toggle')) {
if ($(this).data('value') == 1) {
if ($(this).data('value') === 1) {
$(this).attr('title', $.fn.UIBootgrid.defaults.disableText);
} else {
$(this).attr('title', $.fn.UIBootgrid.defaults.enableText);
@ -419,8 +478,8 @@ $.fn.UIBootgrid = function (params) {
}
}
if (has_option) {
grid.find("."+k).unbind('click').on("click", commands[k].method);
} else if ($("."+k).length > 0) {
grid.find(".command-"+k).unbind('click').on("click", commands[k].method);
} else if ($(".command-"+k).length > 0) {
console.log("not all requirements met to link " + k);
}
});