Forráskód Böngészése

Add search feature to Users

Ivan Kristianto 3 éve
szülő
commit
d3a5a3d0d6
2 módosított fájl, 45 hozzáadás és 14 törlés
  1. 8 0
      frontend/js/app/users/main.ejs
  2. 37 14
      frontend/js/app/users/main.js

+ 8 - 0
frontend/js/app/users/main.ejs

@@ -3,6 +3,14 @@
     <div class="card-header">
         <h3 class="card-title"><%- i18n('users', 'title') %></h3>
         <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>
         </div>
     </div>

+ 37 - 14
frontend/js/app/users/main.js

@@ -12,7 +12,29 @@ module.exports = Mn.View.extend({
     ui: {
         list_region: '.list-region',
         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: {
@@ -23,30 +45,31 @@ module.exports = Mn.View.extend({
         'click @ui.add': function (e) {
             e.preventDefault();
             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 () {
         let view = this;
 
-        App.Api.Users.getAll(['permissions'])
+        view.fetch(['permissions'])
             .then(response => {
                 if (!view.isDestroyed() && response && response.length) {
-                    view.showChildView('list_region', new ListView({
-                        collection: new UserModel.Collection(response)
-                    }));
+                    view.showData(response);
                 }
             })
             .catch(err => {
-                view.showChildView('list_region', new ErrorView({
-                    code:    err.code,
-                    message: err.message,
-                    retry:   function () {
-                        App.Controller.showUsers();
-                    }
-                }));
-
-                console.error(err);
+                view.showError(err);
             })
             .then(() => {
                 view.ui.dimmer.removeClass('active');