我对Google App Maker和编程很新,我正在创建一个应用程序,当点击“+”按钮(onClick事件)字段时添加(有点像每次点击一个表中的新行)。我正在尝试制作一个计数器来跟踪正在创建的“行”数。问题是,因为在App Maker中,所有代码显然都必须在onClick事件中进行,我无法设置全局变量,每次单击按钮时计数器都会重新启动,因此始终为1。
我尝试使用localStorage,但我不知道如何使它工作,因为它仍然会保存相同的值。我怎样才能解决这个问题?
var count = 0;
count = count +1;
localStorage.setItem('counter', JSON.stringify(parseFloat(count)));
textArea.className = 'app-TextArea';
textArea.style.margin = '8px';
textArea.setAttribute("placeholder", "Follow up # " + localStorage.getItem('counter');
widget.root.descendants.Panel1.getElement().appendChild(textArea);
您应首先从localStorage
获取数据,然后递增它,最后将新值设置为localStorage
。
var count = parseInt(localStorage('counter')) || 0; // Get value from localStorage
count = count + 1;
localStorage.setItem('counter', count); // Set new value to localStorage
textArea.className = 'app-TextArea';
textArea.style.margin = '8px';
textArea.setAttribute("placeholder", "Follow up # " + count); // Use new value
widget.root.descendants.Panel1.getElement().appendChild(textArea);
您可以使用custom properties页面。
因此,在页面中,您可以使用Number自定义属性并将其命名为counter。然后你的代码应该是这样的:
var count = widget.root.properties.counter || 0;
count += 1;
widget.root.properties.counter = count;
textArea.className = 'app-TextArea';
textArea.style.margin = '8px';
textArea.setAttribute("placeholder", "Follow up # " + count);
widget.root.descendants.Panel1.getElement().appendChild(textArea);