简体   繁体   English

如何在ember cli项目中使用jquery数据表

[英]How to use jquery datatables in ember cli project

I'm working on an ember project where I'm using jQuery plugin data-tables. 我正在一个余烬项目中,我正在使用jQuery插件数据表。 I've included the plugin in vendor folder and referencing it from ember-cli. 我已将该插件包含在供应商文件夹中,并从ember-cli引用它。 So far so good, but I want to change the data in the table dynamically as per the user selection on the list. 到目前为止,一切都很好,但是我想根据列表上的用户选择动态更改表中的数据。 The way I implemented is 我实施的方式是

index.hbs
{{#data-table tHeaders=tHeaders elementId="myTableID" data=model}}
   {{#each model as |model|}}
        <tr>
            <td>{{#link-to "anotherRoute" model.id}}{{model.id}}{{/link-to}}</td>
            <td>{{ model.type }}</td>
            <td><button {{action 'openModal' model.config}}>{{ model.type }}</button></td>
        </tr>
    {{/each}}
{{/data-table}}

data-table.hbs
 <thead>
    <tr>
       {{#each tHeaders as |tHeaders|}}
          <th>{{tHeaders}}</th>
       {{/each}}
    </tr>
 </thead>
 <tbody>
       {{yield}}
 </tbody>

data-table.js
import Ember from 'ember';
export default Ember.Component.extend({
   tagName:'table',
   classNames:['table','table-bordered','table-striped','mb-none'],
   uiSetup: function(){
      $('#myTableID').DataTable();
   }.on('didInsertElement').observes('data.[]')
});

Whenever I click on my list (on left side of my page), I'm doing a transitionToRoute to the same route but the model changes since the id of the selected element changes. 每当我单击列表(在页面的左侧)时,我都会向相同的路径执行transitionToRoute,但是由于所选元素的ID更改,因此模型也会更改。

When I selected different id on the left side, my model is changing and the datatable is reflecting the new data but with the existing data below to that. 当我在左侧选择其他ID时,我的模型正在更改,数据表正在反映新数据,但现有数据在其下方。 Now, when I click sort on the headers the table is resetting to the previous data by removing the latest data. 现在,当我单击标题上的sort时,表格将通过删除最新数据而重置为先前的数据。

I've been on this issue since past 3 days but nothing is changing. 自过去三天以来,我一直在处理此问题,但没有任何变化。 The other way I approached to this problem is 解决这个问题的另一种方法是

index.hbs
{{data-table tHeaders=tHeaders elementId="myTableID"}}

data-table.hbs
 <thead>
    <tr>
       {{#each tHeaders as |tHeaders|}}
          <th>{{tHeaders}}</th>
       {{/each}}
    </tr>
 </thead>
 <tbody>
       {{yield}}
 </tbody>

data-table.js
import Ember from 'ember';
export default Ember.Component.extend({
   tagName:'table',
   classNames:['table','table-bordered','table-striped','mb-none'],
   uiSetup: function(){
    this._super(...arguments);
    var table=$('#myTableID').DataTable();
    table.clear();
    var JSON=[];
    for (var i = this.get('data.currentState').length - 1; i >= 0; i--)   {
        var innerJSON=[];
        innerJSON.push("<a {{{action 'link1Clicked' 123}}}>"+this.get('data.currentState')[i].id+"</a>");
        innerJSON.push(this.get('data.currentState')[i]._data.type);
        innerJSON.push("<button {{{action 'link2Clicked' 123}}}>"+this.get('data.currentState')[i]._data.config+"</button>");
        if (this.get('data.currentState')[i].id) {
            JSON.push(innerJSON);
        }
    }
    table.rows.add(JSON);
    table.draw();
}.on('didInsertElement').observes('data.[]'),
actions:{
   link1Clicked(){
      console.log('hello');
   }
}
});

For the second approach, everything is working fine but I could not capture action items since those elements were created dynamically and not treated as ember elements instead they are pure HTML elements. 对于第二种方法,一切正常,但是我无法捕获操作项,因为这些元素是动态创建的,没有被视为余烬元素,而是纯HTML元素。

Any idea, where I'm doing wrong or is there any much cleaner approach for using jQuery data tables in ember cli project. 任何想法,我做错了什么,还是在ember cli项目中使用jQuery数据表的方法都更简洁。

I also tried using ember-cli-jquery-datatables addon which works only for static data but not for dynamic data. 我还尝试使用ember-cli-jquery-datatables插件,该插件仅适用于静态数据,不适用于动态数据。

Thanks in advance, any help would be appreciated. 在此先感谢您的帮助。

I'm answering my own question. 我在回答我自己的问题。

For those who are facing the same problems in integrating jQuery data-table plugin, I'd suggest rather than putting your effort on fitting jQuery datatables plugin in to ember app, use ember-models-table add-on which is clearly in sync with ember data models. 对于那些在集成jQuery数据表插件中面临相同问题的人,我建议您不要将精力放在将jQuery数据表插件安装到ember应用程序中,而应使用ember-models-table插件,该插件显然与余烬数据模型。 Its very simple to integrate, 整合起来非常简单,

  1. ember install ember-models-table ember安装ember-models-table
  2. Use it in any of your template/file as 在以下任何模板/文件中使用它

     {{models-table data=model columns=columns}} 
  3. Declare columns as an array in your component.js file as 在component.js文件中将列声明为数组,如下所示:

     columns: [ { "propertyName": "id", "title": "ID" }, { "propertyName": "firstName", "title": "First Name" }, { "propertyName": "lastName", "title": "Last Name" }, { "propertyName": "city", "title": "City" } ] 

    There are many custom methods availabe with add on, for complete list of the features please refer to http://onechiporenko.github.io/ember-models-table 有许多附加的自定义方法,有关功能的完整列表,请参阅http://onechiporenko.github.io/ember-models-table

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM