Flex 3 - how to select an item in the list as default? - list

how to select an item in the list as default
default selected item say of index 0
I tried stuff like this --
listid.selectedIndex = somevalueinmyprogram - 1; // 0
but when i debug this i get
_selectedIndex = 0
selectedIndex = -1
and default value is not selected why so?
[i have already checked for the obvious that somevaluefrommyprogram is not equal to 0]
Help!

I have found that if you set the selectedItems by defining an array of selected items it works better than the selectedIndex.
function setSelectedCategories():void{
var selectedItems :Array = new Array();
for each (var selectedCategory:Category in entry.categories) {
for each (var category:Category in categories) {
if (selectedCategory.categoryID == category.categoryID){
selectedItems .push(category);
break;
}
}
}
categoriesList.selectedItems = selectedItems ;
}
OR using the selectedIndices works if you want to use an array that contains the indexes that you want to be selected.
for ( var i:int=0; i < userIpods.length; i++ ) {
//j will represent the list item's index value
for ( var j:int = 0; j < iPodAry.length; j++) {
if ( userIpods[i] == iPodAry[j].id ) {
selectedIpodIndices.push( j );
break;
} //end if
} //end for ( var iPodObj:Object in iPodAry) {
} //end for ( var i:int in userIpods )
/*mark as selected those index values in the
selectedIpodIndices array*/
iPodList.selectedIndices = selectedIpodIndices ;

Related

Replace non-blank cells with 0

I tried to find solutions online however couldn't find one specifically for my need: I want to create a script which replaces non-blank cells in given column with 0.
Is there a simple solution for this?
Thanks.
Try:
function blankTo0() {
var ss=SpreadsheetApp.getActiveSpreadsheet()
var s = ss.getActiveSheet()
var rng = s.getRange("A:A");//change to column you want
var data= rng.getValues()
for (var i=0; i < data.length; i++) {
if (data[i][0] == "") {
data[i][0] = 0;
} else if (data[i][0] == "") {
data[i][0] = data[i][0];
}}
rng.setValues(data); // replace old data with new
}

Adding items to a Google Forms list from a spreadsheet range

I am struggling to get past the last line in this code any help will be appreciated:
The error I am getting is "Array is empty: values (line 16, file "Code")". I have double checked the item ID, the spreadsheet ID and that there is data for it to pick up within the correct range. Any pointers or insights...?
function GetFleet() {
var ssDEFECTS = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];
var rngFLEET = ssDEFECTS.getDataRange();
var values = rngFLEET.getValues();
var FleetList = [];
//Use column 0 and ignore row 1 (headers)
for (var i = 1; i <= values.length; i++) {
var v = values[i] && values[i][0];
v && values.push(v)
}
// Form ID & List ID
var DefectsForm = FormApp.openById('<FORM KEY ID>');
DefectsForm.getItemById(794194842).asListItem().setChoiceValues(FleetList);
};
Nothing is pushed to your array FleetList. Also the coding in your loop is faulty.
Assuming you want to push the first column (not including the header),
try this and see if it works
function GetFleet() {
var values = SpreadsheetApp.getActiveSpreadsheet()
.getSheets()[0].getDataRange()
.getValues();
var FleetList = [];
//Use column 0 and ignore row 1 (headers)
for (var i = 1, len = values.length; i < len; i++) {
FleetList.push(values[i][0])
}
// Form ID & List ID
var DefectsForm = FormApp.openById('<FORM KEY ID>');
DefectsForm.getItemById(794194842)
.asListItem()
.setChoiceValues(FleetList);
};

Number of entries in 'li' - list form (on Datatable)

I am totally new here, I will try to explain. On each data table header we have show entries, were we have options something like 10,20,30... This is normally displayed in select options, Insted i want it to be in List in ul li.
You need to modify jquery.dataTables.js file, refer below code which create menu item for length:- (Please note, this is code from jquery.dataTables.js file and you need to modify according to convert select option dropdown to ul li list)
/* This can be overruled by not using the _MENU_ var/macro in the language variable */
var sName = 'name="'+oSettings.sTableId+'_length"';
var sStdMenu = '<select size="1" '+sName+'>';
var i, iLen;
var aLengthMenu = oSettings.aLengthMenu;
if ( aLengthMenu.length == 2 && typeof aLengthMenu[0] === 'object' &&
typeof aLengthMenu[1] === 'object' )
{
for ( i=0, iLen=aLengthMenu[0].length ; i<iLen ; i++ )
{
sStdMenu += '<option value="'+aLengthMenu[0][i]+'">'+aLengthMenu[1][i]+'</option>';
}
}
else
{
for ( i=0, iLen=aLengthMenu.length ; i<iLen ; i++ )
{
sStdMenu += '<option value="'+aLengthMenu[i]+'">'+aLengthMenu[i]+'</option>';
}
}
sStdMenu += '</select>';
var nLength = document.createElement( 'div' );
if ( !oSettings.aanFeatures.l )
{
nLength.id = oSettings.sTableId+'_length';
}
nLength.className = oSettings.oClasses.sLength;
nLength.innerHTML = '<label>'+oSettings.oLanguage.sLengthMenu.replace( '_MENU_', sStdMenu )+'</label>';
/*
* Set the length to the current display length - thanks to Andrea Pavlovic for this fix,
* and Stefan Skopnik for fixing the fix!
*/
$('select option[value="'+oSettings._iDisplayLength+'"]', nLength).attr("selected", true);
$('select', nLength).bind( 'change.DT', function(e) {
var iVal = $(this).val();
/* Update all other length options for the new display */
var n = oSettings.aanFeatures.l;
for ( i=0, iLen=n.length ; i<iLen ; i++ )
{
if ( n[i] != this.parentNode )
{
$('select', n[i]).val( iVal );
}
}
/* Redraw the table */
oSettings._iDisplayLength = parseInt(iVal, 10);
_fnCalculateEnd( oSettings );
/* If we have space to show extra rows (backing up from the end point - then do so */
if ( oSettings.fnDisplayEnd() == oSettings.fnRecordsDisplay() )
{
oSettings._iDisplayStart = oSettings.fnDisplayEnd() - oSettings._iDisplayLength;
if ( oSettings._iDisplayStart < 0 )
{
oSettings._iDisplayStart = 0;
}
}
if ( oSettings._iDisplayLength == -1 )
{
oSettings._iDisplayStart = 0;
}
_fnDraw( oSettings );
} );

Open XML Excel Cell Formatting

I am trying to export to excel using Open XML with simple formatting. Export to Excel is working. The problem is with formatting the data. I am trying to have very basic formatting. i.e. the Column names should be in bold and rest of the content in normal font. This is what I did. Please let me know where am I going wrong.
private Stylesheet GenerateStyleSheet()
{
return new Stylesheet(
new Fonts(
new Font(new DocumentFormat.OpenXml.Spreadsheet.FontSize { Val = 12},
new Bold(),
new Font(new DocumentFormat.OpenXml.Spreadsheet.FontSize { Val = 12}))
)
);
}
protected void ExportExcel(DataTable dtExport)
{
Response.ClearHeaders();
Response.ClearContent();
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml";
//"application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml" '"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" '"application/vnd.ms-excel"
Response.AddHeader("content-disposition", "attachment; filename=Test.xlsx");
Response.Charset = "";
this.EnableViewState = false;
MemoryStream ms = new MemoryStream();
SpreadsheetDocument objSpreadsheet = SpreadsheetDocument.Create(ms, SpreadsheetDocumentType.Workbook);
WorkbookPart objWorkbookPart = objSpreadsheet.AddWorkbookPart();
objWorkbookPart.Workbook = new Workbook();
WorksheetPart objSheetPart = objWorkbookPart.AddNewPart<WorksheetPart>();
objSheetPart.Worksheet = new Worksheet(new SheetData());
Sheets objSheets = objSpreadsheet.WorkbookPart.Workbook.AppendChild<Sheets>(new Sheets());
Sheet objSheet = new Sheet();
objSheet.Id = objSpreadsheet.WorkbookPart.GetIdOfPart(objSheetPart);
objSheet.SheetId = 1;
objSheet.Name = "mySheet";
objSheets.Append(objSheet);
WorkbookStylesPart stylesPart = objSpreadsheet.WorkbookPart.AddNewPart<WorkbookStylesPart>();
stylesPart.Stylesheet = GenerateStyleSheet();
stylesPart.Stylesheet.Save();
objSheetPart.Worksheet.Save();
objSpreadsheet.WorkbookPart.Workbook.Save();
for (int cols = 0; cols < dtExport.Columns.Count; cols++)
{
Cell objCell = InsertCellInWorksheet(GetColumnName(cols), 1, objSheetPart);
objCell.CellValue = new CellValue(dtExport.Columns[cols].ColumnName);
objCell.DataType = new EnumValue<CellValues>(CellValues.String);
objCell.StyleIndex = 0;
}
objSheetPart.Worksheet.Save();
objSpreadsheet.WorkbookPart.Workbook.Save();
for (uint row = 0; row < dtExport.Rows.Count; row++)
{
for (int cols = 0; cols < dtExport.Columns.Count; cols++)
{
//row + 2 as we need to start adding data from second row. First row is left for header
Cell objCell = InsertCellInWorksheet(GetColumnName(cols), row + 2, objSheetPart);
objCell.CellValue = new CellValue(Convert.ToString(dtExport.Rows[Convert.ToInt32(row)][cols]));
objCell.DataType = new EnumValue<CellValues>(CellValues.String);
objCell.StyleIndex = 1;
}
}
objSheetPart.Worksheet.Save();
objSpreadsheet.WorkbookPart.Workbook.Save();
objSpreadsheet.Close();
ms.WriteTo(Response.OutputStream);
Response.Flush();
Response.End();
}
// Given a column name, a row index, and a WorksheetPart, inserts a cell into the worksheet.
// If the cell already exists, return it.
private Cell InsertCellInWorksheet(string columnName, uint rowIndex, WorksheetPart worksheetPart)
{
Worksheet worksheet = worksheetPart.Worksheet;
SheetData sheetData = worksheet.GetFirstChild<SheetData>();
string cellReference = (columnName + rowIndex.ToString());
// If the worksheet does not contain a row with the specified row index, insert one.
Row row = default(Row);
if ((sheetData.Elements<Row>().Where(r => r.RowIndex.Value == rowIndex).Count() != 0))
{
row = sheetData.Elements<Row>().Where(r => r.RowIndex.Value == rowIndex).First();
}
else
{
row = new Row();
row.RowIndex = rowIndex;
sheetData.Append(row);
}
// If there is not a cell with the specified column name, insert one.
if ((row.Elements<Cell>().Where(c => c.CellReference.Value == columnName + rowIndex.ToString()).Count() > 0))
{
return row.Elements<Cell>().Where(c => c.CellReference.Value == cellReference).First();
}
else
{
// Cells must be in sequential order according to CellReference. Determine where to insert the new cell.
Cell refCell = null;
foreach (Cell cell in row.Elements<Cell>())
{
if ((string.Compare(cell.CellReference.Value, cellReference, true) > 0))
{
refCell = cell;
break; // TODO: might not be correct. Was : Exit For
}
}
Cell newCell = new Cell();
newCell.CellReference = cellReference;
row.InsertBefore(newCell, refCell);
return newCell;
}
}
It seems like you are missing a ")" after your first font creation. So then you end opp with only one font index (the default one)
Below is the code I use for exactly what you are asking for.
You might remove fills and borders and remove them from cellformat, but I had some syntax problems while writing this so I just left it when it all worked :-)
private Stylesheet GenerateStyleSheet()
{
return new Stylesheet(
new Fonts(
// Index 0 - Default font.
new Font(
new FontSize() { Val = 11 },
new Color() { Rgb = new HexBinaryValue() { Value = "000000" } }
),
new Font(
new Bold(),
new FontSize() { Val = 11 },
new Color() { Rgb = new HexBinaryValue() { Value = "000000" } }
)
),
new Fills(
// Index 0 - Default fill.
new Fill(
new PatternFill() { PatternType = PatternValues.None })
),
new Borders(
// Index 0 - Default border.
new Border(
new LeftBorder(),
new RightBorder(),
new TopBorder(),
new BottomBorder(),
new DiagonalBorder())
),
new CellFormats(
// Index 0 - Default cell style
new CellFormat() { FontId = 0, FillId = 0, BorderId = 0 },
new CellFormat() { FontId = 1, FillId = 0, BorderId = 0, ApplyFont = true }
)
);
}

Setting default Index for a Drop Down List in flex 4

I have a drop down list that gets its data from a php web service method.This response is smthing like :
array('id'=>integer,'name'=>'lin')
When the page loads, I want to set the selected index to "lin" initially. How do I do this ?
you just need to set selectedIndex property of dropdownlist control.
ex.
dwl.selectedIndex = 1; // "Index of "lin"
you should do this.
var iIndex:int;
for(var i:int = 0; i < arrResponse.length; i++)
{
// if(Array(arrResponse[i])[1] == "lin")
if(Array(arrResponse[i]).name == "lin") {
iIndex = i;
}
}
dwl.selectedIndex = iIndex;