Ich habe ein beobachtbares Array, das einige Informationen enthält. Ich möchte die Klasse basierend auf den im Array enthaltenen Daten dynamisch ändern. Ich habe versucht:CSS-Klasse aus Daten in ObservableArray setzen
#js
var pageData = new Observable({
locations: new ObservableArray([
{location: 'OR1'},
{location: 'OR2'},
{location: 'OR3'},
{location: 'WR1'},
{location: 'PO1'}
]),
surgeons: new ObservableArray([
{surgeon: 'Dr. Pepper', selected_text: '', selected_class: ''},
{surgeon: 'Dr. Scholls', selected_text: "\uf111", selected_class: 'font-awesome'}
])
});
exports.loaded = function(args) {
var page = args.object;
page.bindingContext = pageData;
};
#xml
<Page xmlns="http://schemas.nativescript.org/tns.xsd" navigatingTo="onNavigatingTo" xmlns:statusBar="nativescript-statusbar"
class="green" loaded="loaded">
<GridLayout orientation="vertical" columns="*" rows="2*,*,3*,*,5*,*">
<Label text="mrn: 123456" row="0" class="h1" horizontalAlignment="center"/>
<Label text="Surgeon" class="h3" row="1"/>
<ListView col="0" row="2" items="{{ surgeons }}" separatorColor="#58847D" class="margin-sides-10 rounded-corners-all">
<ListView.itemTemplate>
<GridLayout orientation="vertical" columns="75,*" rows="*">
<Label text="{{ selected_text }}" class="{{ selected_class}} black-text" col="0"/>
<Label text="{{ surgeon }}" class="black-text" col="1"/>
</GridLayout>
</ListView.itemTemplate>
</ListView>
<Label text="Location" class="h3" row="3"/>
<ListView col="0" row="4" items="{{ locations }}" separatorColor="#58847D" class="margin-sides-10 rounded-corners-all">
<ListView.itemTemplate>
<Label text="{{ location }}" class="black-text"/>
</ListView.itemTemplate>
</ListView>
<Button text="Save" class="dark-grey margin-top-10" row="5" tap="save"/>
</GridLayout>
Was ist der beste Weg, um bedingt Stil einzelne Komponenten?
Ah, interessant. Die Fehlermeldungen sind ein wenig kryptisch. Angesichts der Tatsache, dass Sie nicht mischen können, was ist die beste Strategie, um das Styling für eine bestimmte Komponente, die mehrere CSS-Klassen haben kann, zu ändern? Wie würden Sie das Äquivalent in nativescript von jQuery machen: $(). RemoveClass(). AddClass()? – DataSwede
Nun, ich habe tatsächlich ein cooles Plugin namens NativeScript-Dom geschrieben, das element.classList.add, remove, toggle usw. anbietet. Die Community-Plugin-Liste befindet sich unter http://plugins.nativescript.rocks – Nathanael