• 技术文章 >web前端 >js教程

    angularjs怎样在table里增加tr

    php中世界最好的语言php中世界最好的语言2018-04-12 16:02:55原创697

    这次给大家带来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>

    这样就可以完成我们的初始要求,不过可以在上面稍微改动下,会实现更棒的功能,下面一行可以自动收缩:

    相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

    推荐阅读:

    vue全局注册和局部注册使用详解

    easyui日期时间框在IE中的兼容性如何处理

    以上就是angularjs怎样在table里增加tr的详细内容,更多请关注php中文网其它相关文章!

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    专题推荐:javascript angularjs table
    上一篇:深入了解commonJS的概念以及使用方法 下一篇:js如何实现轮播效果
    VIP课程(WEB全栈开发)

    相关文章推荐

    • 【腾讯云】年中优惠,「专享618元」优惠券!• 浅析IDEA中如何开发Angular• 整理总结JavaScript常见的BOM操作• 四种方法搞定JavaScript创建多个对象• Angular的:host、:host-context、::ng-deep选择器• 什么是异步资源?浅析Node实现异步资源上下文共享的方法
    1/1

    PHP中文网