删除排序列表上的问题

时间:2014-08-11 08:41:01

标签: javascript html ember.js

我有第一页显示城市列表,第二页显示城市详细信息。在详细信息页面中,我可以编辑或删除城市。编辑模式工作正常但删除操作不是:我只能删除已排序列表的第一项。

当我直接在列表页面上使用supp()动作时,一切正常。但是,如果我想从详细信息页面删除一个城市,使用supprimer()函数,只能删除已排序内容的第一项。

列表控制器:

App.VillesController = Ember.ArrayController.extend({

queryParams: ['perPage','page'],
page: 1,
perPage: 10,

isHelpVisible: false,
sortProperties: ['nom_comm'],
sortAscending: true,    

toDelete: null,

actions: {
    toggleHelpVisibility: function () {
          this.toggleProperty('isHelpVisible'); 
        },

    sortBy: function(field){
        if(field == this.sortProperties){ 
            this.toggleProperty('sortAscending'); 
        }
        else{
            this.set('sortAscending', true);
        }
        this.set('sortProperties', [field]);
    },

    selectPage: function(number){
        this.set('page',number);
    },
    supp: function(ville){
        var results = this.get('results');
        results.removeObject(ville);

    }
},

results: function(){
    this.set('page',1);
    var villes = this.get('arrangedContent'); 
    var searchTerm = this.get('searchTerm');
    var regex = new RegExp('^'+searchTerm,'gi');

    if(searchTerm){
        villes = villes.filter(function(ville){
            return regex.test(ville.get('id'));
        });
        return villes;
    }
    else{
        return villes;
    }

}.property('arrangedContent', 'searchTerm'),

isOnly: function(){
    var results = this.get('results');
    return results.get('length') <= 1 ;
}.property('results.length'),

totalPages: function(){
    var results = this.get('results');
    return Math.ceil(results.get('length') / this.get('perPage'));
}.property('perPage','results.length'),

pages: function(){
    var collection = Ember.A();
    for(var i = 0; i < this.get('totalPages'); i++){
        collection.pushObject(Ember.Object.create({
            number: i + 1
        }));
    }
    return collection;
}.property('totalPages'),

hasPages: function(){
    return this.get('totalPages') > 1;
}.property('totalPages'),

prevPage: function(){
    var page = this.get('page');
    var totalPages = this.get('totalPages');

    if(page > 1 && totalPages > 1)
        return page - 1;    
    else
        return null;

}.property('page', 'totalPages'),

nextPage: function(){
    var page = this.get('page');
    var totalPages = this.get('totalPages');

    if(page < totalPages && totalPages > 1)
        return page + 1;
    else
        return null;
}.property('page','totalPages'),

paginatedContent: function(){
    var results = this.get('results');
    var page = this.get('page');
    var totalPages = this.get('totalPages');

    if(page > totalPages)
        this.set('page',totalPages);

    var start = (this.get('page') - 1) * this.get('perPage');
    var end = start + this.get('perPage');

    return results.slice(start,end);

}.property('page','totalPages')

});

详情控制器:

App.VilleController = Ember.ObjectController.extend({
needs: 'villes', 
results : Ember.computed.alias('controllers.villes.results'),


isEditing: false,
isDisabled: true,
actions: {
    editer: function(){
        this.toggleProperty('isEditing');
        this.toggleProperty('isDisabled');
    },
    valider: function(){
        this.toggleProperty('isEditing');
        this.toggleProperty('isDisabled');      
    },
    supprimer: function(){
        var ville = this.get('model');
        var results = this.get('results');
        results.removeObject(ville);    

    }
}
});

0 个答案:

没有答案