如何更新ng2-smart-table中的占位符文本?

时间:2018-09-08 06:34:56

标签: angular placeholder ng2-smart-table

我正在使用ng2-smart-tableangular 6应用中显示数据。我有启用过滤器功能。现在,我想将默认search设置为所有columns占位符中的文本。我搜了很多。但是我无法更改过滤器的占位符。

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

在.ts文件中。

add: {
  confirmCreate: false
},
columns: {
   id: {
    title: 'ID',
    filter: true        
  },
  name: {
    title: 'First Name',
    filter: true                
  },
}

2 个答案:

答案 0 :(得分:4)

更改ng2智能表的占位符

步骤1:转到-> node_modules \ ng2-smart-table \ lib \ data-set \ column.js

在var列中添加以下行,

this.placeholder = '';

它看起来像

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();
    }

第2步:在同一文件上-> 在Column.prototype.process = function(){},

中添加this.placeholder = this.settings['placeholder'];

它看起来像这样

 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'];
    };

第3步:转到node_modules \ ng2-smart-table \ components \ filter \ filter-types \ input-filter.component.js并更改以下行-> 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  ",
    }),

收件人:

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  ",
        }),

第4步:转到您的component.ts,并在您的列详细信息中添加以下行,如下所示->

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

您准备好了

答案 1 :(得分:2)

我已经检查了该库在github上的实现,占位符从column.title参数获取,因此从理论上讲,当您声明列时,应该在列对象上设置此属性,并且该库将使用设置占位符。

那么,除了hack JJ之外,您不能放置与标题不同的占位符

您可以看一下: https://github.com/akveo/ng2-smart-table/blob/master/src/ng2-smart-table/components/filter/filter-types/input-filter.component.ts#L15

请增补功能https://github.com/akveo/ng2-smart-table/issues/785

已发送PR https://github.com/akveo/ng2-smart-table/pull/900

我的功能叉 https://github.com/RSginer/ng2-smart-table/tree/rsginer/allow-different-placeholder

使用方法

 settings = {
  columns: {
   id: {
    title: 'ID',
    placeholder: 'Different placeholder',
   },