Applying filter to a dropdown list - ngoptions - angularjs

I have a method that returns the list of custom types, those values are being used in both the dropdown lists, but now I want to remove a subset of them based on the type and then add to first drop down. I am using angular js, asp.net mvc. What is the best way to apply filter even before data is being rendered and keeping the same method.
Below is the method in the controller which returns names and departments as a json response. Now I want this method to return two different json objects one with subset of the current set being returned based on department they belong to.
Public JsonResult GetDetails()
{
List<Cust> Customers = new List<Cust>();
Customers = GetCustomerDetails();
var name = Customers.Select(e => new{e.custname}).Distinct().ToList();
var dept = Customers.Select(e => new{e.deptname}).Distinct().ToList();
var response = new{CustomerNames = name, CustomerDepartments = dept};
return Json(response, JsonRequestBehaviour.AllowGet();
}

Related

How to search a column in a given database table using Entity-framework?

I wanted to provide my site with search feature. How can I search a string in a table?
public class SearchController : Controller
{
HOXATEntities db = new HOXATEntities();
public ActionResult Index(string search, int? page)
{
var pageNumber = page ?? 1;
ViewData["Posts"] = db.Posts.ToPagedList(pageNumber, 5);
ViewData["Search"] = search;
return View();
}
}
The search string is passed and it needs to be compared or whatever from the entity
You could use:
var posts = db.Posts.Where(x => x.nameOfPost == search);
return View(posts.ToPagedList(pageNumber, 5));
If you want to search for the name of the post. Otherwise just change it to whatever attribute of post you want to search out. Also you can pop .ToList() on the first line above so it becomes:
var posts = db.Posts.Where(x => x.nameOfPost == search).ToList()
But do notice that all the posts that match the search will be loaded into memory so the list can be populated and be ready for use. If you don't want to load it as a list just used the first example where the returned type will be IQueryable.
Then you can alter your view to expect the paged list of posts as the model and then it can render the view based on this.

Dynamically fetch all the related lists for an Object (for eg. Account)

I am working on a project which requires dynamically fetch related child items for a particular record - I only need those related child objects for which the current record has related lists in the page layout or the current user profile has access to this.
You can use with something like this function to get related objects and after use dynamic SOSL query
public static map<string,string> getRelatedObjects(string masterObjectName){
map<string,string> relatedObjectsMap = new map<string,string>();
list<Schema.Childrelationship> relatedObjectsList = Schema.getGlobalDescribe().get(masterObjectName).getdescribe().getChildRelationships();
for (Schema.Childrelationship relatedObject : relatedObjectsList) {
if(relatedObject.getChildSObject().getDescribe().isUpdateable()
&&
relatedObject.getChildSObject().getDescribe().getKeyPrefix()!=null
&&
!relatedObject.getChildSObject().getDescribe().isCustomSetting()
&&
relatedObject.getChildSObject().getDescribe().isCreateable()
)
relatedObjectsMap.put(relatedObject.getChildSObject().getDescribe().getName(),relatedObject.getChildSObject().getDescribe().getLabel());
}
return relatedObjectsMap;
}

how to get rows what I want in ADF

How do get this simple task done in ADF -
Based on some parameter I want a row to be retrieved from a view object programmatically. I have no idea how this could be done. IF I am not using ADF my business method would have a query like below and then return whatever details i want in the form on object.
*select * from abcTable where abccolumn = param1;*
param1 is an input from a jsf page. I capture that input and based on that input i need to query another database table (which will be in the form of View object in the ADF) to retrieve additional details and fill up some other components in the jsf page. How do I get this task done. I am trying to get instance of the view object but i do not seem to find any method using which I could retrieve only limited rows i want based on a where clause. The executeQuery method does not return anything (weird case).
You can filter viewObject programmatically and can get rows-
you can filter viewObject using 2 methods
1. where clause
2. filterdRows
see- Get Filtered Rows From View Object in Oracle ADF
The VO has a setWhereClause method that you can use to add/modify a where clause to the query.
You can also pre-define a VO with a bind parameter query.
Some more info on the UI side :
ADF Query with Parameters and List of Values and
ADF Query with Parameters and List of Values - Part II
Bind that parameter to a variable in your bean. Let's say you want
to search the value from and input text:
On page:
<af:inputText id="it8" binding="#{pageFlowScope.<YOURBEAN>.inputSearchBox}"/>
In your bean:
private RichInputText inputSearchBox;
public void setInputSearchBox(RichInputText inputSearchBox) {
this.inputSearchBox= inputSearchBox;
}
public RichInputText getInputSearchBox() {
return inputSearchBox;
}
Make a method in the bean that would do the search:
On page:
<af:commandButton text="search" id="cb6" actionListener="#{pageFlowScope.<YOURBEAN>.search}"/>
In bean:
public void search(ActionEvent actionEvent) {
}
In this method you will need to get the ViewObject from AppModuleImpl:
BindingContext bindingContext = BindingContext.getCurrent();
DCDataControl dc =bindingContext.findDataControl("YOURAPPMODULEDATACONTROL");
AppModuleImpl appM = (AppModuleImpl )dc.getDataProvider();
ViewObjectImpl vo = appM.getYourVO();
Create and apply a view criteria on that viewObject with the text that you entered in the input:
String searchValue = null;
//get the value from the search field
if (inputSearchBox.getValue() != null) {
searchValue = inputSearchBox.getValue().toString();
}
ViewCriteria vc = vo.createViewCriteria();
ViewCriteriaRow vcRow = vc.createViewCriteriaRow();
vcRow.setAttribute("Field you want to search by", searchValue);
vc.addRow(vcRow);
vo.applyViewCriteria(vc);
vo.executeQuery();
Now the ViewObject is filtered by your search value.
If you want to go through the result and save some VO values from that line you found you would need to make a row iterator, iterate through it and store the values you need in some variables:
RowSetIterator rsi = vo.getRowSetIterator();
String valueToGet = null;
while (rsi.hasNext()){
Row r = rsi.next();
valueToGet = (String)r.getAttribute("<WHAT ATTRIBUTE YOU WANT TO GET>");
}

ASP.NET MVC dropdownlist with entity framework

I'm new to asp.net mvc and the entity framework and i'm trying to make a dropdownlist with data out of my database. I have the following:
Controler:
//
// GET: /StoreEditor/Edit/(ID)
public ActionResult Edit(int id)
{
StoreEditorViewModel data = new StoreEditorViewModel(id);
return View(data);
}
ViewModel:
public StoreEditorViewModel(int id)
{
using (MvcTicketsEntities storeDB = new MvcTicketsEntities())
{
details = (from t in storeDB.Tickets.Include("Artists").Include("Genres")
where t.TicketId == id
select t).FirstOrDefault();
}
}
Now i get get all my labels and editors in de view. But how do i make a dropdownlist with all the names in the database table GENRES in the field NAME.
The table GENRES has 2 fields GenreId and Name.
Please refer to the below link:
http://codeclimber.net.nz/archive/2009/08/10/how-to-create-a-dropdownlist-with-asp.net-mvc.aspx
This explains quite well, how to approach it.
Basically, you would need to create List<SelectListItem> object and supply it to the dropdown list.
Thanks

How do I correctly use EF4 Navigation Properties?

I've created a database using the EF4 model-first approach. In my model, there's an N-to-M relationship between two entities:
I've filled my database with some dummy data, including 3 records of type Diagnosis and 3 records of type TreatmentSchema and associations between them. Here's the code snippet I used to do this:
using(var container = new SmartTherapyContainer()) {
var diagnosisA = new Diagnosis() { Id = Guid.NewGuid(), Name = "Diagnosis A" };
var diagnosisB = new Diagnosis() { Id = Guid.NewGuid(), Name = "Diagnosis B" };
var diagnosisC = new Diagnosis() { Id = Guid.NewGuid(), Name = "Diagnosis C" };
container.Diagnoses.AddObject(diagnosisA);
container.Diagnoses.AddObject(diagnosisB);
container.Diagnoses.AddObject(diagnosisC);
var schemaA = new TreatmentSchema() { Id = Guid.NewGuid(), Name = "Schema 1" };
var schemaB = new TreatmentSchema() { Id = Guid.NewGuid(), Name = "Schema 1" };
var schemaC = new TreatmentSchema() { Id = Guid.NewGuid(), Name = "Schema 1" };
container.Schemas.AddObject(diagnosisA);
container.Schemas.AddObject(diagnosisB);
container.Schemas.AddObject(diagnosisC);
diagnosisB.TreatmentSchemas.Add(schemaA);
diagnosisC.TreatmentSchemas.Add(schemaA);
diagnosisC.TreatmentSchemas.Add(schemaB);
diagnosisC.TreatmentSchemas.Add(schemaC);
container.SaveChanges();
}
I verified that the associations are indeed stored in the reference table created through EF4's mapping. However, when I retrieve a Diagnosis via the container.Diagnoses collection later, its .TreatmentSchemas collection is always empty.
I tried debugging into the EF4-generated code and all it does is lazily create said collection, but it doesn't fill it with the associated objects. Ayende's Entity Framework Profiler shows no queries being generated at all when the property is accessed, which leads me to believe that I'm doing something wrong here.
How can I get a list of the associated TreatmentSchemas?
Navigation properties are not loaded by default. You must use either eager loading or lazy loading but because you are using self tracking entities your choice is only eager loading because STEs don't support lazy loading. So if you want to get Diagonstic instance with all related TreatmentSchemas you must call:
var diagnosis = context.Diagnoses.Include("TreatmentSchemas").FirstOrDefault();

Resources