Quantcast
Channel: 簡睿隨筆
Viewing all articles
Browse latest Browse all 897

Obs131|Obsidian使用Dataviewjs動態查詢的嘗試

$
0
0

[!WARNING]+ 建議
建議有程式設計經驗的朋友們試用,本次介紹的是概念性的嘗試,不見得會增進你的筆記流程的完整性。

gh|700

1. 插入訊息區域

為了顯示更醒目的執行訊息,在筆記開頭處插入HTML標籤。

![gh|400](https://raw.githubusercontent.com/emisjerry/upgit/master/2023/1684643871000ycb31l.png)

![gh|400](https://raw.githubusercontent.com/emisjerry/upgit/master/2023/1684643933000pv6sau.png)

程式裡用 document.getElementById("ID") 取到HTML元素物件。

let MSG = document.getElementById("MSG");

eleDate.onfocus = function() {
  MSG.innerText = "";
}

2. 日期與標籤輸入元素

使用下列程式碼插入日期與標籤的輸入欄位,最後再加上查詢按鈕。

// Date input 
dv.span("Date: ");
let today = dv.date("today");
let eleDate = dv.el("input");
eleDate.value = dv.date("today").toString().substring(0, 10);
eleDate.style.width = "120px";

// Tag input
dv.span("  ");
dv.span("Tag: ");
let eleTag = dv.el("input");
eleTag.style.width = "120px";

// Query button
dv.span("  ");
let eleBtn = dv.el("button", "Query");

3. Dataviewjs查詢

  • 取出標籤元素的輸入值,形成井號標籤字串
  • 筆記過濾條件:略過 010-Templates與050-Scripts,若有輸入標籤則加入標籤條件(AND)
  • 顯示鏈接、標籤字串、建檔日、修改日、檔案大小(加入千位點)
  let data = [];
  try {
    let sTag = "";
    if (eleTag.value != "") {
      sTag = " and #" + eleTag.value;
    }
    data = dv.pages('-"010-Templates" and -"050-Scripts"'+sTag)
      .where(p=> p.file.mday.toString().substring(0,10)==sDate)
      .map(p => [ p.file.link, /*p.tags*/ "#"+p.tags.join(" #"), 
      p.created.substring(0,10), p.modified.substring(0,10), 
      p.file.size.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")]);
  } catch (e) {
    MSG.innerText = e.message;
  }
  // ...
  dv.header(2, sDate);
  dv.table(["File("+data.length+")", "Tags", "Created", "Modified", "Size"], 
    data);

4. 腳本原始碼

```dataviewjs
// Date input 
dv.span("Date: ");
let today = dv.date("today");
let eleDate = dv.el("input");
eleDate.value = dv.date("today").toString().substring(0, 10);
eleDate.style.width = "120px";

// Tag input
dv.span("  ");
dv.span("Tag: ");
let eleTag = dv.el("input");
eleTag.style.width = "120px";

// Query button
dv.span("  ");
let eleBtn = dv.el("button", "Query");
let MSG = document.getElementById("MSG");

function msg(sMsg) {
  new Notice(sMsg);
  MSG.innerText = sMsg;
}

eleDate.onfocus = function() {
  MSG.innerText = "";
}

eleBtn.onclick = function() {
  const sDate = eleDate.value;
  if (!dv.date(sDate)) {
    new Notice("Invalid date format!");
    MSG.innerText = "Invalid date format! Please try again.";
    return;
  }
  let data = [];
  try {
    let sTag = "";
    if (eleTag.value != "") {
      sTag = " and #" + eleTag.value;
    }
    data = dv.pages('-"010-Templates" and -"050-Scripts"'+sTag)
      .where(p=> p.file.mday.toString().substring(0,10)==sDate)
      .map(p => [ p.file.link, /*p.tags*/ "#"+p.tags.join(" #"), 
      p.created.substring(0,10), p.modified.substring(0,10), 
      p.file.size.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")]);
  } catch (e) {
    MSG.innerText = e.message;
  }
  msg("Matched items: " + data.length);
  let md_table = dv.markdownTable(["File("+data.length+")", "Tags", 
    "Created", "Modified", "Size"], data);
  if (data.length > 0) {
    dv.header(2, sDate);
    //dv.span(table);
    dv.table(["File", "Tags", 
      "Created", "Modified", "Size"], data);
    navigator.clipboard.writeText(md_table);
  }
}

5. 相關鏈接

6. 教學影片

https://youtu.be/JDPMHoZbjow

##

您可能也會有興趣的類似文章


Viewing all articles
Browse latest Browse all 897

Trending Articles