5

I'm using ng2-smart-table for display data in angular 6 app. I have enable filter feature. Now I want to set default search as a text in all columns placeholder. I have searched a lot. But I'm not able to change placeholder of filter.

<ng2-smart-table [settings]="setting" [source]="dataSource"></ng2-smart-table>

In .ts file.

add: {
  confirmCreate: false
},
columns: {
   id: {
    title: 'ID',
    filter: true        
  },
  name: {
    title: 'First Name',
    filter: true                
  },
}
khushboo
  • 715
  • 2
  • 11
  • 24
  • Can you share your code? – kboul Sep 08 '18 at 06:56
  • @kboul I have update my question. Please check it. – khushboo Sep 08 '18 at 07:17
  • I am not sure I get you. I took a look [here](https://akveo.github.io/ng2-smart-table/#/examples/using-filters). Only input that has placeholder is the search input. Do you want to change Search input placeholder to something else? – kboul Sep 08 '18 at 07:24
  • Yes , I have already looked into that. It is just put one textbox static. But I need filter in all columns. – khushboo Sep 08 '18 at 07:32
  • 2
    As per `ng2-smart-table` implementation, there is not way to configure the placeholder for the filters. Filter placeholder is declared as `placeholder="{{ column.title }}"`. If you really need to implement then I am scared that you need to overlay it. – Sunil Singh Nov 06 '18 at 18:09
  • @SunilSingh Thanks for comment. :) – khushboo Nov 07 '18 at 17:39

3 Answers3

4

to change the place holder of ng2 smart table

Step 1: goto--> node_modules\ng2-smart-table\lib\data-set\column.js

add below lines in you var column ,

this.placeholder = '';

it will look like

var Column = (function () {
    function Column(id, settings, dataSet) {
        this.id = id;
        this.settings = settings;
        this.dataSet = dataSet;
        this.title = '';
        this.placeholder = '';
        this.type = '';
        this.class = '';
        this.isSortable = false;
        this.isEditable = true;
        this.isFilterable = false;
        this.sortDirection = '';
        this.defaultSortDirection = '';
        this.editor = { type: '', config: {}, component: null };
        this.filter = { type: '', config: {} };
        this.renderComponent = null;
        this.process();
    }

Step 2: on same file --> Add this.placeholder = this.settings['placeholder']; in Column.prototype.process = function () {},

it will look like this

 Column.prototype.process = function () {
        this.title = this.settings['title'];
        this.placeholder = this.settings['placeholder'];
        this.class = this.settings['class'];
        this.type = this.prepareType();
        this.editor = this.settings['editor'];
        this.filter = this.settings['filter'];
        this.renderComponent = this.settings['renderComponent'];
        this.isFilterable = typeof this.settings['filter'] === 'undefined' ? true : !!this.settings['filter'];
        this.defaultSortDirection = ['asc', 'desc']
            .indexOf(this.settings['sortDirection']) !== -1 ? this.settings['sortDirection'] : '';
        this.isSortable = typeof this.settings['sort'] === 'undefined' ? true : !!this.settings['sort'];
        this.isEditable = typeof this.settings['editable'] === 'undefined' ? true : !!this.settings['editable'];
        this.sortDirection = this.prepareSortDirection();
        this.compareFunction = this.settings['compareFunction'];
        this.valuePrepareFunction = this.settings['valuePrepareFunction'];
        this.filterFunction = this.settings['filterFunction'];
    };

step 3: goto node_modules\ng2-smart-table\components\filter\filter-types\input-filter.component.js and change the below line --> from

   Component({
        selector: 'input-filter',
        template: "\n    <input [(ngModel)]=\"query\"\n           [ngClass]=\"inputClass\"\n           [formControl]=\"inputControl\"\n           class=\"form-control\"\n           type=\"text\"\n           placeholder=\" {{ column.title}}\" />\n  ",
    }),

to:

Component({
            selector: 'input-filter',
            template: "\n    <input [(ngModel)]=\"query\"\n           [ngClass]=\"inputClass\"\n           [formControl]=\"inputControl\"\n           class=\"form-control\"\n           type=\"text\"\n           placeholder=\" {{ column.placeholder }}\" />\n  ",
        }),

step 4: goto your component.ts and add the below line in you column details like below -->

  columns: {
        ExamID: {
          title: this.translate.get("table.ExamID")["value"],
          **placeholder:"your place holder",**
          type: "string"
        },

you are ready to go

  • @ParshantKumar It works for me. Thanks for your answer. – khushboo Jan 21 '19 at 10:16
  • 1
    @ParshantKumar It works in my local fine. But When I make a build for live it doesn't take changes of module. So what should I do to take node module changes in live ? – khushboo Jan 28 '19 at 09:35
  • it's not a good practice to change the source code of the package inside node_modules because, what happens if you install the project in other machine? your changes will lost because it's not in the real package, is for that I did send a Pull request to the repo... – Rubén Soler Feb 21 '19 at 16:16
  • @khushboo make sure your npm cache verify first and then build the project. it should work fine. – Parshant Kumar Jun 19 '20 at 10:17
  • @Ruben i a hve requested to push this change in the node module, as far as they do not include in their node module it should work fine. – Parshant Kumar Jun 19 '20 at 10:18
3

actually there is a simple way to accomplish this and that is by making your own custom input text component, and provide it with any configurations you need...

that component could something like this:

import { Component, OnChanges, OnInit, SimpleChanges } from "@angular/core";
import { FormControl } from "@angular/forms";
import { DefaultFilter } from "ng2-smart-table";
import { debounceTime, distinctUntilChanged } from "rxjs/operators";

@Component({
  selector: "input-filter",
  template: `
    <input
      [ngClass]="inputClass"
      [formControl]="inputControl"
      class="form-control"
      type="text"
      placeholder="{{ column?.filter?.config?.placeholder || column.title }}"
    />
  `,
})
export class CustomInputTextFilterComponent extends DefaultFilter implements OnInit, OnChanges {
  inputControl = new FormControl();

  constructor() {
    super();
  }

  ngOnInit() {
    if (this.query) {
      this.inputControl.setValue(this.query);
    }
    this.inputControl.valueChanges.pipe(distinctUntilChanged(), debounceTime(this.delay)).subscribe((value: string) => {
      this.query = this.inputControl.value;
      this.setFilter();
    });
  }
  ngOnChanges(changes: SimpleChanges) {
    if (changes.query) {
      this.inputControl.setValue(this.query);
    }
  }
}

and you can use it when you initialize the columns like this:

initColumns(): void {
    this.columns = {
      nameEn: {
        title: "nameEn",
        type: "text",
        sort: true,
        filter: {
          type: "custom",
          component: CustomInputTextFilterComponent,
          config: { placeholder: "search here .." },
        }
      }
    };
  }
2

I have reviewed the implementation of this library on github and the placeholder gets it from the column.title parameter, so in theory you should to set this attribute on the column object when you declare your columns and the library will use that to set the placeholder.

Then you can not put a placeholder different to the title, except hack JJ

You can take a look at: https://github.com/akveo/ng2-smart-table/blob/master/src/ng2-smart-table/components/filter/filter-types/input-filter.component.ts#L15

REPO ISSUE TO ADD THE FEATURE: https://github.com/akveo/ng2-smart-table/issues/785

PR SENDED https://github.com/akveo/ng2-smart-table/pull/900

MY FORK WITH THE FEATURE https://github.com/RSginer/ng2-smart-table/tree/rsginer/allow-different-placeholder

How to use:

 settings = {
  columns: {
   id: {
    title: 'ID',
    placeholder: 'Different placeholder',
   },
Rubén Soler
  • 1,147
  • 8
  • 23
  • I have checked this link before. Can you show me it's example? – khushboo Nov 09 '18 at 05:51
  • Yes, but is pretty similar than your code: https://stackblitz.com/edit/angular-yngnnb – Rubén Soler Nov 09 '18 at 07:52
  • I suggest you that consider to make your own table component if you really need this feature or send an issue to the repo of that library. I can make a pull request to the library if you want.(edited) the issue exist: https://github.com/akveo/ng2-smart-table/issues/785 – Rubén Soler Nov 09 '18 at 07:53
  • I made a pull request right now on the repository.. we only can wait until the owner makes the merge or you can also use my fork, follow the progress on the PR link: https://github.com/akveo/ng2-smart-table/pull/900 – Rubén Soler Nov 09 '18 at 08:17
  • Sorry I was not available. Today I'll implement your ans and I'll update you. :) – khushboo Nov 12 '18 at 04:46
  • I have tried but it is not works for me. Can you please add any working screen shot ? – khushboo Nov 12 '18 at 05:17
  • Sure in the fork you have an example, you can clone the repo and checkout the branch rsginer/allow-different-placeholder and run npm run start – Rubén Soler Nov 12 '18 at 06:56
  • Hello @RubénSoler , I have add place holder as you show in example , I have add in settings but it didn't work for me. Can you help me to do that. ? – Sachin Shah Apr 16 '19 at 13:31
  • Yes , I have checked your fork code and make changes based on that in my node_module. I have set this code placeholder="{{ column.placeholder || column.title }}"/> and it is working fine. – Sachin Shah Apr 16 '19 at 13:49
  • I haven't update code in `lib/data-set/column.ts` file. Can you please tell me, Is it important to update change as per your fork ? – Sachin Shah Apr 16 '19 at 13:49
  • Yes, but, the ideal will that the owner of the repo merge my PR... because if you do npm install again the changes inside node_modules will disapear... – Rubén Soler Apr 16 '19 at 13:52
  • Another option it's build the module and upload your own build to npmjs.org with another name – Rubén Soler Apr 16 '19 at 13:53
  • @RubénSoler I have do some RND on this issue. And I found that I can install your fork as a npm. I have tried but not done by me. Can you help me to install your fork to my node module ? – Sachin Shah Apr 18 '19 at 04:49
  • Yes, let me find a minute to build it and upload as a new npm package and i'll send you the command to install it ASAP – Rubén Soler Apr 23 '19 at 09:55