summaryrefslogtreecommitdiffstats
path: root/dev/jquery.jtable.core.js
diff options
context:
space:
mode:
authorHalil İbrahim Kalkan <hikalkan@gmail.com>2013-01-25 23:39:56 +0200
committerHalil İbrahim Kalkan <hikalkan@gmail.com>2013-01-25 23:39:56 +0200
commit844ee6c862cfc3221de4d22e9014a5eae357cabd (patch)
tree7f52f544b750dcd2ca8da428f54e97d9b05d52f0 /dev/jquery.jtable.core.js
parentb296ed7f0384c9ae8259a4299974864a24f4ce15 (diff)
downloadjtable-844ee6c862cfc3221de4d22e9014a5eae357cabd.zip
jtable-844ee6c862cfc3221de4d22e9014a5eae357cabd.tar.gz
jtable-844ee6c862cfc3221de4d22e9014a5eae357cabd.tar.bz2
jTable v2.1.0v2.1.0
Added cascade dropdowns and creating dynamically option list support. [#63, #94] Added field options: dependsOn and optionsSorting. Polish localization (by Grzegorz Zbucki). [#97] Lithuanian localization (by Vygandas Šimkus). [#103] Portuguese - Brazilian localization (by Renato Bigliazzi). [#129] Fixed some issues.
Diffstat (limited to 'dev/jquery.jtable.core.js')
-rw-r--r--dev/jquery.jtable.core.js147
1 files changed, 144 insertions, 3 deletions
diff --git a/dev/jquery.jtable.core.js b/dev/jquery.jtable.core.js
index 0e3b80c..5ac7525 100644
--- a/dev/jquery.jtable.core.js
+++ b/dev/jquery.jtable.core.js
@@ -617,12 +617,37 @@
return this._getDisplayTextForDateRecordField(field, fieldValue);
} else if (field.type == 'checkbox') {
return this._getCheckBoxTextForFieldByValue(fieldName, fieldValue);
- } else if (field.options) {
- return this._getOptionsWithCaching(fieldName)[fieldValue];
- } else {
+ } else if (field.options) { //combobox or radio button list since there are options.
+ var options = this._getOptionsForField(fieldName, {
+ record: record,
+ source: 'list',
+ dependedValues: this._createDependedValuesUsingRecord(record, field.dependsOn)
+ });
+ return this._findOptionByValue(options, fieldValue).DisplayText;
+ } else { //other types
return fieldValue;
}
},
+
+ _createDependedValuesUsingRecord: function (record, dependsOn) {
+ if (!dependsOn) {
+ return {};
+ }
+
+ var dependedValues = {};
+ dependedValues[dependsOn] = record[dependsOn];
+ return dependedValues;
+ },
+
+ _findOptionByValue: function (options, value) {
+ for (var i = 0; i < options.length; i++) {
+ if (options[i].Value == value) {
+ return options[i];
+ }
+ }
+
+ return {}; //no option found
+ },
/* Gets text for a date field.
*************************************************************************/
@@ -636,6 +661,122 @@
return $.datepicker.formatDate(displayFormat, date);
},
+ /* Gets options for a field according to user preferences.
+ *************************************************************************/
+ _getOptionsForField: function (fieldName, funcParams) {
+ var field = this.options.fields[fieldName];
+ var optionsSource = field.options;
+
+ if ($.isFunction(optionsSource)) {
+
+ //prepare parameter to the function
+ funcParams = $.extend(true, {
+ _cacheCleared: false,
+ dependedValues: {},
+ clearCache: function () {
+ this._cacheCleared = true;
+ }
+ }, funcParams);
+
+ //call function and get actual options source
+ optionsSource = optionsSource(funcParams);
+ }
+
+ var options;
+
+ //Build options according to it's source type
+ if (typeof optionsSource == 'string') { //It is an Url to download options
+ var cacheKey = 'options_' + fieldName + '_' + optionsSource;
+ if (funcParams._cacheCleared || (!this._cache[cacheKey])) {
+ this._cache[cacheKey] = this._downloadOptions(fieldName, optionsSource);
+ this._sortFieldOptions(this._cache[cacheKey], field.optionsSorting);
+ }
+
+ options = this._cache[cacheKey];
+ } else if (jQuery.isArray(optionsSource)) { //It is an array of options
+ options = this._buildOptionsFromArray(optionsSource);
+ this._sortFieldOptions(options, field.optionsSorting);
+ } else { //It is an object that it's properties are options
+ options = this._buildOptionsArrayFromObject(optionsSource);
+ this._sortFieldOptions(options, field.optionsSorting);
+ }
+
+ return options;
+ },
+
+ _sortFieldOptions: function (options, sorting) {
+
+ if ((!options) || (!options.length) || (!sorting)) {
+ return;
+ }
+
+ //Determine using value of text
+ var dataSelector;
+ if (sorting.indexOf('value') == 0) {
+ dataSelector = function(option) {
+ return option.Value;
+ };
+ } else { //assume as text
+ dataSelector = function (option) {
+ return option.DisplayText;
+ };
+ }
+
+ var compareFunc;
+ if ($.type(dataSelector(options[0])) == 'string') {
+ compareFunc = function (option1, option2) {
+ return dataSelector(option1).localeCompare(dataSelector(option2));
+ };
+ } else { //asuume as numeric
+ compareFunc = function (option1, option2) {
+ return dataSelector(option1) - dataSelector(option2);
+ };
+ }
+
+ if (sorting.indexOf('desc') > 0) {
+ options.sort(function (a, b) {
+ return compareFunc(b, a);
+ });
+ } else { //assume as asc
+ options.sort(function (a, b) {
+ return compareFunc(a, b);
+ });
+ }
+ },
+
+ _buildOptionsArrayFromObject: function (options) {
+ var list = [];
+
+ $.each(options, function (propName, propValue) {
+ list.push({
+ Value: propName,
+ DisplayText: propValue
+ });
+ });
+
+ return list;
+ },
+
+ /* Creates an options object (that it's property is value, value is displaytext)
+ * from a simple array.
+ *************************************************************************/
+ _buildOptionsFromArray: function (optionsArray) {
+ var list = [];
+
+ for (var i = 0; i < optionsArray.length; i++) {
+ if ($.isPlainObject) {
+ list.push(optionsArray[i]);
+ } else { //assumed as primitive type (int, string...)
+ list.push({
+ Value: optionsArray[i],
+ DisplayText: optionsArray[i]
+ });
+ }
+ }
+
+ return list;
+ },
+
/* Parses given date string to a javascript Date object.
* Given string must be formatted one of the samples shown below:
* /Date(1320259705710)/