diff --git a/README.md b/README.md index e6dcc63..5861778 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,37 @@ To define a custom field type object: } ] ``` +To define a custom field type object, +and to set different properties for input elements depending on the column index in which the input is located: + +```js +var selectOptions = []; +selectOptions[1] = ""; +selectOptions[2] = ""; + +var mytable = $('#edittable')..editTable({ + field_templates: { + 'select': { + html: '', // Input type html + getValue: function getValue(input) { + return $(input).val(); + }, + setValue: function setValue(input, value, col) { + var select = $(input); + //Add options depending on the row number + if (col !== undefined && selectOptions[col] !== undefined) { + select.html(selectOptions[col]); + } + + select.find('option').filter(function () { + return $(this).val() == value; + }).attr('selected', true); + return select; + } + } + } +}); +``` That's it, now give a look to [the examples](http://codeb.it/edittable/) to understand how it works. diff --git a/jquery.edittable.js b/jquery.edittable.js index e9d07d4..73b9554 100644 --- a/jquery.edittable.js +++ b/jquery.edittable.js @@ -1,4 +1,4 @@ -/*! editTable v0.2.0 by Alessandro Benoit */ +/*! editTable v0.2.2 by Alessandro Benoit */ (function ($, window, i) { 'use strict'; @@ -35,12 +35,12 @@ i = i + 1; // Build cell - function buildCell(content, type) { + function buildCell(content, type, col) { content = (content === 0) ? "0" : (content || ''); // Custom type if (type && 'text' !== type){ var field = s.field_templates[type]; - return '' + field.setValue(field.html, content)[0].outerHTML + ''; + return '' + field.setValue(field.html, content, col)[0].outerHTML + ''; } // Default return ''; @@ -56,13 +56,13 @@ if (!s.row_template) { // Without row template for (b = 0; b < (len || data.length); b += 1) { - rowcontent += buildCell(data[b]); + rowcontent += buildCell(data[b], false, b); } } else { // With row template for (b = 0; b < s.row_template.length; b += 1) { // For each field in the row - rowcontent += buildCell(data[b], s.row_template[b]); + rowcontent += buildCell(data[b], s.row_template[b], b); } } @@ -306,4 +306,4 @@ }; }; -})(jQuery, this, 0); \ No newline at end of file +})(jQuery, this, 0);