jquery - How to create a name with as desired while reading for loop -
i reading json , construction table , during process reading inner array "video_details"
while reading inner array need take first 3 elements , if there more elements need put ... (3 dots )
so looks (3 dots after 1 2 three)
test 001 xxx 1 2 3 ... sertval var aajxresponse = { "tag_video_details": [{ "video_id": "369", "tag_name": "three", "video_name": "test 001", "video_file": "xxx", "video_details": [ "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten" ] }] } $(document).ready(function() { updatetable(aajxresponse); }); function updatetable(aajxresponse) { var html = '<tr class="existingvideos">\ <th width="20%">id</th>\n\ <th width="35%">course</th>\n\ <th width="30%">name</th>\n\ <th width="15%">sert</th>\n\ </tr>'; var span = '<span class="btn btn-sm btn-success btn-circle"></span> '; var v = aajxresponse.tag_video_details; (var = 0; < v.length; i++) { html += '<tr video-id="' + v[i].video_id + '" class="newvideos exercises-add-table-content">\ <td>' + capitalise(v[i].video_name) + '</td>\n\ <td><a href="' + v[i].video_file + '" target="_blank">' + v[i].video_file.split('/').pop() + '</a></td>\n'; var vd = v[i].video_details; html += '<td>\n'; (var j = 0; j < vd.length; j++) { if(j<3) { html += span.split("</span>").join(vd[j] + "</span>"); } } html += '</td>\n'; html += '<td>\n\ <i class="fa fa-check">sertval</i>\n\ </td>\n\ </tr>'; } $("#videosfromtagstable tbody").html(html); }
where edit text, add condition when count greater 3. have this:
if(j < 3) { html += span.split("</span>").join(vd[j] + "</span>"); }
add this:
else { html += span.split("</span>").join('...' + "</span>"); break; }
the break
there condition happens once, rather every additional element in loop. once '...'
added loop should exit because remaining elements aren't needed.
there are, imagine, number of ways accomplish same thing. suggestion in keeping logic have, refactor once it's working , tested.
Comments
Post a Comment