hp开发问题日志——selectpage 使用
最近修改fastadmin的selectpage,想用js动态修改选中值,查找文档,百度搜索都没有结果,有知道的大神可以留言,告诉我方法,不胜感谢。
在找解决方法时,看到一个属性searchField:查询关键字字段。fastadmin中,并没有这个属性,但是原生的有,我找了一下fastadmin的selectpage.js文件,搜索了词条searchField,找到这个地方
SelectPage.prototype.setOption=function (option) {
修改了里面的内容:
SelectPage.prototype.setOption=function (option) {
//use showField to default
//option.searchField=option.searchField || option.showField;
let search_field=option.searchfield || option.searchField || option.showField;
if(Array.isArray(search_field)){
var arr=['searchField'];
for (var i=0; i < arr.length; i++) {
option[arr[i]]=this.strToArray(option[arr[i]]);
}
}else{
option.searchField=search_field.split(',');
}
console.log(option);
option.andOr=option.andOr.toUpperCase();
if (option.andOr !=='AND' && option.andOr !=='OR') option.andOr='AND';
//support multiple field set
然后在view中html中添加了data-searchField="mobile,username,nickname",这样的效果就是可以输入一个关键词,用or搜索三个字段;html代码:
<input id="c-user_id" data-rule="required" data-source="user/user/index" data-params='{"custom[agent_type]":0}' data-field="username"
data-searchField="username,nickname,mobile" class="form-control selectpage" name="row[user_id]" type="text" value="">
控制器的selectpage方法的相关代码:
$logic=$andor=='AND' ? '&' : '|';
$searchfield=is_array($searchfield) ? implode($logic, $searchfield) : $searchfield;
$searchfield=str_replace(',', $logic, $searchfield);
$word=array_filter(array_unique($word));
if (count($word)==1) {
$query->where($searchfield, "like", "%" . reset($word) . "%");
} else {
$query->where(function ($query) use ($word, $searchfield) {
foreach ($word as $index=> $item) {
$query->whereOr(function ($query) use ($item, $searchfield) {
$query->where($searchfield, "like", "%{$item}%");
});
}
});
从控制器中的代码看,searchfield是可以为数组的;
参数传递的效果:
同时发送三个搜索字段
TML 的 select 标签是网页开发人员构建交互式下拉列表的强大工具。它允许用户从预定义的选项列表中进行选择,使表单输入更加高效且用户友好。在本文中,我们将全面探索 select 标签的各种可能性,揭秘一些提高表单功能和用户体验的技巧。
select 标签的基本语法
select 标签的基本语法如下:
<select>
<option value="value1">选项 1</option>
<option value="value2">选项 2</option>
<option value="value3">选项 3</option>
</select>
在这个例子中,select 标签定义了下拉列表,而 option 标签定义了列表中的选项。每个 option 标签都有一个 value 属性,表示选项的值,以及显示给用户的文本。
自定义选项
select 标签提供了多种属性来自定义选项:
来看一个带有默认选中和禁用选项的例子:
<select>
<option value="apple">苹果</option>
<option value="banana" selected>香蕉</option>
<option value="orange" disabled>橙子</option>
</select>
在这个例子中,"香蕉" 选项将被默认选中,而 "橙子" 选项将被禁用。
增强 select 标签
select 标签可以通过多种方式进行增强,以提高用户体验:
来看一个带有多个选中的例子:
<select multiple>
<option value="apple">苹果</option>
<option value="banana">香蕉</option>
<option value="orange">橙子</option>
</select>
在这个例子中,用户可以选择多个水果选项。
样式化 select 标签
虽然 select 标签的样式化受到浏览器限制,但你仍然可以使用 CSS 来一定程度地美化它:
来看一个添加自定义样式的例子:
<style>
select {
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
}
select:hover {
border-color: #007bff;
}
</style>
<select>
<option value="option1">选项 1</option>
<option value="option2">选项 2</option>
</select>
在这个例子中,我们使用 CSS 为 select 标签添加了内边距、边框和圆角,并在悬停时改变边框颜色。
结论:打造动态选择体验
HTML select 标签为网页开发人员提供了创建动态选择体验的强大工具。通过结合选项自定义、增强功能和样式化技术,你可以创建出高效、直观且视觉吸引人的下拉列表。不断探索 select 标签的无限可能,让你的网页表单更加充满活力和互动性!释放你的创造力,打造令人难忘的用户选择体验!
当用户进行鼠标框选选择了页面上的内容时,把选择的内容进行上报。
虽然这需求就一句话的事,但是很显然,没那么简单...
因为鼠标框选说起来简单,就是选择的内容,但是这包含很多中情况,比如:只选择文案、选择图片、选择输入框、输入框中的内容选择、iframe、等。
简单总结,分为以下几点:
鼠标框选包含以下几点:
老生常谈的技术点了,这里不能用节流,因为肯定不能你鼠标选择的时候,隔一段时间返回一段内容,肯定是选择之后一起返回。
这里用 debounce 主要也是用在事件监听和事件处理上。
事件监听,因为鼠标选择,不仅仅是鼠标按下到鼠标抬起,还包括双击、右键、全选。
需要使用事件监听对事件作处理。
Range 接口表示一个包含节点与文本节点的一部分的文档片段。
Range 是浏览器原生的对象。
<body>
<ul>
<li>Vite</li>
<li>Vue</li>
<li>React</li>
<li>VitePress</li>
<li>NaiveUI</li>
</ul>
</body>
<script>
// 创建 Range 对象
const range=new Range()
const liDoms=document.querySelectorAll("li");
// Range 起始位置在 li 2
range.setStartBefore(liDoms[1]);
// Range 结束位置在 li 3
range.setEndAfter(liDoms[2]);
// 获取 selection 对象
const selection=window.getSelection();
// 添加光标选择的范围
selection.addRange(range);
</script>
可以看到,选择内容为第二行和第三行
只选择 li 中的 itePres
可以看出 range 属性对应的值
const range=document.createRange();
const range=window.getSelection().getRangeAt(0)
if (document.caretRangeFromPoint) {
range=document.caretRangeFromPoint(e.clientX, e.clientY);
}
const range=new Range()
Selection 对象表示用户选择的文本范围或插入符号的当前位置。它代表页面中的文本选区,可能横跨多个元素。
window.getSelection()
锚指的是一个选区的起始点(不同于 HTML 中的锚点链接)。当我们使用鼠标框选一个区域的时候,锚点就是我们鼠标按下瞬间的那个点。在用户拖动鼠标时,锚点是不会变的。
选区的焦点是该选区的终点,当你用鼠标框选一个选区的时候,焦点是你的鼠标松开瞬间所记录的那个点。随着用户拖动鼠标,焦点的位置会随着改变。
范围指的是文档中连续的一部分。一个范围包括整个节点,也可以包含节点的一部分,例如文本节点的一部分。用户通常下只能选择一个范围,但是有的时候用户也有可能选择多个范围。
一个用户可编辑的元素(例如一个使用 contenteditable 的 HTML 元素,或是在启用了 designMode 的 Document 的子元素)。
首先要清楚,选择的起点称为锚点(anchor),终点称为焦点(focus)。
function debounce (fn, time=500) {
let timeout=null; // 创建一个标记用来存放定时器的返回值
return function () {
clearTimeout(timeout) // 每当触发时,把前一个 定时器 clear 掉
timeout=setTimeout(()=> { // 创建一个新的 定时器,并赋值给 timeout
fn.apply(this, arguments)
}, time)
}
}
/**
* debounce 函数类型
*/
type DebouncedFunction<F extends (...args: any[])=> any>=(...args: Parameters<F>)=> void
/**
* debounce 防抖函数
* @param {Function} func 函数
* @param {number} wait 等待时间
* @param {false} immediate 是否立即执行
* @returns {DebouncedFunction}
*/
function debounce<F extends (...args: any[])=> any>(
func: F,
wait=500,
immediate=false
): DebouncedFunction<F> {
let timeout: ReturnType<typeof setTimeout> | null
return function (this: ThisParameterType<F>, ...args: Parameters<F>) {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const context=this
const later=function () {
timeout=null
if (!immediate) {
func.apply(context, args)
}
}
const callNow=immediate && !timeout
if (timeout) {
clearTimeout(timeout)
}
timeout=setTimeout(later, wait)
if (callNow) {
func.apply(context, args)
}
}
}
nterface IGetSelectContentProps {
type: 'html' | 'text'
content: string
}
/**
* 获取选择的内容
* @returns {null | IGetSelectContentProps} 返回选择的内容
*/
const getSelectContent=(): null | IGetSelectContentProps=> {
const selection=window.getSelection()
if (selection) {
// 1. 是焦点在 input 输入框
// 2. 没有选中
// 3. 选择的是输入框
if (selection.isCollapsed) {
return selection.toString().trim().length
? {
type: 'text',
content: selection.toString().trim()
}
: null
}
// 获取选择范围
const range=selection.getRangeAt(0)
// 获取选择内容
const rangeClone=range.cloneContents()
// 判断选择内容里面有没有节点
if (rangeClone.childElementCount > 0) {
// 创建 div 标签
const container=document.createElement('div')
// div 标签 append 复制节点
container.appendChild(rangeClone)
// 如果复制的内容长度为 0
if (!selection.toString().trim().length) {
// 判断是否有选择特殊节点
const isSpNode=hasSpNode(container)
return isSpNode
? {
type: 'html',
content: container.innerHTML
}
: null
}
return {
type: 'html',
content: container.innerHTML
}
} else {
return selection.toString().trim().length
? {
type: 'text',
content: selection.toString().trim()
}
: null
}
} else {
return null
}
}
/**
* 判断是否包含特殊元素
* @param {Element} parent 父元素
* @returns {boolean} 是否包含特殊元素
*/
const hasSpNode=(parent: Element): boolean=> {
const nodeNameList=['iframe', 'svg', 'img', 'audio', 'video']
const inpList=['input', 'textarea', 'select']
return Array.from(parent.children).some((node)=> {
if (nodeNameList.includes(node.nodeName.toLocaleLowerCase())) return true
if (
inpList.includes(node.nodeName.toLocaleLowerCase()) &&
(node as HTMLInputElement).value.trim().length
)
return true
if (node.children) {
return hasSpNode(node)
}
return false
})
}
/**
* 获取框选的文案内容
* @returns {string} 返回框选的内容
*/
const getSelectTextContent=(): string=> {
const selection=window.getSelection()
return selection?.toString().trim() || ''
}
// 是否时鼠标点击动作
let selectionchangeMouseTrack: boolean=false
const selectionChangeFun=debounce(()=> {
const selectContent=getSelectContent()
console.log('selectContent', selectContent)
// todo... 处理上报
selectionchangeMouseTrack=false
})
// 添加 mousedown 监听事件
document.addEventListener('mousedown', ()=> {
selectionchangeMouseTrack=true
})
// 添加 mouseup 监听事件
document.addEventListener(
'mouseup',
debounce(()=> {
selectionChangeFun()
}, 100)
)
// 添加 selectionchange 监听事件
document.addEventListener(
'selectionchange',
debounce(()=> {
if (selectionchangeMouseTrack) return
selectionChangeFun()
})
)
// 添加 dblclick 监听事件
document.addEventListener('dblclick', ()=> {
selectionChangeFun()
})
// 添加 contextmenu 监听事件
document.addEventListener(
'contextmenu',
debounce(()=> {
selectionChangeFun()
})
)
也可以进行封装
/**
* addEventlistener function 类型
*/
export interface IEventHandlerProps {
[eventName: string]: EventListenerOrEventListenerObject
}
let selectionchangeMouseTrack: boolean=false
const eventHandlers: IEventHandlerProps={
// 鼠标 down 事件
mousedown: ()=> {
selectionchangeMouseTrack=true
},
// 鼠标 up 事件
mouseup: debounce(()=> selectionChangeFun(), 100),
// 选择事件
selectionchange: debounce(()=> {
if (selectionchangeMouseTrack) return
selectionChangeFun()
}),
// 双击事件
dblclick: ()=> selectionChangeFun(),
// 右键事件
contextmenu: debounce(()=> selectionChangeFun())
}
Object.keys(eventHandlers).forEach((event)=> {
document.addEventListener(event, eventHandlers[event])
})
function debounce (fn, time=500) {
let timeout=null; // 创建一个标记用来存放定时器的返回值
return function () {
clearTimeout(timeout) // 每当触发时,把前一个 定时器 clear 掉
timeout=setTimeout(()=> { // 创建一个新的 定时器,并赋值给 timeout
fn.apply(this, arguments)
}, time)
}
}
let selectionchangeMouseTrack=false
document.addEventListener('mousedown', (e)=> {
selectionchangeMouseTrack=true
console.log('mousedown', e)
})
document.addEventListener('mouseup', debounce((e)=> {
console.log('mouseup', e)
selectionChangeFun()
}, 100))
document.addEventListener('selectionchange', debounce((e)=> {
console.log('selectionchange', e)
if (selectionchangeMouseTrack) return
selectionChangeFun()
}))
document.addEventListener('dblclick', (e)=> {
console.log('dblclick', e)
selectionChangeFun()
})
document.addEventListener('contextmenu',debounce(()=> {
selectionChangeFun()
}))
const selectionChangeFun=debounce(()=> {
const selectContent=getSelectContent()
selectionchangeMouseTrack=false
console.log('selectContent', selectContent)
})
const getSelectContent=()=> {
const selection=window.getSelection();
if (selection) {
// 1. 是焦点在 input 输入框
// 2. 没有选中
// 3. 选择的是输入框
if (selection.isCollapsed) {
return selection.toString().trim().length ? {
type: 'text',
content: selection.toString().trim()
} : null
}
// 获取选择范围
const range=selection.getRangeAt(0);
// 获取选择内容
const rangeClone=range.cloneContents()
// 判断选择内容里面有没有节点
if (rangeClone.childElementCount > 0) {
const container=document.createElement('div');
container.appendChild(rangeClone);
if (!selection.toString().trim().length) {
const hasSpNode=getSpNode(container)
return hasSpNode ? {
type: 'html',
content: container.innerHTML
} : null
}
return {
type: 'html',
content: container.innerHTML
}
} else {
return selection.toString().trim().length ? {
type: 'text',
content: selection.toString().trim()
} : null
}
} else {
return null
}
}
const getSpNode=(parent)=> {
const nodeNameList=['iframe', 'svg', 'img', 'audio', 'video']
const inpList=['input', 'textarea', 'select']
return Array.from(parent.children).some((node)=> {
if (nodeNameList.includes(node.nodeName.toLocaleLowerCase())) return true
if (inpList.includes(node.nodeName.toLocaleLowerCase()) && node.value.trim().length) return true
if (node.children) {
return getSpNode(node)
}
return false
})
}
*请认真填写需求信息,我们会在24小时内与您取得联系。