jsgrid.field.select.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. (function(jsGrid, $, undefined) {
  2. var NumberField = jsGrid.NumberField;
  3. function SelectField(config) {
  4. this.items = [];
  5. this.selectedIndex = -1;
  6. this.valueField = "";
  7. this.textField = "";
  8. if(config.valueField && config.items.length) {
  9. this.valueType = typeof config.items[0][config.valueField];
  10. }
  11. this.sorter = this.valueType;
  12. NumberField.call(this, config);
  13. }
  14. SelectField.prototype = new NumberField({
  15. align: "center",
  16. valueType: "number",
  17. itemTemplate: function(value) {
  18. var items = this.items,
  19. valueField = this.valueField,
  20. textField = this.textField,
  21. resultItem;
  22. if(valueField) {
  23. resultItem = $.grep(items, function(item, index) {
  24. return item[valueField] === value;
  25. })[0] || {};
  26. }
  27. else {
  28. resultItem = items[value];
  29. }
  30. var result = (textField ? resultItem[textField] : resultItem);
  31. return (result === undefined || result === null) ? "" : result;
  32. },
  33. filterTemplate: function() {
  34. if(!this.filtering)
  35. return "";
  36. var grid = this._grid,
  37. $result = this.filterControl = this._createSelect();
  38. if(this.autosearch) {
  39. $result.on("change", function(e) {
  40. grid.search();
  41. });
  42. }
  43. return $result;
  44. },
  45. insertTemplate: function() {
  46. if(!this.inserting)
  47. return "";
  48. return this.insertControl = this._createSelect();
  49. },
  50. editTemplate: function(value) {
  51. if(!this.editing)
  52. return this.itemTemplate(value);
  53. var $result = this.editControl = this._createSelect();
  54. (value !== undefined) && $result.val(value);
  55. return $result;
  56. },
  57. filterValue: function() {
  58. var val = this.filterControl.val();
  59. return this.valueType === "number" ? parseInt(val || 0, 10) : val;
  60. },
  61. insertValue: function() {
  62. var val = this.insertControl.val();
  63. return this.valueType === "number" ? parseInt(val || 0, 10) : val;
  64. },
  65. editValue: function() {
  66. var val = this.editControl.val();
  67. return this.valueType === "number" ? parseInt(val || 0, 10) : val;
  68. },
  69. _createSelect: function() {
  70. var $result = $("<select>"),
  71. valueField = this.valueField,
  72. textField = this.textField,
  73. selectedIndex = this.selectedIndex;
  74. $.each(this.items, function(index, item) {
  75. var value = valueField ? item[valueField] : index,
  76. text = textField ? item[textField] : item;
  77. var $option = $("<option>")
  78. .attr("value", value)
  79. .text(text)
  80. .appendTo($result);
  81. $option.prop("selected", (selectedIndex === index));
  82. });
  83. $result.prop("disabled", !!this.readOnly);
  84. return $result;
  85. }
  86. });
  87. jsGrid.fields.select = jsGrid.SelectField = SelectField;
  88. }(jsGrid, jQuery));