Combining the powers of AngularJS and Django

Just diving into the world of Django and AngularJs, feeling a bit lost.

I have my data flowing in from an SQL database to a table and I'm struggling to implement sorting and searching functionalities.

I have the basics covered with Django model and view, but it's the HTML and JS that are causing me trouble.

The data is being displayed without any issues, but the search and sort features aren't functioning properly.

This is what my HTML looks like:

<div class="box" ng-app="sortApp" ng-controller="mainController">

    <form>
        <div class="form-group">
            <div class="input-group">
                <div class="input-group-addon"><i class="fa fa-search"></i></div>

                <input type="text" class="form-control" placeholder="Search table" ng-model="searchTable">

            </div>
        </div>
    </form>
    <table class="table table-bordered table-striped">
        <thead>
            <tr>
                <td>
                    <a href="#" ng-click="sortType = 'name'; sortReverse = !sortReverse">
                        Name
                        <span ng-show="sortType == 'name' && !sortReverse" class="fa fa-caret-down"></span>
                        <span ng-show="sortType == 'name' && sortReverse" class="fa fa-caret-up"></span>
                      </a>
                </td>
                <td>
                    <a href="#" ng-click="sortType = 'cost'; sortReverse = !sortReverse">
                      Cost
                        <span ng-show="sortType == 'cost' && !sortReverse" class="fa fa-caret-down"></span>
                        <span ng-show="sortType == 'cost' && sortReverse" class="fa fa-caret-up"></span>
                      </a>
                </td>
                <td>
                    <a href="#" ng-click="sortType = 'resource_type'; sortReverse = !sortReverse">
                      Resource Type
                        <span ng-show="sortType == 'resource_type' && !sortReverse" class="fa fa-caret-down"></span>
                        <span ng-show="sortType == 'resource_type' && sortReverse" class="fa fa-caret-up"></span>
                      </a>
                </td>
                <td>
                    <a href="#" ng-click="sortType = 'unit'; sortReverse = !sortReverse">
                      Unit
                        <span ng-show="sortType == 'unit' && !sortReverse" class="fa fa-caret-down"></span>
                        <span ng-show="sortType == 'unit' && sortReverse" class="fa fa-caret-up"></span>
                      </a>
                </td>
            </tr>

        </thead>

        <tbody>
            {% for resource in resource_list %}
            <tr ng-repeat="resource in resource_list | orderBy:sortType:sortReverse | filter:searchTable">
                <td>{{ resource.resource_name }}</td>
                <td>{{ resource.resource_cost }}</td>
                <td>{{ resource.resource_type }}</td>
                <td>{{ resource.resource_unit }}</td>
            </tr>
            {% endfor resource%}

        </tbody>

    </table>
</div>

JS:

    // Resource Table
angular.module('sortApp', [])

.controller('mainController', function($scope) {
  $scope.sortType     = 'name'; // set the default sort type
  $scope.sortReverse  = false;  // set the default sort order
  $scope.searchTable   = '';     // set the default search/filter term

});

It seems like the issue might be related to this line

<tr ng-repeat="resource in resource_list | orderBy:sortType:sortReverse | filter:searchTable">

I've seen ng-repeat used when the data is coming from JavaScript, but what should I use if I only want to implement sorting and searching?

Any assistance on this matter would be highly appreciated

Answer №1

Eliminate the for loop; it is unnecessary when using ng-repeat.

Also, make sure to review the spelling details. This should be effective. Have you checked the console?

Answer №2

I do not detect any resources within your scope. I have created an example for you below:

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>

<div ng-app="currencyApp" ng-controller="currencyCtrl">

<ul>
  <li ng-repeat="x in resources | orderBy:'country' | filter: 'a' ">
    {{ x.country + ', ' + x.currency }}
  </li>
</ul>

</div>

<script>
angular.module('currencyApp', []).controller('currencyCtrl', function($scope) {
    $scope.resources = [
        {currency:'a',country:'Norway'},
        {currency:'b',country:'Sweden'},
        {currency:'c',country:'England'},
        {currency:'d',country:'Norway'},
        {currency:'e',country:'Denmark'},
        {currency:'g',country:'Sweden'},
        {currency:'h',country:'Denmark'},
        {currency:'i',country:'England'},
        {currency:'j',country:'Norway'}
        ];
});
</script>

</body>
</html>

Instead of adding a text field for filtering, I have hard-coded the filter to provide a functional example.

By the way, for the sorting functionality, here is the syntax:

{{ array-you-are-looping | orderBy : expression : true/false }}

If you set the third parameter to true:

{{ array-you-are-looping | orderBy : expression : true}}

it will sort in reverse order.

If you set the third parameter to false:

{{ array-you-are-looping | orderBy : expression : false }}

it will sort in ascending order.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for dynamically assigning values to scope variables in AngularJS

My ng-model looks like this: <tr ng-repeat="user in users"> <input type="text" ng-model="code[user.id]"/> When I set $scope.code = {0: 'value'};, it works fine. However, if I try to pass a dynamic value like: var index = 0; $scope ...

Unable to refresh the fullcalendar section following an ajax post click

Currently developing a calendar using fullcalendar. I have created an ajax button that retrieves events from another php page. The first click on the ajax button works fine, displaying a nice month calendar with events. However, my issue arises when I cl ...

Yarn combined with Webpack fails to execute post-compilation tasks

When using yarn and running yarn prod, I encountered the following error: https://i.stack.imgur.com/2emFk.jpg It seems to be stuck at this particular part of the code: mix.then(() => { execSync(`npm run rtlcss ${__dirname}/Assets/css/admin.css ${__dir ...

City-based Address Suggestions with Google API Places

I have been struggling to find a solution to this specific issue without any luck. Your assistance would be greatly appreciated. Currently, I am attempting to implement autocomplete address functionality using Google API with the following code: var inpu ...

What steps are involved in configuring Meteor-Angular with Twitter Bootstrap?

I'm currently struggling with setting up Twitter Bootstrap in my Meteor-Angular project. Here's what I've done so far: Installed Bower meteor add mquandalle:bower Added a dependency to bower.json: { ... "dependencies": { "a ...

Refreshing a page occurs every 30 seconds or upon the user submitting a form

My PHP page consists of various includes for different sections of a video website. One of these sections is a comments area where users can submit their feedback to a database successfully. However, I want to ensure that only the specific included page/di ...

Would it be frowned upon in JavaScript to use "if (somestring in {'oneoption':false, 'secondoption':false})"?

Is the use of this construct considered a bad practice in JavaScript and could it lead to unexpected behavior? if (e.target.name in {name: '', number: ''}) { // do something } This code checks if the 'name' attribute of an ...

Received a String instead of an Object while attempting to parse JSON data

Currently, my goal is to parse a JSON string using jQuery var parsedData = $.parseJSON('{"graph_info": "{}"}'); I assumed that typeof(parsedData.graph_data) would be an Object but surprisingly it is returning as a string. Can someone guide me o ...

Checking the integrity of JSON information

Currently in my AJAX requests, I am manually validating each JSON entry by using the "key" in json.some_location.keys() method repeatedly. Is there a more efficient way to accomplish this, such as XML validation? Additionally, I am open to using validatio ...

Incorporating an Edit button with an icon into a react-bootstrap-table2

Is there a way to insert buttons in the Edit column of my table so I can easily edit a row? I believe there should be a method to personalize the column and incorporate icons as shown in the example image. Sample Image of What I want to do: import React ...

Twilio/Django experiencing issues with receiving SMS responses

When a user texts a twilio number, I want to initiate a series of questions. For new users, a new "Caller" should be created. If they have interacted before, I aim to retrieve the last question asked and present them with the next appropriate one. Unfortun ...

The File Filter feature of Angular's ng2-file-upload is not functioning correctly for PNG files in the Internet Explorer

I am encountering an issue with the file uploader plugin ng2-file-upload when trying to upload a PNG file using Internet Explorer 11. For some reason, the PNG files are not being added to the queue, even though all other allowed file types work perfectly f ...

What is the easiest way to retrieve a basic date with the month represented by a numerical

Struggling to retrieve the date in the format "Oct 29". I attempted using split but it varies every day. This is what I've come up with so far. let currentDate = new Date().toLocaleDateString('en-US', { month: 'short', day: 'n ...

What is the best way to retrieve my files stored on my express server?

I have successfully uploaded my images to my server using Multer, but now I am struggling to serve that file. When I try to access the path: http://localhost:3001/public/backend/public/uploads/user-admin-1556247519876.PNG, I receive a 404 error. I believe ...

Experience a seamless front/back DIV transition with a mouseover effect similar to the ones on USAT

Recently, I was tasked with creating a mouseover transition effect for a div similar to the one used on USAToday's website. The structure of the boxes on the site includes: <div class="asset"> <div class="front">'image and some t ...

Having trouble capturing the 'notificationclick' event in the service worker when using Firebase messaging with Nuxt.js and Vue.js?

Experiencing difficulties in detecting events other than install, activate, or push in my firebase-messaging-sw.js. Notifications are being received and displayed, but I am unable to detect the event handler for notificationclick. When a firebase notificat ...

AngularJS: Issue with Variable Value Rendering

I recently started working with Angular. In my JavaScript file, I have the following code: App.controller('ProductController', ['$scope', 'ProductService', function ($scope, ProductService) { console.clear(); console. ...

Exporting data from AngularJS to Excel is not functioning correctly in Internet Explorer. Additionally, the exported Excel file does not display properly in Firefox and

I have been attempting to Export a table from a jsp page to Excel using AngularJs. Is Angularjs not compatible with IE? I am also encountering this error SCRIPT5009: 'Node' is undefined In Chrome and Firefox, the Excel file only opens when save ...

Having issues with Phonegap's getPhoto functionality

Even though the documentation here seems to be effective, I encountered an issue when implementing the code into my application. The getPhoto function returns 'content://media/external/images/media/16053' without loading the image into the img el ...

Utilizing ES6 Map type in TypeScript for Angular 2 Response Data Transfer Object Definition

Is it possible to utilize the es6 Map type in an HTTP Response DTO? Let's consider an Angular 2 request: public loadFoos(): Observable<FoosWrapper> { return this.http.get("/api/foo") .map(res => res.json()); } Now, take a loo ...