My grid has a foreign key link to the user table. I would like to use autocomplete to search the user table for users by name but store only the user_id in the database.
Everything is working except I don't know how to store the user Id field in the grid.
( ui.item.id ) is the correct user_id value
How do I write it to the dataIndx:"user_id"?
I would also like to hear suggestions of how to not write the user_descriptor field to the database on save.
Bonus points:
.focus(function () {
$(this).data("autocomplete").search($(this).val());
}
throws an error message, but everything works when its commented out. Any ideas what it does and if I need it?
Parts:
dataModel:
{title:"User ID", width:4, dataType:"integer", dataIndx:"user_id", hidden:true},
{title:"User", width:100, dataType:"string", dataIndx:"user_descriptor", editor: { type: autoCompleteEditor }},
Function:
var autoCompleteEditor = function (ui) {
//debugger;
var $cell = ui.$cell,
rowData = ui.rowData,
dataIndx = ui.dataIndx,
width = ui.column.width,
cls = ui.cls;
var dc = $.trim(rowData[dataIndx]);
var $inp = $("<input type='text' name='" + dataIndx + "' class='" + cls + " pq-ac-editor' />")
.width(width - 6)
.appendTo($cell)
.val(dc);
$inp.autocomplete({
source: function( request, response) {
$.getJSON($SCRIPT_ROOT +'/fkey_search', { table:'users', srch: request.term }, function( data ) {
response( data )
});
},
minLength: 2,
select: function( event, ui ) {
// this is where I would like to set the id value
// alert(( ui.item.id ));
}
}).focus(function () {
//open the autocomplete upon focus
// Paramvir - this gives me an error message
//$(this).data("autocomplete").search($(this).val());
});
}
Python:
@app.route('/fkey_search')
def fkeySearch():
tbl = request.args.get('table', 0, type=str)
srch = request.args.get('srch', 0, type=str)
fmt_srch = "%"+ srch +"%"
if tbl == 'users':
sql = "select user_id as id, name as value from users where name ilike %s"
r = query_db(sql, (fmt_srch,))
json_output = json.dumps(r)
return json_output