toscawidgets/tw2.jqplugins.select2 — pull request #1 by bjonnh (2012-06-25 21:49:40+00:00)
Title: Adding a way to set initial values.

self.value : sqlalchemy collectionadapter or array of dicts
self.fields : contains fields of the model to be selected (avoid sending the whole data)

User must be careful to use the right results callback e.g:

class FractionField(Select2AjaxSingleSelectField):
options = []
opts = dict(
multiple=True,
ajax=dict(
url="/fetch_fractions",
dataType="json",
data=twc.js_callback(
"""
function (term, page) {
return {
q: term, // search term
};
}
"""),
results=twc.js_callback(
"""
function (data, page) {
return {results: data.data};
}
"""),
),
formatSelection=twc.js_callback(
"""
function (row) {
return row.code;
}
"""),
formatResult=twc.js_callback(
"""
function (row) {
return row.code;
}
"""),
)

where row.code is the data to be displayed to the user.