Dynamic Search Form with AngularJS

Image

Hey there, I'm facing a challenge that I need help with. I am in the process of creating a dynamic search form using Angular.js. The form needs to have dynamic criteria and display results dynamically based on user input.

The form allows users to add multiple search conditions, which are stored in an array. My goal is to link the values of form controls to scope variables using models that correspond to my conditions array.

Here is a snippet of the basic HTML structure:

<div ng-repeat="condition in conditions">
    <select >
        <option ng-repeat="column in columns" value="">{{column.name}}</option>
    </select>
    <select >
        <option>Like</option>
        <option>=</option>
        <option><</option>
        <option>></option>
        <option><></option><span>-</span>
    </select><span>-</span>
    <input type="text" style="height: 20px;" value="{{condition.value}}">
</div>

Below are the scope variables being used:

   $scope.conditions = [];
        $scope.columns = [
                            {name:'Location',value:'tb1'},
                            {name:'Place',value:'tb3'},
                            {name:'Purchase Date',value:'dt3'},
                            {name:'First Name',value:'tr5'},
                            {name:'Last Name',value:'tf6'},
                            {name:'e mail',value:'em6'},
                            {name:'Place',value:'tb3'},
                            {name:'Address',value:'ad1'}
                        ];

        $scope.addCondition = function()
        {
            $scope.conditions.push({value: 'val', cond: 'cond', col :'col'});
        };


        $scope.log = function(){
          console.log($scope);
        };

I have attempted to use ng-model in combination with ng-repeat. I hope to achieve a final result similar to the following:

  $post_data = [
    {'dd1','=','1'},
    {'tb2','LIKE','Mark'},
    {'tb3','LIKE','Lamorav'},
    {'tb7','<','2015-01-04'},
    {'tb4','LIKE','Kochi'}
    ]

Your advice on this matter would be greatly appreciated.

Answer №1

If you are looking to maintain all the conditions added in $scope.conditions and have a $post_data with all the user-entered values for final submission, then I have a solution for you.

You can define ng-model with an index as shown below:

<div ng-controller="MyCtrl">

<div ng-repeat="condition in conditions">
<select ng-model="conditions[$index].val">
    <option ng-repeat="column in columns" >{{column.name}}</option>
</select>
<select ng-model="conditions[$index].cond">
    <option>Like</option>
    <option>=</option>
    <option><</option>
    <option>></option>
    <option><></option><span>-</span>
</select><span>-</span>
<input type="text" style="height: 20px;" value="{{condition}}" ng-model="conditions[$index].col">
 <a ng-click="addCondition(conditions[$index])">Add</a>

The add condition function will look like this:

 $scope.addCondition = function(condition)
    {

        $scope.conditions.push({val: condition.val, cond: condition.cond, col :condition.col});

        console.log($scope.conditions);
    };

To assist you further, I have set up a working JsFiddle. However, please note that you may need to customize the code based on your specific requirements as your question is not entirely clear to me.

JSFiddle

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

How is it possible that my form is able to save data into the database even without any

I am considering adding a captcha process to my form and I am brainstorming some logic for it. I downloaded a login from Google, but I am confused why my form is still storing data into the database using action=' ' instead of action="register.ph ...

Accessing the next and previous elements using jQuery

Aug: <input type="text" value="100000" name="targetMonth_8" id="targetMonth_8" class="targetMonth" disabled> Sep: <input type="text" value="100000" name="targetMonth_9" id="targetMonth_9" class="targetMonth" disabled> Oct: <input type="text" ...

My content is being obstructed by a single-page navigation system

I attempted to create a simplified version of the issue I am facing. Basically, I am working on a header with navigation that stays at the top of the page while scrolling. The problem arises when clicking on a section in the navigation. The screen scrolls ...

What is causing my Directive to trigger the error "Error: $injector:unpr Unknown Provider"?

I have been diligently working on updating my Controllers, Factories, and Directives to align with the recommended Angular Style Guide for Angular Snippets. So far, I have successfully refactored the Controllers and Factories to comply with the new style ...

Modifying the style of a specific row when the form is submitted

My webpage contains nested records, with each nested record displaying a total number of clicks in a div labeled "count". I am looking to increment the count by 1 for each specific record when the button with a class of view is clicked. Currently, clicking ...

Simulated external prerequisite

//user.js const database = require('database'); exports.createUser = function(req, res){ let user = req.body; if( validateUser(user) ) { database.insertUser(user); //redirect } else { //render new page with the user data ...

Creating a dynamic directive in AngularJS: Learn how to add or remove it from the DOM based on specific conditions

Suppose I have a customized modal directive that looks like this: return { restrict: 'E', templateUrl: 'modal.html', scope: { modalContent: "&", modalOpen: "&" } } And in the HTML: <ng-modal modal-content="co ...

Implementing JSON object parsing within a Liferay hook

Currently, I am working on developing a hook where I need to parse a JSON string. My initial thought was to use the json-simple API for this purpose, so I included the corresponding JAR in the build path. However, upon deploying the hook, an error message ...

Steps for refreshing the content within a div without having to reload the entire page following an ajax request

I am trying to achieve the task of reloading the content of a specific div on a webpage without refreshing the entire page. My goal is to reload the div after uploading a photo so that the newly uploaded photo can be displayed from the database. However, t ...

How to access form elements within a submit function without specifically defining the form name

I have a form that I am submitting using a submit function. However, instead of using document id for submission variable, I am utilizing classes. $(".modalform").submit(function(event) { /* prevent form from submitting normally */ event.preventDefa ...

Perhaps dividing numbers with regular expressions

My HTML textarea allows users to input numeric serial numbers in various formats - either inline, separated by any character, or in columns from an Excel file. The serial codes are either 14 characters long if the first character is "1," or always 15 char ...

What is the best way to apply a className to a component using styled-components in React?

NavigationStyles.js import styled from 'styled-components'; export const Navigation = styled.navwidth: 100%; ; export const MobileNavMenu = styled.ul` height: 80px; .navbar_list_class { font-size: 2rem; background-co ...

Getting the iframe onload event in an ASP.NET application

I have integrated ReportViewer to display SSRS reports on my .aspx page. However, since the ReportViewer is rendered as an iframe in the browser, I am looking for a way to trigger a JavaScript function every time the iframe loads. Using window.onload w ...

Finding and choosing a date from an ng-model datepicker using Selenium Webdriver - a guide

I am encountering an issue where I can open the date picker pop-up calendar with a click, but I cannot select a specific date from it. This is due to the input field being read-only, which means that any input provided through sendkeys or JavascriptExecuto ...

Is there a way to send just one element from my object using POST method?

Recently, I started learning Angular and have been practicing different exercises. Currently, I am working on a function to add a comment. newComment ( issue: Issue, key$: string) { let message = JSON.stringify( issue); let headers = new Headers ...

I'm struggling to figure out why my code is throwing an Unexpected token error. What am I missing here?

I keep encountering an Unexpected token error in my code, specifically with a closing parenthesis ). What exactly does this error signify? Experimented by adding and removing parentheses as well as curly brackets. const getUserChoice = userInput => {u ...

Node.js continually throwing 503 errors

After successfully installing and running Node.js for a while, I attempted to execute a standard Hello World program. The code snippet looked something like this: var http = require("http"); http.createServer(function (request, response) { response.w ...

Organizing requirejs and angularjs code structure into separate files

Looking to organize my Angular application with requirejs by separating controllers, services, and directives into different files. Hoping to achieve this structure: src/ components/ Navigation/ index.js module.js NavigationCon ...

Enhance the worth of text box

Is there a way to adjust the value of a textbox by one on keyup, keydown, or tap events? Check out this example. Here is the HTML structure: <div class="right-content" id="rc-0" style="height: 250px;"> <div class="right-cont-main" id="rcm-0" s ...

Vue.js Ready event is not firing

In my Vue function, I have two methods. The first method, postStatus, is used to save a post when the user clicks on the save button. The second method, getPosts, is used to retrieve all previous posts from the database for that user. Below is the Vue.js ...