0

I use jQuery DataTable in my code. I need to make changes to the dataTables_scrollBody class (add my own scroll bar) because on linux the touch screen (chrome) does not work with the swype, and the scroll button itself is very small (not convenient).

I implemented this by injecting jQuery code into the angular component. But I would like to know how to make my code better.

component.ts:

import { Component, OnInit, ViewChild, ElementRef, Renderer2, Host, ViewContainerRef, TemplateRef, ContentChild } from '@angular/core';
import { Product } from '../models/Product';
declare var $: any;

@Component({
  selector: 'app-sales-touch',
  templateUrl: './sales-touch.component.html',
  styleUrls: ['./sales-touch.component.less']
})



export class SalesTouchComponent implements OnInit {

  @ViewChild('table') table;
  // jQuery DOM для таблицы с продуктами
  dataTable: any;
  @ViewChild('modalWindow') modal;
  // jQuery DOM для модалки
  modalWindow: any;

  @ContentChild('test1') tst;

  // Переменная для ввода в окне
  inputModal: string = "";

  // Команда-ключ-идентификатор case для модального окна
  Command: string = null;
  // Command:string = "AddProduct";

  Products: Array<Product> = [];

  constructor(
    private saleDOM: ElementRef
  ) {

  }

  ngOnInit(): void {
    this.dataTable = $(this.table.nativeElement);
    this.modalWindow = $(this.modal.nativeElement);

    this.dataTable.DataTable({
      "scrollY": this.dataTable.closest(".card").height() - 46,
      "scrollCollapse": true,
      "paging": false,
      "searching": false,
      "info": false,
      "ordering": false,
      "language": {
        "zeroRecords": " "
      }
    });
    $(this.saleDOM.nativeElement).find(".dataTables_wrapper, .dataTables_scrollHeadInner").width("100%");

    let table = $(".dataTables_scrollBody .table");
    let scrollBody = $(".dataTables_scrollBody").css("overflow", "hidden");
    let scrollHead = $(".dataTables_scrollHead");
    let scroll = $(".dataTables_scroll");

    $(".dataTables_scrollBody").remove();

    scroll.append(
      "<div class='d-flex'><div class='col-auto' id='colDT'><div class='row' id='colDTable'>" +
      "</div></div>" +
      "<div class='col-auto m-0 p-0' id='scrollBar'>" +
      "</div></div>"
    );

    $("#colDT").prop("style").setProperty("width", (scroll.width() - 40).toString() + "px", "important");
    scroll.find("#colDTable").append(scrollBody);

    $("#scrollBar").append("<button id='listUp' class='btn btn-success' style='margin:0;padding: 0;width: 40px;height: 40px;top: 0;left: 0;position: absolute;'></button><button id='scrollBtn' class='btn btn-danger' style='margin:0;padding: 0;width: 40px;height: 40px;top: 40px;left: 0;position: absolute;'></button><button id='listDown' class='btn btn-success' style='margin:0;padding: 0;width: 40px;height: 40px;bottom: 1px;left: 0;position: absolute;'></button>");

    $("#listUp").on('click', function () {
      scrollBody.prop("scrollTop", scrollBody.prop("scrollTop") - 100);
    });

    $("#listDown").on('click', function () {
      scrollBody.prop("scrollTop", scrollBody.prop("scrollTop") + 100);
    });

    let flag = false;
    let y = 70;
    $("#scrollBtn").on('mousemove', function (e) {
      // console.log($("#listDown").prop("offsetTop"));
      if (flag) {
        $("#scrollBtn").css("top", e.clientY - y);
        if ($("#scrollBtn").prop("offsetTop") < 40) {
          $("#scrollBtn").css("top",40);
        }
        if ($("#scrollBtn").prop("offsetTop") > $("#listDown").prop("offsetTop") - 40){
          $("#scrollBtn").css("top",$("#listDown").prop("offsetTop") - 40);
        }

        //считаю %
        //100%
        let pes100 = $("#listDown").prop("offsetTop") - 40 - 40;
        //нулевое расположение кнопки
        let topB = $("#scrollBtn").prop("offsetTop") - 40;
        // прцент сдвига
        let pes = (topB*100)/pes100;

        console.log(scrollBody);

        //двигаю сам список на %
        //100%
        let listAllPes = scrollBody.prop("scrollHeight") - scrollBody.prop("offsetHeight");
        let t =(listAllPes * pes) / 100;
        console.log(t);
        scrollBody.prop("scrollTop", (listAllPes * pes) / 100);

      }
    });
    $("#scrollBtn").on('mousedown', function (e) {
      flag = true;
      //y = e.clientY - 40;
      //console.log(e.clientY);
    });
    $("#scrollBtn").on('mouseup', function () {
      flag = false;
    });
    // $("#scrollBtn").on('mouseleave', function () {
    //   if (flag) {
    //     flag = false;
    //   }
    // });
  }
}

result:

enter image description here

1 Answers1

0

It is better to use PrimeNg or AngularMaterial while you are developing Angular projects.

Angular material is built by the team who developed Angular.

The Angular Material has limited themes, whereas Primeng provides many. Also there are some custom components provided by primeng, that are built from same angular material.

Behind the scenes, PrimeNg uses the same angular material for base, and extend it by adding more themes and components to create their components.

If you are not worried at all about theme and style, then you can use Angular Material.

StepUp
  • 36,391
  • 15
  • 88
  • 148
  • How can I increase the scroll bar buttons in Angular Material for DataTables? – Виталий Шебаниц Sep 11 '19 at 07:28
  • @ВиталийШебаниц [you can use pagination](https://material.angular.io/components/table/overview) to avoid showing scrollbar. Or about styling scrollbat you can read [this post](https://stackoverflow.com/questions/4053220/css-scrollbar-width) and [this](https://css-tricks.com/almanac/properties/s/scrollbar-width/) – StepUp Sep 11 '19 at 07:42
  • In chrome :: - webkit-scrollbar works well, but it doesn’t work in FireFox. Is there a way around this? – Виталий Шебаниц Sep 11 '19 at 07:53
  • @ВиталийШебаниц please see [this answer](https://stackoverflow.com/a/54101063/405015) and [this one answer](https://stackoverflow.com/a/53739309/405015) for Firefox. – StepUp Sep 11 '19 at 08:04