Home ng-repeat parameter in search, SpringBoot/AngularJS
Reply: 1

ng-repeat parameter in search, SpringBoot/AngularJS

makaveli
1#
makaveli Published in 2017-12-07 19:24:14Z

Here is my Repository:

@Repository
public interface DataRepository extends JpaRepository<Data, Long>{

@Query("SELECT d FROM Data d WHERE "
+ "(:name IS NULL OR d.name like :name) AND "
+ "(:minPriority IS NULL OR d.priority >= :minPriority) AND "
+ "(:maxPriority IS NULL OR d.priority <= :maxPriority) AND "
+ "( //needed code here )"                 
Page<Data> search(
@Param("name") String name, 
@Param("minPriority") Integer minPriority, 
@Param("maxPriority") Integer maxPriority,
@Param("nameOfEmployee") String name, int page);

}

I have problem with namrOfEmployee parameter. In my Angular I need to select one of values from ng-repeat:

<div class="form-group">
  <label for="employee">Employees</label> 
  <select class="form-control" ng-model="newEmployee.idEmployee"    
    id="employee">
    <option ng-selected="newEmployee.idEmployee == e.id" ng-repeat="e in   
    employees" value="{{ e.id }}"> {{ e.name }}</option>
  </select>
</div>  
<div class="form-group">
  <button class="btn btn-primary" ng-click="search()">Search</button>
</div>

Employee is different entity. Do I maybe need to join Data and Employee tables? Any ideas?

Here are Data and Employee classes:

@Entity
@Table(name="Data")
public class Data { 
@Id
@GeneratedValue
@Column(name="id")
private Long id;
@Column(name="name")
private String name;
@Column(name="description")
private String description;
@Column(name="priority")
private int priority;
}

@Entity
@Table(name="Employee")
public class Employee {
@Id
@GeneratedValue
@Column(name="id")
private Long id;
@Column(name="firstName")
private String firstName;
@Column(name="lastName")
private String lastName;
@Column(name="position")
private String position;
}
Brian
2#
Brian Reply to 2017-12-08 02:01:26Z

I am struggling a bit to understand your question here. There seem to be a few components. Primarily, your Employee and Data entities are not related from a code perspective. There is also a bit of design confusion. I've put together what seems to make sense from what you have here.

The below solution will provide this workflow:

UI -> Request & List Employees -> Select Employee -> Request & Display associated data.

From the AngularJS side, I would first make a request for your list of employees. If you don't have that implemented yet, it's just going to be another repository (ie. EmployeeRepository), and an exposed endpoint for listing them. At that point, you'll have an array of employees in your UI. Once one is selected, you would then pass the request to this search method, providing the supplied parameters. In return, you would get back the datum that is associated to that employee.

First, we need to update your @Entity models to have a relationship:

Here, we make use of @OneToMany and @ManyToOne. @OneToMany means that an Employee has many Data. @ManyToOne means that many datum have one Employee.

Employee.java

@Entity
@Table(name = "Employee")
public class Employee {
    @Id
    @GeneratedValue
    @Column(name = "id")
    private Long id;
    @Column(name = "firstName")
    private String firstName;
    @Column(name = "lastName")
    private String lastName;
    @Column(name = "position")
    private String position;

    @OneToMany
    private Set<Data> data;

    // Getters/Setters/Etc

}

Data.java

@Entity
@Table(name = "Data")
public class Data {
    @Id
    @GeneratedValue
    @Column(name = "id")
    private Long id;
    @Column(name = "name")
    private String name;
    @Column(name = "description")
    private String description;
    @Column(name = "priority")
    private int priority;

    @ManyToOne
    private Employee employee;

    // Getters/Setters/Etc

}

Now that they are related from a JPA perspective, we can update the query:

I noticed that you had defined int page. I've updated to use Pageable. I have also changed your parameter to employeeId, as it would appear that your > goal is to pull back data given the employee's ID.

DataRepository.java

@Query("SELECT d FROM Data d "
        + "WHERE d.employee.id = :employeeId AND "
        + "(:name IS NULL OR d.name like :name) AND "
        + "(:minPriority IS NULL OR d.priority >= :minPriority) AND "
        + "(:maxPriority IS NULL OR d.priority <= :maxPriority) ")
Page<Data> search(@Param("name") String name,
                    @Param("minPriority") Integer minPriority, 
                    @Param("maxPriority") Integer maxPriority,
                    @Param("employeeId") Long employeeId,
                    Pageable pageable);
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.325146 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO