您好,欢迎访问一九零五行业门户网

angularjs给table增加tr的方法

本文主要和大家分享angularjs实现table增加tr的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧,希望能帮助到大家。
需求:
上面是一个table,运用了
<tr ng-repeat="rule in formdata.rulelist track by $index">
循环显示。现在的一个需求是:需要在每行添加一个字段,不过不能在同一行显示,需要在下一行显示。我首先想到了直接加个,不过没有办法换行。在下面再加个也不行。只能依赖强大的angulajs,自定义指令。下面我们就开始。
1 自定义指令
.directive(    'kbntablerow',    function($compile) {     return {     restrict : 'a',     link : function(scope, element) {     element.after('<tr>');    function expressdeschtml() {    var detailhtml = '<td></td><td colspan="5">'    + '<p ng-show="rule.type!==1">'     + '<p class="col-xs-9 row">'  + ' <input type="text" class="form-control" ng-model="rule.exprdesc"readonly ">' + '</p>' +'</p>' + '</td>'; return detailhtml;     }   },  templateurl : 'libs/kbntable/table_row/rule.html'       };      });
2 rule.html是原来的里的内容
<td class="form-control-static">  <p class="form-control-static">{{$index+1}}</p> </td> <td>  <p class="form-control-static" ng-show="rule.type===1" style="text-align: -webkit-left;">   {{rule.rightvar.desc}}</p>  <p ng-show="rule.type!==1">   <p class="col-xs-9 row">    <input type="text" class="form-control" ng-model="rule.rightvar.desc" readonly title="{{rule.rightvar.desc}}">   </p>   <p class="col-xs-3 ">    <button class="btn btn-warning" ng-click="showrightvar(rule,'rightvar')">设置</button>   </p>  </p> </td> <td class="form-control-static" ng-show="formdata.exectype == 't02'">  <p class="form-control-static" style="padding-top: 0;">   <input type="text" class="form-control" ng-model="rule.score" title="{{rule.score}}" />  </p> </td> <td class="td-button" style="padding-left: 0; padding-right: 1px;">  <button class="btn btn-danger" ng-click="del(rule)">删除</button> <input type="hidden" ng-model="rule.enable" /> </td> <td class="td-button" style="padding: 8px 0;">  <button class="btn btn-danger" ng-click="disabledrule(rule, $event)">   <span ng-if="rule.enable == 0">启用</span> <span ng-if="rule.enable == 1">禁用</span>  </button> </td>
不需要改变,原来是什么,这里就写什么。
3 初始页面里的tr循环部分,用我们新建的指令改写:
<p class="row">    <p class="col-xs-12 row">     <h4 class="col-xs-12">      <b>表达式设置</b>     </h4>    </p>    <p class="col-xs-12">     <p class="row">      <p class="col-xs-10">       <table class="table text-center">        <tr>         <th ng-click="toggleall()">           <i class="fa discover-table-open-icon" ng-class="{ 'fa-caret-down': formdata.on, 'fa-caret-right': !formdata.on }">            </i>         </th>         <th width="45px">序号</th>         <th>左变量</th>         <th>操作符</th>         <th>右变量</th>         <th width="75px" ng-show="formdata.exectype == 't02'">分值</th>         <th colspan="2">操作</th>         <th></th>        </tr>        <tbody>         <tr ng-repeat="rule in formdata.rulelist track by $index" kbn-table-row class="discover-table-row"></tr>        </tbody>       </table>      </p>      <p class="col-xs-1">       <button class="btn btn-info" ng-click="addrule()">新增</button>      </p>     </p>    </p>
这样就可以完成我们的初始要求,不过可以在上面稍微改动下,会实现更棒的功能,下面一行可以自动收缩:
相关推荐:
table实现双击可编辑、添加、删除行
jquery获取table某列的值详解
jquery实现table排序功能实例分享
以上就是angularjs给table增加tr的方法的详细内容。
其它类似信息

推荐信息