简体   繁体   中英

How to Use Jquery Datatables in Ember 2.0

I picked up learning emberJS lately and I have had issues doing some basic things I would have done while not using this framework. The main issues I have had was using jquery plugins , in this case jquery datatables

in my component's component.js

import Ember from 'ember';
export default Ember.Component.extend({
    didInsertElement: function(model){
        Ember.run.scheduleOnce('afterRender', this, function(model) {
            this.$(".datatables").DataTable();
        });
    }
});

in my component's template.hbs

<table class="table table-hover datatables">
    <thead>
        <tr>
            <th>Course Name</th>
            <th>Course Title</th>
            <th class="text-center">Actions</th>
        </tr>
    </thead>
    <tbody>
        {{#each courses as |course|}}
        <tr>
            <td> {{ course.name }} </td>
            <td> {{ course.title }} </td>
            <td  class="text-center"> {{#link-to 'courses.edit' course }} Edit {{/link-to}} </td>
        </tr>
        {{/each}}
    </tbody>
</table>

**then i used the component like :- **

{{#course-datatable courses=model}}{{/course-datatable}}

I would appreciate a demo accompanied with answers.

cheers

Ok so i haven't done a component of the jquery datatable plugin. But yes for other Jquery plugins, it would be more or less like this. If you are building your own component: Add the Datatable js files to include inside your BrocFile

Run in ember client

ember g my-jquery-datatable

this will generate the component in the generated hbs fill in the general html you would use

    <table id="example" class="display" cellspacing="0" width="100%">
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Position</th>

                </tr>
         </thead>
    {{#each modelWithData}}
    <tr>
    <td>this.name</td>
    <td>this.position</td>
    </tr>
    {{/each}}
</table>

then on you js generated file initiate the plugin in the didInsertElementMethod

export default Ember.Component.extend({
    didInsertElement(){
       this.$('#example').DataTable();
}
});

then to be able to use this table in other components or controllers you can do

{{my-jquery-datatable modelWithData=otherArrayWithTheTableAttributes}}

Hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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