Clone auto complete not working as expected
Hi all,
Currently working on jquery clone and auto complete hre is my detail code for jquery clone
- var num = $('.cloned-row1').length;
- var newnum = num + 1;
- $(document).on("click", ".edu_add_button", function() {
- var $clone = $('.cloned-row1:eq(0)').clone(true, true).attr('id', "cloned-row" + newnum);
- $clone.find('[id]').each(function() {
- $(this).removeClass("errRed");
- if ($(this).hasClass("required_Field")) {
- $(this).prevAll('label').find('span.required-star').removeClass('text-error-red');
- $(this).addClass("cloned_field");
- //$(this).addClass("errRed");
- } else {
- $(this).removeClass("errRed");
- $(this).removeClass("text-error-red");
- }
- });
- $clone.find('.btn_more').after("<input type='button' class='btn_less1 edu_btnle' id='buttonless'/>");
- $clone.find(".school_Name").attr('disabled', true).val('');
- $clone.find(".txt_schName").val('').attr('id', 'txt_schName_' + newnum);
- $clone.find(".degree_Description").attr('disabled', true).val('');
- $clone.find(".ipt_Havg").val('');
- $clone.find(".trans_date").val('');
-
- var dateobj = new Date();
- var datemonth;
- if (dateobj.getMonth() + 1 < 10) datemonth = "0";
- datemonth += dateobj.getMonth() + 1;
- var fulldate = datemonth + "-" + dateobj.getDate() + "-" + dateobj.getFullYear();
- $clone.find("input.deg_date")
- .removeClass('hasDatepicker')
- .removeData('datepicker')
- .unbind()
- .datepicker({
- dateFormat: "mm/dd/yy",
- changeMonth: true,
- yearRange: "-100:+0",
- changeYear: true,
- maxDate: new Date(),
- showButtonPanel: false,
- });
-
- $('.cloned_field').addClass("errRed");
- var dateobj = new Date();
- var datemonth;
- if (dateobj.getMonth() + 1 < 10) datemonth = "0";
- datemonth += dateobj.getMonth() + 1;
- var fulldate = datemonth + "-" + dateobj.getDate() + "-" + dateobj.getFullYear();
- $clone.find("input.trans_date")
- .removeClass('hasDatepicker')
- .removeData('datepicker')
- .unbind()
- .datepicker({
- dateFormat: "mm/dd/yy",
- changeMonth: true,
- yearRange: "-100:+0",
- changeYear: true,
- maxDate: new Date(),
- showButtonPanel: false,
- });
-
- $(this).parents('.educat_info').after($clone);
-
- autoComplete($('#txt_schName_' + newnum));
- });
-
Here is my auto complete code
- $.widget('custom.tableAutocomplete', $.ui.autocomplete, {
- options: {
- open: function (event, ui) {
- // Hack to prevent a 'menufocus' error when doing sequential searches using only the keyboard
- $('.ui-autocomplete .ui-menu-item:first').trigger('mouseover');
- },
- focus: function (event, ui) {
- event.preventDefault();
- },
- highlightClass: "ui-state-highlight"
- },
- _create: function () {
- this._super();
- // Using a table makes the autocomplete forget how to menu.
- // With this we can skip the header row and navigate again via keyboard.
- this.widget().menu("option", "items", ".ui-menu-item");
- },
- _renderMenu: function (ul, items) {
- var self = this;
- var $li = $("<li>");
- $table = $('<table class="table-autocomplete">'),
- $thead = $('<thead>'),
- $headerRow = $('<tr>'),
- $tbody = $('<tbody>');
-
- $.each(self.options.columns, function (index, columnMapping) {
- $('<th>').text(columnMapping.title).appendTo($headerRow);
- });
-
- $thead.append($headerRow);
- $table.append($thead);
- $table.append($tbody);
- $li.append($table);
-
- ul.html($li);
-
- $.each(items, function (index, item) {
- self._renderItemData(ul, ul.find("table tbody"), item);
- });
- },
- _renderItemData: function (ul, table, item) {
- return this._renderItem(table, item).data("ui-autocomplete-item", item);
- },
- _renderItem: function (table, item) {
- var self = this;
- var $tr = $('<tr class="ui-menu-item" role="presentation">');
-
- $.each(self.options.columns, function (index, columnMapping) {
- var cellContent = !item[columnMapping.field] ? '' : item[columnMapping.field];
- $('<td>').text(cellContent).appendTo($tr);
- });
-
- return $tr.appendTo(table);
- }
- });
-
- $(document).ready(function() {
- autoComplete($('#txt_schName_1'));
- });
-
- function autoComplete(t) {
- t.tableAutocomplete({
- highlightClass: "bold",
- source: function(request, response) {
- var regex = new RegExp(request.term, 'i');
- //var filteredArray = filteredArray.slice(0,10);
- $.ajax({
- url: "json/dummy.json",
- dataType: "json",
- data: {
- term: request.term
- },
- success: function(data) {
- response($.map(data, function(item) {
- // This code is only for testing. It should be done on the server!
- if (regex.test(item.id) || regex.test(item.label)) {
- return item
- }
- }));
- },
-
- });
- },
- columns: [{
- field: 'id',
- title: 'Search School Name'
- }, {
- field: 'label',
- title: 'School Name'
- }],
- delay: 500,
- select: function (event, ui) {
- if (ui.item != undefined) {
- $(this).val(ui.item.value);
- $('#school_Name').val(ui.item.label);
- console.log(ui.item.label);
- if (ui.item.label === "Other"){
- var schoolObj = $(".school_Name");
- schoolObj.prop('disabled', false);
- schoolObj.val('');
- }
- }
- }
- });
- }
Here is my JSON value
- [{"id":"COL000001","label":"Emirates College of Technology- UAE","value":"COL000001"},
- {"id":"COL000002","label":"Al Khawarizmi International College- UAE","value":"COL000002"},
- {"id":"COL000003","label":"Syscoms College","value":"COL000003"},
- {"id":"COL000004","label":"Abounajm Khanj Pre-Uni Center","value":"COL000004"},
- {"id":"COL000005","label":"Advanced Placement","value":"COL000005"},
- {"id":"COL000006","label":"Al Buraimi College (Uni Clge)","value":"COL000006"},
- {"id":"COL000007","label":"Al-Ain Community College","value":"COL000007"},
- {"id":"COL000008","label":"AMA Computer College","value":"COL000008"},
- {"id":"COL000009","label":"Arab Academy for Bankg and Fin","value":"COL000009"},
- {"id":"COL0000010","label":"ARABACDSCITECHMARTIMETRNS","value":"COL0000010"},
- {"id":"COL0000011","label":"Arapahoe Community College","value":"COL0000011"},
- {"id":"Other","label":"Other","value":"Other"}]
Html field
<div class="container-fluid cloned-row1 cloned_div educat_info" id="cloned-row1" name="cloned-row1">
<input type="text" id="txt_schName_1" class="ipt_Field required_Field txt_schName "/>
<input type="button" class="btn_more edubtnmore btn_right edu_add_button" />
</div>
First time it was working fine but when i clik button in the second time it was not working