【问题标题】:How to get double click event in angular-datatables如何在角度数据表中获取双击事件
【发布时间】:2018-07-01 23:14:14
【问题描述】:

我正在使用数据表和角度数据表。 如何检测数据表中的双击事件并获取行数据? 我找到了下面的代码,但我需要它的角度。

$(document).on("dblclick", "#myTable tr", function () {
    //code here
});

html

<table datatable="tblRecipe" dt-options="showCase.dtOptions" dt-columns="showCase.dtColumns" dt-instance="showCase.dtInstance" class="table table-bordered"></table>

controller.js

var app = angular.module('app', ['datatables'])
app.controller('MainController', function ($scope, $window, $http, $filter, $timeout, $compile, DTOptionsBuilder, DTColumnDefBuilder, DTColumnBuilder) {
    var vm = this;
    vm.dtInstance = {};
    vm.Recipes = {};
    vm.delete = deleteRow;
    vm.edit = editRow;
    vm.dtOptions = DTOptionsBuilder.newOptions()
    .withOption('ajax', {
        url: "/Recipes/GetAllRecipes",
        type: "POST"
    })
    .withOption('createdRow', createdRow)
    .withOption('select', true);
    vm.dtColumns = [
                    //...
DTColumnBuilder.newColumn(null).withTitle('Actions').notSortable().renderWith(actionsHtml)
        ];
        function actionsHtml(data, type, full, meta) {
            vm.Recipes[data.Id] = data;
            return '<a title="View"  href="javascript:void(0)" ng-click="showCase.view(showCase.Recipes[' + data.Id + '])">' +
                ' <i class="fa fa-eye" aria-hidden="true"></i>' + '</a>' + '<a title="Edit"  href="javascript:void(0)" ng-click="showCase.edit(showCase.Recipes[' + data.Id + '])">' +
                ' <i class="fa fa-pencil"></i>' + '</a>' + '<a title="Delete" href="javascript:void(0)" ng-click="showCase.delete(showCase.Recipes[' + data.Id + '])" )"="">' + ' <i class="fa fa-trash-o"></i>' + '</a>';
        };
        //...
});
});

【问题讨论】:

  • 你在使用 angularjs 1 吗?
  • @Sathiyaraj 是的,angularjs 1.6.6
  • 能贴出代码吗?
  • @Sajeetharan 我没有我要问的双击事件的角度代码。我将编辑我的问题标题。

标签: javascript angularjs angular-datatables


【解决方案1】:

我正在使用数据表和角度数据表。如何检测双重 数据表中的单击事件并获取行数据?我找到了代码 下面,但我需要它的角度。

DataTables jQuery,Angular-DataTables 只是让它们在 AngularJS 上下文中工作的指令。除非您正在渲染“角度方式”,即datatable="ng",否则如果您希望任何指令起作用,则需要$compile 回调中的表格、行或每个单元格。

只需像使用 jQuery DataTables 一样使用委托事件处理程序,并通过 dtInstance 获取数据,其中包含 API 实例:

$('#tableId').on('dblclick', 'tbody tr', function() {
  console.log( $scope.dtInstance.DataTable.row(this).data() )
})

更新:http://plnkr.co/edit/PdrKxxxsDFdNSzgf8c07?p=preview

【讨论】:

    【解决方案2】:

    我们可以编写自定义指令逻辑并解决问题。能否请您参考directive

    html代码

    <div ng-controller="MainController">
    <div id="goodDiv" ngdblclick>Click Here</div>
    <div id="goodDiv">Don't Click Here</div>
    </div>
    

    角码

    angular.module("myapp", []).
    controller("MainController", ["$scope", function($scope){
    
    }]).
    directive("ngdblclick", ['$compile', function($compile){
        'use strict'
        return{
            compile : function(elements, attributes){
                return{
                    restrict: 'C',
                    post : function(scope, element, attribute){
                        var isSingleClick = true;
                        element.bind('click', function(){
                            setTimeout(function(){
                                if(isSingleClick){
                                    console.log("It's a single click");
                                    return;
                                }
                            }, 500);
                        });
    
                        element.bind('dblclick', function(){
                            console.log("It's a double click");
                            isSingleClick = false;
                            setTimeout(function(){
                                isSingleClick = true;
                                return;
                            }, 500);
                        });
                    }
                };
            }
        };
    }]);
    

    css 样式:

    #goodDiv{
        width : 100px;
        height : 100px;
        background-color : green;
    }
    

    您可以找到您的元素并为特定元素呈现事件(#myTable li)

    【讨论】:

    • 1、为什么要在指令中注入$compile? 2. 为什么使用setTimeout? 3. 为什么不使用angularjs自己的ng-dblclick呢?
    • 1. $compile是动态重新绑定元素并获取值的范围,2.我们需要了解下一次连续点击事件监听的间隔3.我们无法在角度元素注入中绑定ng-双击事件。
    • 1.您根本没有使用$compile$compile 隐含在指令方法中。 2. 你不应该将setTimeout 与 angularjs 一起使用,但$timeout 3. 你的解决方案将永远无法使用角度数据表,因为 Angular 不知道 DT 注入的任何 DOM 元素。您只需将一个没有$compile 就无法工作的标准指令替换为另一个没有$compile 就无法工作的指令......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多