angularjs实现table增加tr

需求:
angularjs实现table增加tr_第1张图片
上面是一个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('');

            function expressDescHtml() {
            var detailHtml = '5">'
            + '
rule.type!==1">' + '
9 row">' + ' " class="form-control" ng-model="rule.exprDesc"readonly ">' + '
' +'
' + ''; return detailHtml; } }, templateUrl : 'libs/kbnTable/table_row/rule.html' }; });

2 rule.html是原来的里的内容

<td class="form-control-static">
    <div class="form-control-static">{{$index+1}}div>
td>
<td>
    <div class="form-control-static" ng-show="rule.type===1"
        style="text-align: -webkit-left;">   {{rule.rightVar.desc}}div>
    <div ng-show="rule.type!==1">
        <div class="col-xs-9 row">
            <input type="text" class="form-control" ng-model="rule.rightVar.desc"
                readonly title="{{rule.rightVar.desc}}">
        div>
        <div class="col-xs-3 ">
            <button class="btn btn-warning"
                ng-click="showRightVar(rule,'rightVar')">设置button>
        div>
    div>
td>
<td class="form-control-static" ng-show="formData.execType == 't02'">
    <div class="form-control-static" style="padding-top: 0;">
        <input type="text" class="form-control" ng-model="rule.score"
            title="{{rule.score}}" />
    div>
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循环部分,用我们新建的指令改写:

<div class="row">
            <div class="col-xs-12 row">
                <h4 class="col-xs-12">
                    <b>表达式设置b>
                h4>
            div>
            <div class="col-xs-12">
                <div class="row">
                    <div 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>
                    div>
                    <div class="col-xs-1">
                        <button class="btn btn-info" ng-click="addRule()">新增button>
                    div>
                div>
            div>

这样就可以完成我们的初始要求,不过可以在上面稍微改动下,会实现更棒的功能,下面一行可以自动收缩:
angularjs实现table增加tr_第2张图片

你可能感兴趣的:(angularjs)