javascript - How to add jquery autocomplete UI functionality to each cell in my table -
i have project requirement need create dynamic table using jquery. have reached state able add dynamic columns or rows table. can have @ fiddle code here : demo link here
html code here:
<br /><br /> <div id='input_div' name='input_div'> please input matrix dimensions : <input type="text" id="rowcount" size="2" value="1"> <span>x <input type="text" id="columncount" size ="2" value="1"> </div> <br><br> <input type="button" id="tablebtn" value="create table"> <input type="button" id="newabilitybtn" value="add ability"> <input type="button" id="newlevelbtn" value="add level"> <input type="button" id="submit" value="submit"> <br><br> <div id="box"> </div>
jquery code here:
var arr = [ {val : 1, text: 'one'}, {val : 2, text: 'two'}, {val : 3, text: 'three'}, {val : 4, text: 'four'} ]; $("#newabilitybtn").hide(); $("#newlevelbtn").hide(); $("#submit").hide(); $("#tablebtn").click(function(){ mytable = $('<table></table>').attr({ id: "matrixtable" }); var rows = new number($("#rowcount").val()); var cols = new number($("#columncount").val()); var tr = []; (var = 0; < rows; i++) { var row; if( == 0){ row=$('<tr><th></th><th>level</th></tr>').attr({ class: ["lheader"] }).appendto(mytable); } row = $('<tr><th>ability</th>').attr({ class: ["aheader"] }).appendto(mytable); (var j = 0; j < cols; j++) { var td; td = $('<td>').attr({ class: ["matrix_row"] }).appendto(row); var sel = $('<select />').attr({ class: ["matrix"], text:'elway',id: "matrixtable"+"_"+i+"_"+j }).appendto(td); $(arr).each(function() { sel.append($("<option>").attr('value',this.val).text(this.text)); }); $('<br><br><span>').attr({'class':'rubric_span' }).html('selected: ').appendto(td); // $('#rubric_name').html(span); $('</span>').appendto(row); $('</td>').appendto(row); } $('</tr>').appendto(row); } //console.log("ttttt:"+mytable.html()); mytable.appendto("#box"); $("#tablebtn").hide(); $("#input_div").hide(); $("#newabilitybtn").show(); $("#newlevelbtn").show(); $("#submit").show(); }); $("#newabilitybtn").click(function(){ $('#matrixtable').append($('#matrixtable').find("tr:last").clone()); }); $("#newlevelbtn").click(function(){ $('#matrixtable tr').each(function(){ $(this).append($(this).find(">*:last").clone()); }); }); $("#submit").click(function(){ jsonobj = []; var dropdown_count = 0; var rowcount = $('#matrixtable tr').length; var colcount = $('#matrixtable').find("tr:last td").length; var row = 1; var col = 1; // fetching level , ability names //$("select[class=lheader]").each(function() { // var id = "level_"+ row + "_" + col++ ; // var selected_val = 0 ;//$(this).text(); // item = {} // item ["id"] = id; // item ["names"] = selected_val; // jsonobj.push(item); //}); //row = 1; //col = 1; // fetching rubric selections $("span[class=rubric_span]").each(function() { var id = "selected_rubric_"+ row + "_" + col++ ; //var selected_val = $(this).val(); var selected_val = $(this).text(); item = {} item ["id"] = id; item ["selected_rubric"] = selected_val; if( row == rowcount ){ row = 1; } if( col == colcount + 1 ){ col = 1; row++; } jsonobj.push(item); }); console.log(jsonobj); });
css code:
table{ width:200px; height:200px; } table td{ padding:10px; margin:10px; border:1px solid #ccc; } table tr{ height:10px; }
now, problem need replace dropdown in each cell search input field. searched around net , found link suitable. autocomplete widget. searched on how implement , found if dom elements created dynamically clone function not work or of them have suggested change way in table created. can 1 guide me tutorial or blog, educates me on how task done. project developed using perl language.
my initial approach was, clear contents of array arr
, load list of options on page load ( ajax call ). re-use list drop downs in each cell. right approach?
i newbie jquery. please guide me. thanks
just run .autocomplete()
function after creating new elements: https://jsfiddle.net/hmfpkdxh/5/
$('#matrixtable tr').each(function () { $(this).append($(this).find(">*:last").clone()); $(this).find(">*:last input").val("").autocomplete({ source: ["c++", "java", "php", "coldfusion", "javascript", "asp", "ruby"] }); }); $('#matrixtable').append($('#matrixtable').find("tr:last").clone()); $('#matrixtable tr:last td input').val("").autocomplete({ source: ["c++", "java", "php", "coldfusion", "javascript", "asp", "ruby"] });
Comments
Post a Comment