If else condition in jQuery + append function -
i have condition:
$(document).on("click", "#save, .delrow, #closeaddbutton", function (e) { if ($(this).is("#save")) { $($("#template").html()).appendto("#datatables-example"); $(".rowvalue").append($("#textvalue").val()).attr("class", "rowvalues"); $(".taskvalue").append($("#task").val()).attr("class", "taskvalues"); $(".prolevalue").append($("#primaryrole").val()).attr("class", "rolevalues"); var stuff = $("#secondaryrole").val(); var result = (stuff.substr(stuff.length-2, 2) == ", ")? stuff.substr(0, stuff.length-2):stuff; $(".srolevalue").append(result).attr("class", "rolevalues"); $(".actions").append('<a href="#" class="edit" data-toggle="modal" data-target="#mymodaledit"><i class="fa fa-edit fa-fw"></i></a>' + '<a href="#" class="delrow"><i class="fa fa-trash-o fa-fw"></i></a>').attr("class", ""); /* update numbering */ updateroworder(); if($('#datatables-example tbody tr').length == 2) { $('#datatables-example tbody tr:first').find('.movedown').show(); $('#datatables-example tbody tr:last').find('.moveup').show(); } else if ($('#datatables-example tbody tr').length > 2) { $('#datatables-example tbody tr:first').find('.movedown').show(); $('#datatables-example tbody tr').find('.moveupdown').show(); $('#datatables-example tbody tr:last').find('.moveup').show(); } else { } } }); });
this html:
<table class="table table-bordered" id="datatables-example"> <thead> <tr> <td>order</td> <td>activity</td> <td>task code</td> <td>primary role code</td> <td>secondary role code</td> </tr> </thead> <tbody> <script id="template" type="text/template"> <tr class="move"> <td class="id"></td> <td><p class="rowvalue"></p></td> <td><p class="taskvalue"></p></td> <td><p class="prolevalue"></p></td> <td><p class="srolevalue"></p></td> <td><p class="actions"></p></td> <td><p class="moveup" style="display:none;"><a href="#" class="up"><i class="fa fa-arrow-up fa-fw"></i></a></p> <p class="moveupdown" style="display:none;"> <a href="#" class="up"><i class="fa fa-arrow-up fa-fw"></i></a> <a href="#" class="down"><i class="fa fa-arrow-down fa-fw"></i></a> </p> <p class="movedown" style="display:none;"><a href="#" class="down"><i class="fa fa-arrow-down fa-fw"></i></a></p> </td> </tr> </script> </tbody> </table> if click 'add' button append html codes. how can insert if..else statement in code if table row 1: not insert html codes; if has 2 table rows: first table row, insert class 'down' , last table row, insert class 'up'; , if has 3 table rows: first table row, insert class 'down', second table row, insert class 'up' , 'down' , third table row, insert class 'down'. summary, there should class 'up' first table row , class 'down last table row, if 2 or more table rows.
$('#addbutton').click(function(){ var count = $('#datatables-example tbody tr').length; if(count > 1) { $(#datatables-example tbody tr:first-child).addclass( "up" ); // add class first row if(count > 2) { for(i=1;i<(count-1); i++) { $(#datatables-example tbody tr).eq(i).addclass( "up down" ); //add class intermediate rows } } $(#datatables-example tbody tr:last-child).addclass( "down" ); // add class last row } });
Comments
Post a Comment