0

我目前有一个非常疯狂的代码,可以在动态表中单击和展开行。我的问题是我需要使表格中的最后一列不可点击,我不知道该怎么做。我搜索了互联网并尝试了不同的方法,但似乎没有任何帮助。

这是我的桌子:

<table id="myTable">
                    <tr>
                        <th>Name</th>
                        <th>Type</th>
                        <th>Details</th>
                    </tr>
           {{#each companies}}

                   <tr class="record">
                       <td>
                          {{this.name}}
                       </td>
                       <td>
                          {{this.type}}
                      </td>
                      <td id="details">
                          <img style="height: 20px; width: 20px" src="img/arrow_right.png">
                     </td>
                 </tr>
         {{/each}}
  </table>

我想让最后一列(ID =“详细信息”)不可点击。

这是我的疯狂点击事件:

$("#myTable").on("click", "tr.record", function () {
            var rowIndex = this.rowIndex,
                    services = null;
            var self = this;
            $.ajax({
                url: "api/relation",
                type: 'GET',
                success: function (response) {
                    if (response[rowIndex - 1].relationStats) {
                        services = response[rowIndex - 1].relationStats.PROVIDES_SERVICE;
                    }

                    if (services > 0 && $(self).hasClass('record active')) {
                        console.log("CLOSING");
                        $(self.nextSibling).remove();
                        $(self).removeClass('active');

                    } else if (services > 0) {
                        console.log("OPENING");
                        var openDetails = $('<tr/>').addClass('openDetails').insertAfter(self);
                        $(self).addClass('active');
                        var td = $('<td/>').addClass('td').appendTo(openDetails);
                        $('<p/>').html("Provides service(s):&nbsp").appendTo(td);
                        $('<td/>').html(services).appendTo(openDetails);

                    }
                }
            });
        });

任何帮助,将不胜感激!

4

3 回答 3

3

而不是注册点击处理程序以tr使用td和过滤掉最后一个孩子

$("#myTable").on("click", "td:not(:last-child)", function () {
    var self = $(this).closest('tr').get(0), rowIndex = self.rowIndex,
        services = null;
    $.ajax({
        url: "api/relation",
        type: 'GET',
        success: function (response) {
            if (response[rowIndex - 1].relationStats) {
                services = response[rowIndex - 1].relationStats.PROVIDES_SERVICE;
            }

            if (services > 0 && $(self).hasClass('record active')) {
                console.log("CLOSING");
                $(self.nextSibling).remove();
                $(self).removeClass('active');

            } else if (services > 0) {
                console.log("OPENING");
                var openDetails = $('<tr/>').addClass('openDetails').insertAfter(self);
                $(self).addClass('active');
                var td = $('<td/>').addClass('td').appendTo(openDetails);
                $('<p/>').html("Provides service(s):&nbsp").appendTo(td);
                $('<td/>').html(services).appendTo(openDetails);

            }
        }
    });
});
于 2013-08-23T07:03:00.500 回答
0
$("#myTable").on("click", "#details", function (e) {
    e.stopPropagation();
});
于 2013-08-23T07:15:00.240 回答
0

我认为你应该使用

$("#myTable").on("click", "td:not(:last)", function () {.....}

而不是not(:last-child), 如果情况是:

<tr>
   <td>....</td>
   <td>....</td>
   <td class="not record">....</td>
</tr>
<tr>
   <td>....</td>
   <td>....</td>
   <td class="record">....</td>
</tr>

参考last-child , last

于 2013-08-23T09:43:06.210 回答