common.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. function drawTable(fields, rows)
  2. {
  3. var head = '<tr>';
  4. for(let field in fields)
  5. {
  6. var fieldName = fields[field];
  7. fieldName = (typeof fieldSettings[field] !== 'undefined' && fieldSettings[field].name ? fieldSettings[field].name : fieldName);
  8. var width = getTextWidth(fieldName) + 40;
  9. head += '<th class="header" style="min-width:' + width + 'px;">' +
  10. fieldName +
  11. '<a class="btn btn-link header-config" data-toggle="dropdown" onClick="configHeader(this);"><i class="icon icon-cog-outline"></i></a>' +
  12. '</th>';
  13. }
  14. head += '</tr>';
  15. var html = '<thead>' + head + '</thead>';
  16. var body = '';
  17. for(let row of rows)
  18. {
  19. body += '<tr>';
  20. for(let index in row) body += '<td>' + row[index] + '</td>';
  21. body += '</tr>';
  22. }
  23. html += '<tbody>' + body + '</tbody>';
  24. $('table.result').html(html);
  25. }
  26. function getTextWidth(text)
  27. {
  28. var font = "bold 12pt arial"
  29. var canvas = document.createElement("canvas");
  30. var context = canvas.getContext("2d");
  31. context.font = font;
  32. var measure = context.measureText(text);
  33. return measure.width;
  34. }
  35. function configHeader(obj)
  36. {
  37. modal = new $.zui.ModalTrigger({title: lang.fieldSettings, custom: function(el) {
  38. var html = '<form id="edit" onsubmit="javascript:saveSettings();return false;">';
  39. html += '<table class="table table-form">';
  40. html += '<thead><tr><th class="w-100px">' + lang.field + '</th><th class="w-250px">' + lang.fieldName + '</th><th class="w-300px">' + lang.fieldType + '</th></tr></thead><tbody>';
  41. for(let index in fields)
  42. {
  43. var field = fields[index];
  44. var typeOptions = genTypeOptions(index);
  45. var name = typeof fieldSettings[field] == 'undefined' ? '' : fieldSettings[field].name;
  46. html += '<tr><td>' + field + '</td><td><input name="name" id="name' + index + '" class="form-control" value="' + name + '" /></td><td class="td-row">' + typeOptions + '</td></tr>';
  47. }
  48. html += '</tbody>';
  49. html += '<tfoot><tr><td colspan="3" class="text-center"><button type="submit" id="submit" class="btn btn-wide btn-primary">' + lang.save + '</button></td></tr></tfoot>';
  50. html += '</table>';
  51. html += '</form>';
  52. return html;
  53. }});
  54. modal.show({onShow: function()
  55. {
  56. setTimeout(function() {
  57. $('.chosen').chosen();
  58. $('select[name^="object"]').change(function(e)
  59. {
  60. var object = $(this).val();
  61. var objectid = $(this).attr('id');
  62. var $that = $(this);
  63. if(!object)
  64. {
  65. return false;
  66. }
  67. $.get(createLink('dataset', 'ajaxGetTypeOptions', 'object=' + object), function(res)
  68. {
  69. var index = objectid.substring(6);
  70. var id = 'type' + index;
  71. var res = JSON.parse(res);
  72. var options = '';
  73. for(let key in res.options)
  74. {
  75. options += '<option value="' + key + '">' + res.options[key].name + '</option>';
  76. }
  77. allTypeOptions[object] = res.options;
  78. var html = '<select class="form-control chosen" name="type[]" id="' + id + '">'+ options + '</select>';
  79. $that.closest('.td-row').find('.typebox').html(html);
  80. $('.types').chosen();
  81. });
  82. });
  83. }, 20)
  84. return false;
  85. }})
  86. modal.show();
  87. }