Add search feature to Users

This commit is contained in:
Ivan Kristianto 2022-02-12 12:52:22 +07:00
parent 366fcf0bed
commit d3a5a3d0d6
2 changed files with 45 additions and 14 deletions

View File

@ -3,6 +3,14 @@
<div class="card-header"> <div class="card-header">
<h3 class="card-title"><%- i18n('users', 'title') %></h3> <h3 class="card-title"><%- i18n('users', 'title') %></h3>
<div class="card-options"> <div class="card-options">
<form class="search-form" role="search">
<div class="input-icon">
<span class="input-icon-addon">
<i class="fe fe-search"></i>
</span>
<input name="source-query" type="text" value="" class="form-control form-control-sm" placeholder="Search User…" aria-label="Search in Users">
</div>
</form>
<a href="#" class="btn btn-outline-teal btn-sm ml-2 add-item"><%- i18n('users', 'add') %></a> <a href="#" class="btn btn-outline-teal btn-sm ml-2 add-item"><%- i18n('users', 'add') %></a>
</div> </div>
</div> </div>

View File

@ -12,7 +12,29 @@ module.exports = Mn.View.extend({
ui: { ui: {
list_region: '.list-region', list_region: '.list-region',
add: '.add-item', add: '.add-item',
dimmer: '.dimmer' dimmer: '.dimmer',
search: '.search-form',
query: 'input[name="source-query"]'
},
fetch: App.Api.Users.getAll,
showData: function(response) {
this.showChildView('list_region', new ListView({
collection: new UserModel.Collection(response)
}));
},
showError: function(err) {
this.showChildView('list_region', new ErrorView({
code: err.code,
message: err.message,
retry: function () {
App.Controller.showUsers();
}
}));
console.error(err);
}, },
regions: { regions: {
@ -23,30 +45,31 @@ module.exports = Mn.View.extend({
'click @ui.add': function (e) { 'click @ui.add': function (e) {
e.preventDefault(); e.preventDefault();
App.Controller.showUserForm(new UserModel.Model()); App.Controller.showUserForm(new UserModel.Model());
},
'submit @ui.search': function (e) {
e.preventDefault();
let query = this.ui.query.val();
this.fetch(['permissions'], query)
.then(response => this.showData(response))
.catch(err => {
this.showError(err);
});
} }
}, },
onRender: function () { onRender: function () {
let view = this; let view = this;
App.Api.Users.getAll(['permissions']) view.fetch(['permissions'])
.then(response => { .then(response => {
if (!view.isDestroyed() && response && response.length) { if (!view.isDestroyed() && response && response.length) {
view.showChildView('list_region', new ListView({ view.showData(response);
collection: new UserModel.Collection(response)
}));
} }
}) })
.catch(err => { .catch(err => {
view.showChildView('list_region', new ErrorView({ view.showError(err);
code: err.code,
message: err.message,
retry: function () {
App.Controller.showUsers();
}
}));
console.error(err);
}) })
.then(() => { .then(() => {
view.ui.dimmer.removeClass('active'); view.ui.dimmer.removeClass('active');