我的ngModel在选择未显示为已选中时出现问题.id和name都匹配但不起作用,请参阅selectedState.将模型指向options数组中的实际对象,请参阅selelectedState2.不知道发生了什么......
小提琴:http: //jsfiddle.net/fedorsmirnoff/b49n4Ldp/2/
<select ng-model="selectedState" ng-options="state.name for state in stateOptions"></select>
<select ng-model="selectedState2" ng-options="state.name for state in stateOptions"></select>
function MainCtrl($scope) {
$scope.stateOptions = [
{id: 1, name: "Alaska"},
{id: 2, name: "Montana"},
{id: 3, name: "Nebraska"},
{id: 4, name: "Texas"}
]
$scope.selectedState = {id: 2, name: "Montana"};
$scope.selectedState2 = $scope.stateOptions[1];
}
Run Code Online (Sandbox Code Playgroud) javascript angularjs angularjs-ng-options angularjs-ng-model
我正在寻找一种克隆/重命名或重新创建地址字段的有效方法,以提供在同一页面上提交多个地址的能力.所以使用这样的表单示例:
<div id="addresses">
<div class="address">
<input type="text" name="address[0].street">
<input type="text" name="address[0].city">
<input type="text" name="address[0].zip">
<input type="text" name="address[0].state">
</div>
</div>
<a href="" id="add_address">Add address form</a>
Run Code Online (Sandbox Code Playgroud)
根据我的理解,有两种选择:
按字段重新创建表单字段并递增索引,这是一种详细的:
var index = $(".address").length;
$('<`input`>').attr({
name: 'address[' + index + '].street',
type: 'text'
}).appendTo(...);
$('<`input`>').attr({
name: 'address[' + index + '].city',
type: 'text'
}).appendTo(...);
$('<`input`>').attr({
name: 'address[' + index + '].zip',
type: 'text'
}).appendTo(...);
$('<`input`>').attr({
name: 'address[' + index + '].state',
type: 'text'
}).appendTo(...);
Run Code Online (Sandbox Code Playgroud)克隆现有图层并替换克隆中的名称:
$("div.address").clone().appendTo($("#addresses"));
Run Code Online (Sandbox Code Playgroud)你建议使用哪一个更高效,如果它的#2可以请你建议我如何进行搜索并用[1]([n])替换所有出现的[0].谢谢.
所以我的字符串看起来像这样:
Basic information, advanced information, super information, no information
Run Code Online (Sandbox Code Playgroud)
我想把所有内容捕获到第二个逗号,所以我得到:
基本信息,高级信息
什么是正则表达式?
我试过了(.*,.*),但是我明白了
基本信息,高级信息,超级信息,
我有一个多选择变量发布到控制器.多个选择的工作方式是,如果只选择了一个值,则将其作为单个String传递;如果选择了多个值,则将其作为String []传递.我想保持处理简单并将传递的值视为相同.所以我能做到的最好的方法是将其转换为List,如下所示:
def selectedValues = params.selectedValues
List valuelist = new ArrayList()
if(selectedValues instanceof String) {
valuelist.add(selectedValues)
} else {
valuelist = selectedValues as List
}
Run Code Online (Sandbox Code Playgroud)
它有效,但我很好奇,如果有一个更通常的方式来做这个,也许有一个班轮:).
当然,如果我只是这样做:
List valuelist = selectedValues as List
Run Code Online (Sandbox Code Playgroud)
它不适用于单个选定值,因为它会将它从24个转换为[2,4]
有任何想法吗?
这是扩展Grails父类和子类的正确方法吗?
最初我认为覆盖hasMany和belongsTo属性,这将是一个好主意但是它没有工作得那么好,因为它引入了冲突的功能,所以我从子类中删除它.
我在这里要做的是在多个应用程序之间打包共享代码.我在插件中开始使用这两个类.
class Purchase {
String firstName
String lastName
static mapping = {
tablePerHierarchy false
}
static hasMany = [items: PurchaseItem]
}
class PurchaseItem {
BigDecimal price
Integer qty
statiuc belongsTo = [purchase: Purchase]
static mapping = {
tablePerHierarchy false
}
}
Run Code Online (Sandbox Code Playgroud)
特定于应用程序的类必须扩展Purchase和PurchaseItem,所以我这样实现它,继承一对多的关系:
class Flight {
static hasMany = [purchases: TicketPurchase]
}
class TicketPurchase extends Purchase {
// some class specific properties
static belongsTo = [flight: Flight]
}
class TicketPurchaseItem extends PurchaseItem
Integer bagQty
static namedQueries = {
ticketPurchaseItemsWithBagsByFlight …Run Code Online (Sandbox Code Playgroud) 这是一个相当奇怪的问题,我已经有一段时间了,所以我疯了.
我有一个控制器扩展另一个控制器,所以我可以让多个控制器继承一个方法,他们是这样的:
class EventController extends EventAwareController {
def springSecurityService
def edit = {
// this line prints out principal id
println springSecurityService.principal.id
def eventInstance = getAuthorizedEventById(params.id)
if (!eventInstance) {
flash.message = "${message(code: 'event.not.found.message')}"
redirect(action: "list", controller: "event")
return false
}
}
class EventAwareController {
def eventService
def springSecurityService
def getAuthorizedEventById(def eventId) {
def event
if (eventId) {
// this springSecurityService is null and throws an error
event = eventService.findAuthorizedEvent(eventId, springSecurityService.principal.id)
if (event) {
session.eventId = eventId
}
}
return event …Run Code Online (Sandbox Code Playgroud) 有没有办法将Grails转换不匹配错误更改为自定义消息?
我正进入(状态:
无法将类型的属性值转换为属性
java.lang.String所需的类型;嵌套异常是java.lang.IllegalStateException:无法将类型[java.lang.String]的值转换为属性项的必需类型[java.util.Map]:无匹配编辑或转换策略找到java.util.Mapitems
此错误类型不在messages.properties.我得到这个,如果用户试图将请求参数注入我的命令对象的地图,他们不应该这样做,但除此之外:
class CartCommand implements Serializable {
Map<Integer, Integer> items =
MapUtils.lazyMap([:], FactoryUtils.constantFactory(''))
}
Run Code Online (Sandbox Code Playgroud)
谢谢
到目前为止我所拥有的是:
def imageColumns = ["products_image", "procuts_subimage1", "products_subimage2", "prodcuts_subimage3", "products_subimage4"]
def imageValues = ["1.jpg","2.jpg","3.jpg"]
def imageColumnsValues = []
// only care for columns with values
imageValues.eachWithIndex { image,i ->
imageColumnsValues << "${imageColumns[i]} = '${image}'"
}
println imageColumnValuePair.join(", ")
Run Code Online (Sandbox Code Playgroud)
它有效,但我认为它会更好.希望有一个collectWithIndex ...有什么建议吗?
我需要得到每个商店每个订单销售的所有商品的总和.我正在使用executeQuery()在表达式上运行sum().它工作得很好,如下所示,但我想知道是否有更好,更时髦的方法来做到这一点.
StoreService {
static transactional = false
def getTotalOrders(def store) {
return Store.executeQuery("select sum(a.soldQuantity * a.soldPrice) as total
from OrderItem a inner join a.order b inner join b.store c
where c= :store", [store: store]).get(0)
}
}
Store {
transient storeService
def getTotalSales() {
storeService.getTotalSales()
}
static hasMany = [items: Item]
// no hasMany to Order
}
Item {
static belongsTo = [store: Store]
// no hasMany to OrderItem
}
Order {
static hasMany = [orderItems: OrderItem]
static belongsTo = [store: …Run Code Online (Sandbox Code Playgroud)