running-coder / jquery-typeahead

Javascript Typeahead (autocomplete) plugin with more than 50 options and callbacks.
http://www.runningcoder.org/jquerytypeahead/
MIT License
544 stars 121 forks source link

"undefined" is appearing in the template when a key is not defined instead of empty string #285

Closed GDM70 closed 7 years ago

GDM70 commented 7 years ago

When t118voornaam is absent :

When I type "Schram" in the inputfield :

In older versions I get "Schram" (in the dropdown list) In the develop branch I get "Schram undefined" (in the dropdown list)

my code :

$.typeahead({ input: "#l01a", order: "asc", maxItem: 50, minLength: 3, cancelButton: false, dynamic: true, display: ["t118achternaam", "t118voornaam", "t118straat", "t118postcode", "t118gemeente", "t118land", "t118tel", "t118ned", "t118gsm", "t118gsminfo", "t118gsm2", "t118gsm2info", "t118opmerking", "t118adres1", "t118adres2", "t118adres3", "t118adres4", "t118adres5", "t118email"], template: function () { return "{{t118achternaam}}" + " " + "{{t118voornaam}}" + "
" + "{{t118straat}}" + " " + "{{t118huisnummer}}" + ", " + "{{t118postcode}}" + " " + "{{t118kenletters}}" + " " + "{{t118gemeente}}"; }, emptyTemplate: "geen resultaten gevonden voor {{query}}", source: { klanten: { ajax: function (query) { return { type: "GET", path: "waarden", url: "/vf/rest/klanten/T118_04?", data: { q: query } }; } } }, callback: { onResult: function (node, query, result, resultCount) { if (resultCount === 1) { vm.input_l01a = result[0]; vm.resultCount_l01a = resultCount; } }, onNavigateBefore: function (node, query, event) { if (event.keyCode === 13 && vm.resultCount_l01a === 1) { vm.findKlant(vm.input_l01a.t118klantid); $("[tabindex=2]").focus(); } }, onNavigateAfter: function (node, lis, a, item, query, event) { if (~[38,40].indexOf(event.keyCode)) { var resultList = node.closest("form").find("ul.typeahead__list"), activeLi = lis.filter("li.active"), offsetTop = activeLi[0] && activeLi[0].offsetTop - (resultList.height() / 2) || 0;

                resultList.scrollTop(offsetTop);
            }
        },
        onClickAfter: function (node, a, item, event, data) {
            vm.findKlant(item.t118klantid);
            $("[tabindex=2]").focus();
        }
    }
});

testdata :

{"waarden":[{"t118achternaam":"Schram","t118afhaal":false,"t118debet":0.0000,"t118dierenarts":"","t118doorgestuurd":false,"t118gemeente":"Putte-Kapellen","t118gsm":"0473/81.59.00","t118huisnummer":"25","t118id":555,"t118klantid":581,"t118korting":0,"t118laatste":"2008-07-12T00:00:00+02:00","t118land":"België","t118nieuwsbrief":false,"t118opmerking":"gsm Wendy : 0496 97 01 50","t118postcode":"2950","t118praktijk":1,"t118status":"A","t118straat":"Selstbaan","t118taal":"nederlands","t118tel":"664.89.12","t118wachtdienst":false,"t118zone":" 03"},{"t118achternaam":"Schram","t118afhaal":false,"t118debet":0.0000,"t118dierenarts":"","t118doorgestuurd":false,"t118gemeente":"Putte","t118gsm":"0496/97.00.78","t118gsminfo":"Wendy","t118huisnummer":"31","t118id":1485,"t118kenletters":"CW","t118klantid":1552,"t118korting":0,"t118laatste":"2007-06-23T00:00:00+02:00","t118land":"Nederland","t118nieuwsbrief":false,"t118postcode":"4645","t118praktijk":1,"t118status":"A","t118straat":"Achterstraat","t118taal":"nederlands","t118voornaam":"Wendy","t118wachtdienst":false,"t118zone":" 03"},{"t118achternaam":"Schram","t118afhaal":false,"t118debet":0.0000,"t118dierenarts":"","t118doorgestuurd":false,"t118gemeente":"Putte-Kapellen","t118gsm":"0489/73 01 40","t118gsminfo":"Joeri","t118huisnummer":"29","t118id":2221,"t118kenletters":"","t118klantid":2062,"t118korting":0,"t118land":"België","t118nieuwsbrief":false,"t118postcode":"2950","t118praktijk":1,"t118status":"A","t118straat":"Selstbaan","t118taal":"nederlands","t118voornaam":"Joeri","t118wachtdienst":false,"t118zone":" 03"}]}

running-coder commented 7 years ago

Should be good now, I have to write a bunch of unit test for that namespace helper function to not miss those in the future

GDM70 commented 7 years ago

yes, problem solved :-)

running-coder commented 7 years ago

Added unit and integration tests to cover this