重新加载tableview的选择器元素与更新的数据在钛

reload tableview on click of picker element with updated data in titanium

本文关键字:元素 更新 数据 选择器 加载 tableview 新加载      更新时间:2023-09-26

我有一个json响应,像这样来自API调用。

`{
"id": "7",
"issue_title": "Apr - May 2015",
"issue_no": "Issue 1.4",
"cover_image_url": "http://www.link.org/apr--may-2015-7.jpg",
"synopsis_pdf_url": "",
"advertisers_pdf_url": "",
"issue_date": "01-04-2015",
"issue_year": "2015" 
},
{
"id": "3",
"issue_title": "Feb-Mar 2015",
"issue_no": "Issue 1.3",
"cover_image_url": "http://www.link.org/febmar-2015-3.jpg",
"synopsis_pdf_url": "http://www.link.org/febmar-2015-3.pdf",
"advertisers_pdf_url": "http://www.link.org/febmar-2015-3.pdf",
"issue_date": "01-02-2015",
"issue_year": "2015"
},
{
"id": "2",
"issue_title": "Dec 2014 - Jan 2015",
"issue_no": "Issue 1.2",
"cover_image_url": "http://www.link.org/dec-2014--jan-2015-2.jpg",
"synopsis_pdf_url": "",
"advertisers_pdf_url": "",
"issue_date": "01-12-2014",
"issue_year": "2014"
},
{
"id": "1",
"issue_title": "Oct - Nov 2014",
"issue_no": "Issue 1.1",
"cover_image_url": "http://www.link.org/oct--nov-2014-1.jpg",
"synopsis_pdf_url": "",
"advertisers_pdf_url": "",
"issue_date": "01-10-2014",
"issue_year": "2014"
}`

现在我正在检索我在选择器中显示的issue_year

我想如果用户点击issue_year表数据应该只显示记录具有相同的issue_year。就像如果2014从选择器点击它应该只显示第三和第四个记录。现在,从控制器,我正在传递对象,其中jsonResponse[I]在选择器中,当选择器数据准备,以便在其点击中检索相同的,但我得到undefined

js代码如下

function openpastIssues(e) {
var url = "http://example.com/api/past_year_issue.aspx";
var jsonResponse;
var response;
var data = [];
var pickerData = [];
var singleData = [];
var row;
var xhr = Ti.Network.createHTTPClient({
    onload : function() {
        // parse the retrieved data, turning it into a JavaScript object
        Ti.API.info("From Past Issues (function): " + this.responseText);
        jsonResponse = JSON.parse(this.responseText);
        var rowHeight = 150;
        // just pick the right height here
        var imageRowIsFull = false;
        var myImage;
        var issuelbl;
        var row = Ti.UI.createTableViewRow({
            width : Ti.UI.SIZE,
            height : Ti.UI.SIZE,
            layout : "horizontal"
        });
        for (var i = 0; i < jsonResponse.length; i++) {
            var tView = Ti.UI.createView({
                width : Ti.UI.SIZE,
                height : Ti.UI.SIZE,
                left : "5%",
                right : "5%",
                top : "5",
                bottom : "5",
                backgroundColor : "#A9F5A9",
                //backgroundColor : "black",
                layout : "vertical"
            });
            issuelbl = Ti.UI.createLabel({
                color : 'blue',
                text : jsonResponse[i].issue_title,
                height : "auto",
                width : "auto",
                left : "3%",
                font : {
                    fontSize : 13
                },
            });
            tView.add(issuelbl);
            myImage = Ti.UI.createImageView({
                width : 120,
                //left : "5%",
                //right : "5%",
                top : "5",
                //bottom:"5",
                height : 150,
                image : jsonResponse[i].cover_image_url,
                obj : jsonResponse[i]
            });
            tView.add(myImage);
            try {
                myImage.addEventListener('click', function(e) {
                    Ti.API.info('Clicked data = ' + JSON.stringify(e.source.obj));
                    var jsonRes = e.source.obj;
                    Alloy.createController('singleissue', jsonRes);
                });
            } catch(e) {
            }
            row.add(tView);
            //row.add(myImage);
            imageRowIsFull = false;
            if ((i + 1) % 2 == 0) {//this will add a new row every 2 items.
                imageRowIsFull = true;
                data.push(row);
                row = Ti.UI.createTableViewRow({
                    width : Ti.UI.SIZE,
                    height : Ti.UI.SIZE,
                    layout : "horizontal",
                    filterCriteria :jsonResponse[i].issue_year
                });
            }
            pickerData[i] = Ti.UI.createPickerRow({
            title : jsonResponse[i].issue_year,
            obj : jsonResponse[i]
            });
        }
        if (!imageRowIsFull) {//do not forget to add the last row
            data.push(row);
        }

        //table.setData(data); 
        $.issueTable.setData(data);
        $.picker.add(pickerData);
    },
    onerror : function(e) {
        Ti.API.debug(e.error);
        alert('error');
    },
    timeout : 5000
});
xhr.open("GET", url);
xhr.send();
}
$.picker.addEventListener('change', function(e) {
Ti.API.info('From picker ' + e.source.obj);
});
xml文件

<Alloy>
<Window id="winpast" class="container" title="Past issues" onOpen="openpastIssues">
    <View id="view2"  width="Ti.UI.FILL" height="Ti.UI.FILL" backgroundColor="#A9F5A9" >
        <View id="viewcheck1" >
            <Label id="title" width="Ti.UI.SIZE" text="Past Issues" height="Ti.UI.SIZE" textAlign="Ti.UI.TEXT_ALIGNMENT_CENTER"></Label>
            <ImageView id="menuImg" image="/images/menu.png" onClick="showsideBar" left="5"></ImageView>
            <Picker id="picker"  selectionIndicator="true" height="Ti.UI.SIZE" width="Ti.UI.SIZE" right="10">
            </Picker>
        </View>
        <View id="tablePast" width="Ti.UI.FILL" height="Ti.UI.FILL" top="40">
            <TableView id="issueTable" scrollable="true"></TableView>
        </View>
        <View id="viewBelow" width="150" height="Ti.UI.FILL" backgroundColor="#A9A5A9" left="-150" visible="false" top="40">
            <TableView id="menuTable"></TableView>
        </View>
    </View>
</Window>
</Alloy>

基本上我想显示从picker中选择的特定年份的记录。

有谁能帮帮忙吗?我在这上面花了很多时间

你可以创建一个NSMUtableArray,并根据"issue_year"的值重新填充它。你可以使用NSPredicate对json

进行排序