【问题标题】:How to display image in detail view using dataTransform?如何使用 dataTransform 在详细视图中显示图像?
【发布时间】:2015-12-10 12:55:08
【问题描述】:

我可以使用 dataTransform 在我的索引页面中显示图像和其他来源。 但我在详细视图中显示相同的源图像时遇到问题。 我发布了我所有的代码,我的错误是 It doesn't display image but other attribute

// index.xml

<Alloy>
	<Collection src="products"/>
  <Window class="container">
    <TableView filterAttribute="title"  dataCollection="products" id="productTableView" dataTransform="myTransformer">
      <SearchBar platform="android,ios"id="search" barColor="#000" showCancel="true" height="43" top="0" />
  	<TableViewRow height="150dp" productId="{productid}">
        <View layout="vertical" top="10">
          <View height="150" layout="horizontal">
            <View backgroundColor="white" left="5dp" width="40%">
              // image here
              <ImageView image="{my_image}"></ImageView>
            </View>
            <View backgroundColor="white" layout="vertical" left="15dp" width="50%">
              <Label id="titel" text="{title}"></Label>
              <Label id="price" text="{price},{currency}"></Label>
            </View>
          </View>
        </View>
      </TableViewRow>
    </TableView>
  </Window>
</Alloy>

// productdetails.xml
<Alloy>
      <Window id="detailWindow" class="container" onClick="CloseWindow">
		<View layout="vertical" top="100dp">
			<ImageView image="{my_image}" width="96%"></ImageView>
			<Label id="titel" text="{title}"></Label>
            <Button id="closeBtn" title="Back"></Button>
		</View>
	</Window>
</Alloy>

//index.js

Alloy.Collections.products.fetch();

function myTransformer(model) {
  var transform = model.toJSON();

  transform.my_image = transform.images[0].sizes['100'];

  return transform;
}


$.index.open();


var args = arguments[0] || {};
var collection = Alloy.Collections.products;
collection.fetch({ 
    success : function(){
        _.each(collection.models, function(element, index, list){
    element.attributes.productid = element.cid;
           
 });
    },
    error : function(){
        Ti.API.error("hmm - this is not good!");
    }
});

$.productTableView.addEventListener('click', function(_event) {
	// get the correct model
	var model =
	    Alloy.Collections.products.getByCid(_event.rowData.productId);
	model.__transform = {};
	// create the controller and pass in the model
	var detailController = Alloy.createController('productdetails', {
	    $model : model
	});
    // get view returns the root view when no view ID is provided
    detailController.getView().open({
        model : true
    });
});

//productdetails.js


var args = arguments[0] || {};



// close the window when button is clicked
$.closeBtn.addEventListener('click', function() {
   CloseWindow();
});

function CloseWindow()
{
	 $.detailWindow.close();
}

// Free model-view data binding resources when this
// view-controller closes
$.detailWindow.addEventListener('close', function() {
    $.destroy();
});

【问题讨论】:

  • 在图片上设置宽度和高度?你有什么问题!
  • 我在 Detailview 页面上遇到问题 无法显示图像?
  • 创建 detailController 时,通过 data 属性传递特定的 Product。 var detailController = Alloy.createController('productdetails', { data : model });为什么要在 ScrollView 上设置属性 dataCollection?如果只是绑定到
  • 我删除了属性数据集合,现在只使用 {my_image} 但仍然有错误

标签: xml titanium titanium-alloy


【解决方案1】:

如果您想在详细信息页面上使用数据绑定,技巧是将模型作为$model 传递。在详细视图中,只需使用{attributeName}。我相信 Alloy 确实希望 $model.__transform 存在。所以在你通过之前一定要添加它(只是一个空对象)。

// get the correct model
var model =
    Alloy.Collections.products.getByCid(_event.rowData.productId);
model.__transform = {};
// create the controller and pass in the model
var detailController = Alloy.createController('productdetails', {
    $model : model
});

【讨论】:

  • 您遇到了哪个错误?如果您在创建控制器期间传递了您的模型(请参阅 Fokke Zandbergen 的评论),那么您的 detailView 的控制器已经知道该模型。您不必在 上设置 model="$.model"。假设您按照 Fokke Zandbergen 的解释创建了控制器,您的 detailview 应该只包含 .. (因此在 上没有 标签和模型属性
  • Hej Fokke,我的模型有问题吗?
  • 您的最新代码可用吗?或者您能否更新您的原始问题,使其反映您最新的代码更改。
  • 去掉“$.Product.set(args.data.attributes);”以及详细信息页面视图中的“”标签。应该这样做。
  • Hej Fokke 我摆脱了“$.Product.set(args.data.attributes);”还有模型标签......现在它不显示错误但它是空的我可以显示其他属性而不是图像。 Hej DavidCyp 我用更新的代码更新了问题。
猜你喜欢
  • 2016-10-25
  • 1970-01-01
  • 1970-01-01
  • 2020-01-28
  • 2011-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多