// 通过属性设置选项
var editor = new YAHOO.ext.grid.NumberEditor();
editor.allowBlank = false;
editor.minValue = 10;
editor.minText = "Nice try buddy, %0 is too small.";
//通过配置对象设置选项
var editor = new YAHOO.ext.grid.NumberEditor({allowBlank: false, minValue: 10,
minText: "Nice try buddy, %0 is too small."});
//设置最大最小值,NumberEditors的西班牙文错误提示
YAHOO.ext.grid.NumberEditor.prototype.minText = "La cantidad debe ser menos de %0";
YAHOO.ext.grid.NumberEditor.prototype.maxText = "La cantidad debe ser más de %0";
[b]预定义编辑器Predefined Editors[/b]
当前的编辑器已经实现的功能:
[b]文本编辑器TextEditor[/b] 提供一个简单的文本编辑器,下列是配置选项:
[list]
[*]allowBlank - True if the cell is allowed to be empty.
[/*][*]selectOnFocus - True to select the text when the editor is activated.
[/*][*]blankText - The tooltip (error message) to display when the cell is empty and is not allowed to be.
[/*][*]regex - A regular expression to match if the value is valid. If the regex.test(value) returns false, the value is considered invalid.
[/*][*]regexText - The tooltip (error message) to display when regex does not match.
[/*][*]validator - Any custom validation function you want called. The function must return true if the data is valid or an error message otherwise.
[/*][*]validationDelay - The delay in milliseconds for validation. Each time the user types something the field is validated after a specified delay, setting this value allows you to customize that delay (for example, if your custom validation routine is slow). [/*][/list]
[b]数字编辑器 NumberEditor[/b]
[list]
[*][i]allowDecimals[/i] - True if the cell can have decimal values.
[/*][*][i]decimalSeparator[/i] - Character(s) to allow as the decimal separator.
[/*][*][i]decimalPrecision[/i] - Set the maximum decimal precision.
[/*][*][i]decimalPrecisionFcn[/i] - Define the function to call to remove extra precision (ie. Math.floor, Math.round, Math.ceil or your own function).
[/*][*][i]allowNegative[/i] - True if the cell allows negative values.
[/*][*][i]selectOnFocus[/i] - True to select the text when the editor is activated.
[/*][*][i]minValue[/i] - The minimum value the cell will allow.
[/*][*][i]maxValue[/i] - The maximum value the cell will allow.
[/*][*][i]minText[/i] - The tooltip to display when the value in the cell is below the minimum.
[/*][*][i]maxText[/i] - The tooltip to display when the value in the cell is above the maximum.
[/*][*][i]nanText[/i] - The tooltip to display when the value in the cell is not a valid number (for example, negatives are allowed and the value in the cell is just "-" with no numbers).
[/*][*][i]allowBlank[/i] -同TextEditor。
[/*][*][i]blankText[/i] -同TextEditor。
[/*][*][i]validationDelay[/i] -同TextEditor。
[/*][*][i]validator[/i] -同TextEditor。 [/*][/list]
[b]日期编辑器 DataEditor[/b]
提供一个日期编辑器的字段,可选地提供一个DatePicker。如果你不想用内置的DatePicker,DateEditor提供一个方法(showCalendar)来覆盖override内置的DatePicker。相比YUI日历组件,这里我选用了自己的DataPicker,它非常容易地override Gird事件,并工作地很好。压缩后它只有5K大小,而YUI日历压缩后40k大小。使用DataPicker时,左/右改变月份,上下改变年份,鼠标滚轮快速地切换月份。DateEditor支持下列配置选项:
[list]
[*][i]format[/i] - The date format for the editor. The format is now identical to [url=http://www.php.net/date]PHP date()[/url] and text is allowed. Credit for that goes to [url=http://www.xaprb.com/blog/2006/05/14/javascript-date-formatting-benchmarks/]this fantastic date library[/url]. This format is for the editor only and doesn't affect the rendering of the cell when not in edit mode. Your rendering function can use any date format it wants.
[/*][*][i]minValue[/i] - The minimum allowed date. Can be either a Javascript date object or a string date in the specified format.
[/*][*][i]maxValue[/i] - The maximum allowed date. Can be either a Javascript date object or a string date in the specified format.
[/*][*][i]minText[/i] - The tooltip to display when the date in the cell is before minValue.
[/*][*][i]maxText[/i] - The tooltip to display when the date in the cell is after maxValue.
[/*][*][i]invalidText[/i] - The text to display when the date in the field is invalid (for example: 02/31/06)
[/*][*][i]disabledDays[/i] - An array of days to disable, 0 based. For example, [0, 6] disables Sunday and Saturday.
[/*][*][i]disabledDaysText[/i] - The tooltip to display when the date in the cell (or DatePicker) falls on a disabled day.
[/*][*][i]disabledDates[/i] - An array of "dates" to disable, as strings. These strings will be used to build a dynamic regular expression so they are very powerful. For example, ["03/08/2003", "09/16/2003"] would disable those dates, but ["03/08", "09/16"] would disable them for every year. If you are using short years, you will want to use ^ to tell the regular expression to only match the beginning like ["^03/08"]. To disable March of 2006: ["03/../2006"] or every March ["^03"]. In order to support regular expressions, if you are using a date format that has "." in it, you will have to escape the dot when restricting dates. For example: ["03.08.03"].
[/*][*][i]disabledDatesText[/i] - The tooltip to display when the date in the cell (or DatePicker) falls on a disabled date.
[/*][*][i]allowBlank[/i] -同TextEditor。
[/*][*][i]blankText[/i] -同TextEditor。
[/*][*][i]validationDelay[/i] -同TextEditor。
[/*][*][i]validator[/i] -同TextEditor。 [/*][/list]
[b]单选项编辑器CheckboxEditor[/b]
为布尔值提供checkbox。当前没有配置选项。
[b]SelectEditor[/b]
利用xhtml中的Select字段创建编辑器。你可以在JAVASCRIPT中用DOM来创建select,然后传入到SelectEditor的构建函数,或者用这种简单的方式:定义一个select在xhtml文档中,定义 ygrid-editor class的样式。
创建SelectEditor 对象, 传入Select之ID
var editor = new YAHOO.ext.grid.SelectEditor('light');
[b]组合所有的功能在一起[/b]
你可以采用这种方式定义一个ColumnModel:
var yg = YAHOO.ext.grid;
var cols = [{
header: "Common",
width: 120,
editor: new yg.TextEditor()
},{
header: "Light",
width: 80,
editor: new yg.SelectEditor('light')
},{
header: "Price",
width: 50,
renderer: formatMoney,
editor: new yg.NumberEditor({allowBlank: false, allowNegative: false})
},{
header: "Available",
width: 85,
renderer: formatDate,
editor: new yg.DateEditor({format: 'MM/dd/yy', minValue: '01/01/06'})
},{
header: "Indoor?",
width: 50,
renderer: formatBoolean,
editor: new yg.CheckboxEditor()
}];
var colModel = new YAHOO.ext.grid.DefaultColumnModel(cols);
[b]保存数据[/b]
改变单元格数据会触发DataModel的onCellUpdated事件。你可利用该事件来保存数据。如果你使用是XMLDataModel,所有的编辑会自动保存到由DataMODEL加载的XML文档。通过getDocument() 可获取该文档。
[b]反馈[/b]
回溯到这篇贴子的目的,是为了可以尽可能测试,然后归档,准备发布出来。我正正需要的是反馈。缺了些什么?我肯定这里还有很多你需要、想要的功能而我还没做的。当前只有我一个人在做(不过我也希望有所改变),没有大家的参与是不可能开发想这样的GIRD。如果有你想看到功能,请发表评论或在[url=http://www.ajaxjs.com/yuicn/bbs/]论坛[/url]里参与讨论。谢!
目前这些功能来自反馈:
[list]
[*]NumberEditor.decimalSeparator - 支持不同的十进制分隔符
[/*][*]NumberEditor.decimalPrecision, NumberEditor.decimalPrecisionFcn - Support for decimal precision and rounding etc. See the thread in the forum for details.
[/*][*]Shift-tab to navigate backwards, enter moves down 1 cell.
[/*][*]DatePicker changes: Highlights selected date, highlights today, restricts selection based on min/max values for the DateEditor.
[/*][*]Hiding and "Unhiding" of columns programmatically. You can also start columns in a hidden state.
[/*][*]More DateEditor/Picker enhancements: disabling of days (i.e. Saturday and Sunday), and disabling of individual dates/date ranges/dates each year. The disabling of the dates is accomplished by dynamically building a regular expression so it is very flexible and very fast. See DateEditor above for more details. The date format specifiers are different now and match the PHP date() function instead of Java mm/dd/yy style. The reason is I integrated [url=http://www.xaprb.com/blog/2006/05/14/javascript-date-formatting-benchmarks/]this small date lib[/url] and got rid of my own date parsing code. It's faster and also support dates with text now, such as 15-Mar-06.
[/*][*]Regular expression validation on the TextEditor without using a validator function. [/*][/list]
请随时发表反馈!