【问题标题】:How to prevent row click function on specific column in datatable?如何防止数据表中特定列的行点击功能?
【发布时间】:2016-08-11 17:31:39
【问题描述】:

我正在处理一个带有可点击行的 javascript 数据表。每一行都有 onclick 功能,但在我的一列中,我有不同的链接打开 jquery 对话框,在这一列上我想禁用行点击方法,怎么做? 这是我实现的行点击功能

$(rec' tbody').on( 'click', 'tr', function () {

});

【问题讨论】:

    标签: javascript jquery datatable


    【解决方案1】:

    您必须禁用该特定列的行点击

    $('rec' tbody'').on('click', 'td', function () {
    
             if ($(this).index() == 4 ) { // provide index of your column in which you prevent row click here is column of 4 index
                 return;
             }
    
                // you can do additional functionality by clicking on this column here                         
        });
    

    【讨论】:

      【解决方案2】:

      您可以检查点击的是不是锚点

      $(rec' tbody').on( 'click', 'tr', function (evt) {
          if ( $(evt.target).is("a") ) {
              return;
          }
          /* do whatever here */
      });
      

      或其他选项是停止从链接/单元传播事件。

      【讨论】:

      • 这是最好的答案,只需使用标准的 js/jquery 功能。很好的答案!
      【解决方案3】:

      使用类或其他东西来识别您不想被点击的行/列,并检查点击事件是否来自该元素并使用event.preventDefault() 取消操作。

      【讨论】:

        【解决方案4】:

        您可以尝试为每一列设置类,您希望可点击特定的内容,不可点击的其他内容。

        <table>
          <tr>
            <td class="clickable">click this</td>
            <td class="clickable">and this</td>
            <td class="something-else">but not this</td>
          </tr>
        </table>
        

        然后让你的 jQuery 在类上做一些事情,而不是 &lt;tr&gt;

        $(".clickable").click(function(){
          alert('hello');
        });
        

        【讨论】:

          【解决方案5】:

          接受的答案有效,但可能有人希望与行交互而不是单击的列。对于这些情况,试试这个:

          table.on("click", 'tbody tr td', function() {
              if ($(this).index() == 10) return false;
              var $row = $(this).parent()  // this is the row you wanted
              var theRow =   table.row($row);
              //var artcode = theRow.data()[4];
              // Further logic goes here
          });
          

          【讨论】:

            猜你喜欢
            • 2016-11-24
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-03-24
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多