Coder Social home page Coder Social logo

attrash-islam / ng-infinite-autocomplete Goto Github PK

View Code? Open in Web Editor NEW
4.0 2.0 1.0 40 KB

AngularJS 1.x infinite-autocomplete wrapper

License: MIT License

JavaScript 9.38% TypeScript 90.62%
autocomplete auto-complete web-component infinite input angular infinite-scroll

ng-infinite-autocomplete's Introduction

Coverage Status

ng-inifinte-autocomplete

AngularJS 1.x Wrapper for infinite-autocomplete library

Note: Whenever I mention the CORE, then I'm targeting the infinite-autocomplete library

Install

via npm:

$ npm i --save ng-infinite-autocomplete

via bower:

$ bower i -S ng-infinite-autocomplete

Developer section

In the package.json scripts we've 4 basic tasks:

build - Build the code once

build:watch - Build the code and watch changes

test - Run tests once

test:watch - Run tests and watch changes

Usage Example

import * as angular from 'angular';
import 'ng-infinite-autocomplete';
//or <script src="bower_components/ng-infinite-autocomplete/dist/index.js"></script>

//inject infinite-autocomplete as dependency for myApp module
angular.module('myApp', ['infinite-autocomplete'])
...

Controller:

  angular.module('myApp')
   .controller('MainCtrl', ['$http', '$q', function($http, $q){
   vm.onSelectHandler = function($element, $data) {
      console.log(arguments);
   }
   
    vm.data = [
                { text: 'Islam Attrash', value: 1},
                { text: 'Shai Reznik', value: 2},
                { text: 'Uri Shaked', value: 3},
                { text: 'Salsabel Eawissat', value: 4}
            ];
    vm.getData = function($text, $page, $fetchSize) {
        var deferred = $q.defer();
        $http.get("http://localhost:5000/data?text="+ $text + "&page=" + $page + "&fetchSize=" + $fetchSize)
          .then(function(res) {
              deferred.resolve(res.data);
          });
        return deferred.promise;
    }
   });

Template:

<div ng-controller="MainCtrl as vm">
  <!-- For Static data -->
  <ng-infinite-autocomplete
    data="vm.data"
    on-select="vm.onSelectHandler($element, $data)"
  ></ng-infinite-autocomplete>

  <!-- For external API -->
  <ng-infinite-autocomplete
    fetch-size="8"
    get-data-from-api="vm.getData($text, $page, $fetchSize)"
  ></ng-infinite-autocomplete>
</div>

Live Demo (Default Style)

Override Implementations

See this section on CORE, you pass a class via customizedInput, customizedOptions attributes

Directive Inputs/Outputs

  /**
   * current value
   */
  ngModel?: string;
  /**
   * data static source
   */
  data?:Array<IOption>;
  /**
   * on-select event output handler when choosing an option
   */
  onSelect?:Function($element, $data);
  /**
    * on-loading-state-change event output handler when choosing an option
    */
  onLoadingStateChange?($loadingState: boolean);
  /**
    * on-error event output handler when exception thrown
    */
  onError?(error: $error);
  /**
   * max height for the options
   */
  maxHeight?:string;
  /**
   * data dynamic api source
   */
  getDataFromApi?($text:string, $page:number, $fetchSize:number):es6Promise<Array<any>>;
  /**
   * Chunk fetch size
   */
  fetchSize?:number,
  /**
   * Customized input class to override the default input
   */
  customizedInput?:IInputCompoenentConstructor;
  /**
   * Customized options class to override the default input
   */
  customizedOptions?:IOptionsComponentConstructor;
  /**
   * treat data source as immutable - To improve $watch process
   */
   immutable?

Restrictions regards Attributes

  • You must choose to pass data or getDataFromApi depends on whether the data is static or dynamic
  • data attribute must be an array of { text: 'some text', value: 1}, and value could be any type (number, string, object..)
  • max-height should be a string
  • fetch-size should be a number
  • customizedInput should extend InputComponent [For more See CORE]
  • customizedOptions should extend OptionComponent [For more See CORE]

Directive Example

  <ng-infinite-autocomplete
            ng-model="vm.currentText"
            data="vm.data"
            immutable
            fetch-size="vm.fetchSize"
            on-select="vm.onSelectHandler($element, $data)"
            on-error="vm.onErrorHandler($error)"
            on-loading-state-change="vm.onLoadingStateChangeHandler($loadingState)"
            max-height="vm.maxHeight"
            customized-input="vm.CustomInput"
            customized-options="vm.CustomOptions">
  </ng-infinite-autocomplete>

This directive will use vm.data as a static data source, and the number of fetched data is vm.fetchSize in every scroll and/or search, the data will be treated as immutable and then you need to create new instance to update the options list for the autocomplete, it will run vm.onSelectHandler($element, $data) when selecting an option passing the clicked HTNLElement as the first argument and the data as the second one ({text: 'text', value: 'hi'}), and the maxmium height for the scrollable options will be vm.maxHeight (It will be overriden if not supplying enough scrollable area for options), and will use vm.CustomInput for implementing the input, and vm.CustomOptions for implementing the custom options also, loading states can be caught in vm.onLoadingStateChangeHandler and every exception will be passed to vm.onErrorHandler.

You can exclude any attribute not desired as you like following the restrictions mentioned above.

ng-infinite-autocomplete's People

Contributors

attrash-islam avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

usertesting

ng-infinite-autocomplete's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.