Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update jquery.edittable.js #7

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,37 @@ To define a <strong>custom field type</strong> object:
}
]
```
To define a <strong>custom field type</strong> 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] = "<option value='val-1-1'>val-1-1</option><option value='val-1-2'>val-1-2</option>";
selectOptions[2] = "<option value='val-2-1'>val-2-1</option><option value='val-2-2'>val-2-2</option>";

var mytable = $('#edittable')..editTable({
field_templates: {
'select': {
html: '<select></select>', // 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.

Expand Down
12 changes: 6 additions & 6 deletions jquery.edittable.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*! editTable v0.2.0 by Alessandro Benoit */
/*! editTable v0.2.2 by Alessandro Benoit */
(function ($, window, i) {

'use strict';
Expand Down Expand Up @@ -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 '<td>' + field.setValue(field.html, content)[0].outerHTML + '</td>';
return '<td>' + field.setValue(field.html, content, col)[0].outerHTML + '</td>';
}
// Default
return '<td><input type="text" value="' + content.toString().replace(/"/g, "&quot;") + '" /></td>';
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -306,4 +306,4 @@
};
};

})(jQuery, this, 0);
})(jQuery, this, 0);