| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- (function(jsGrid, $, undefined) {
- var NumberField = jsGrid.NumberField;
- function SelectField(config) {
- this.items = [];
- this.selectedIndex = -1;
- this.valueField = "";
- this.textField = "";
- if(config.valueField && config.items.length) {
- this.valueType = typeof config.items[0][config.valueField];
- }
- this.sorter = this.valueType;
- NumberField.call(this, config);
- }
- SelectField.prototype = new NumberField({
- align: "center",
- valueType: "number",
- itemTemplate: function(value) {
- var items = this.items,
- valueField = this.valueField,
- textField = this.textField,
- resultItem;
- if(valueField) {
- resultItem = $.grep(items, function(item, index) {
- return item[valueField] === value;
- })[0] || {};
- }
- else {
- resultItem = items[value];
- }
- var result = (textField ? resultItem[textField] : resultItem);
- return (result === undefined || result === null) ? "" : result;
- },
- filterTemplate: function() {
- if(!this.filtering)
- return "";
- var grid = this._grid,
- $result = this.filterControl = this._createSelect();
- if(this.autosearch) {
- $result.on("change", function(e) {
- grid.search();
- });
- }
- return $result;
- },
- insertTemplate: function() {
- if(!this.inserting)
- return "";
- return this.insertControl = this._createSelect();
- },
- editTemplate: function(value) {
- if(!this.editing)
- return this.itemTemplate(value);
- var $result = this.editControl = this._createSelect();
- (value !== undefined) && $result.val(value);
- return $result;
- },
- filterValue: function() {
- var val = this.filterControl.val();
- return this.valueType === "number" ? parseInt(val || 0, 10) : val;
- },
- insertValue: function() {
- var val = this.insertControl.val();
- return this.valueType === "number" ? parseInt(val || 0, 10) : val;
- },
- editValue: function() {
- var val = this.editControl.val();
- return this.valueType === "number" ? parseInt(val || 0, 10) : val;
- },
- _createSelect: function() {
- var $result = $("<select>"),
- valueField = this.valueField,
- textField = this.textField,
- selectedIndex = this.selectedIndex;
- $.each(this.items, function(index, item) {
- var value = valueField ? item[valueField] : index,
- text = textField ? item[textField] : item;
- var $option = $("<option>")
- .attr("value", value)
- .text(text)
- .appendTo($result);
- $option.prop("selected", (selectedIndex === index));
- });
- $result.prop("disabled", !!this.readOnly);
- return $result;
- }
- });
- jsGrid.fields.select = jsGrid.SelectField = SelectField;
- }(jsGrid, jQuery));
|